Java与C#通讯中的AES加密

背景

AES(高级加密标准:Advanced Encryption Standard)加密是一种对称的加密方式,用来替代原先的DES。

在此,将会介绍Java与C#在通讯过程中的对文本字符串的加密操作,分别对Java中的AES和C#中的AES加密进行说明,同时,当Java对文本进行加密后,C#能够根据相同的加密规则对文本进行解密,反之,亦然。
本文将会采用AES的ECB模式进行加密,填充方式为PKCS5Padding,加密的密码必须为16位。编码方式统一使用UTF-8

  • Java AES加密中的ECB加密模式对应于C#中的System.Security.Cryptography.CipherMode.ECB模式;
  • Java中的PKCS5Padding填充方式,对应于C#中的System.Security.Cryptography.PaddingMode.PKCS7;

Java中的AES加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by child on 2016/4/9.
* <p/>
* AES 加密/解密
*/
@SuppressWarnings("restriction")
public class AESEncryptUtil {
public static String encrypt(String content, String key) throws Exception {
if (StringUtils.isBlank(content) || StringUtils.isBlank(key)) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = cipher.doFinal(content.getBytes("utf-8"));
return new BASE64Encoder().encode(bytes);
}
public static String decrypt(String content, String key) throws Exception {
if (StringUtils.isBlank(content) || StringUtils.isBlank(key)) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = new BASE64Decoder().decodeBuffer(content);
bytes = cipher.doFinal(bytes);
return new String(bytes, "utf-8");
}
public static void main(String[] args) {
try {
String content = "helloworld";
String password = "1234567891234567";
String result = encrypt(content, password);
System.out.println(result);
System.out.println(decrypt(result, password));
} catch (Exception e) {
e.printStackTrace();
}
}

C#中的AES加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Security.Cryptography;
using System.Text;
namespace Util.Encrypt
{
// AES 加密
public static string encrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
// AES 解密
public static string decrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
}
}