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加密
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.
*
* 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加密
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);
}
}
}
Java中的AES加密(SHA1PRNG)
Java代码:
KeyGenerator aesGen = KeyGenerator.getInstance("AES");
SecureRandom secureRadmon= new SecureRandom().getInstance("SHA1PRNG");
secureRadmon.setSeed(aesKey.getBytes());
aesGen.init(128,secureRadmon);
SecretKey secretKey =aesGen.generateKey();
.Net(C#)代码:
byte[] keyArray = null;
using (var sha1 = new SHA1CryptoServiceProvider())
{
byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(aesKey));
var rd = sha1.ComputeHash(hash);
keyArray = rd.Take(16).ToArray();
}