1.AES算法
AES:密码学中的高级加密标准(Advanced Encryption Standard,AES)
AES算法是目前应用最广泛的加密算法。比较常见的工作模式是ECB和CBC。
AES加密算法:P(明文),K(密钥),D(加密函数),C(密文)
C = D(K,D) P = D(K,C)
2.AES算法的常见工作模式
1.ECB模式
ECB模式是是最简单的AES加密模式,它需要一个固定长度的密钥,固定的明文会生成固定的密文。
public static void main(String[] args) throws GeneralSecurityException {
// 原文:
String message = "天生我材必有用飞流直下三千尺";
System.out.println("Message(原始信息): " + message);
// 128位密钥 = 16 bytes Key:
byte[] key = "1234567890abcdef".getBytes();
// 加密:
byte[] data = message.getBytes();
byte[] encrypted = encrypt(key, data);
System.out.println("Encrypted(加密内容): " + Base64.getEncoder().encodeToString(encrypted));
// 解密:
byte[] decrypted = decrypt(key, encrypted);
System.out.println("Decrypted(解密内容): " + new String(decrypted));
}
// 加密:
public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 创建密码对象,需要传入算法/工作模式/填充模式
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 根据key的字节内容,"恢复"秘钥对象
SecretKey keySpec = new SecretKeySpec(key,"AES");
// 初始化秘钥:设置加密模式ENCRYPT_
cipher.init(Cipher.ENCRYPT_MODE,keySpec);
// 根据原始内容(字节),进行加密
return cipher.doFinal(input);
}
// 解密:
public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 创建密码对象,需要传入算法/工作模式/填充模式
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 根据key的字节内容,"恢复"秘钥对象
SecretKey keySpec = new SecretKeySpec(key,"AES");
// 初始化秘钥:设置解密模式DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE,keySpec);
// 根据原始内容(字节),进行解密
return cipher.doFinal(input);
}
1.根据算法名称/工作模式/填充模式 获取Cipher实例;
2.根据算法名称初始化一个Secretkey实例,密钥必须是指定长度;
3.使用Secretkey初始化Cipher实例,并设置加密或解密模式;
4.传入明文或密文,获取密文或明文。
2.CBC模式
CBC加密模式需要一个随机数作为IV参数,这样同一份密文每次加密的结果都不同。相对于ECB模式安全性较高。
public static void main(String[] args) throws Exception {
// 原文:
String message = "123";
System.out.println("Message(原始信息): " + message);
// 256位密钥 = 32 bytes Key:
byte[] key = "1234567890abcdef1234567890abcdef".getBytes();
// 加密:
byte[] data = message.getBytes();
byte[] encrypted = encrypt(key,data);
System.out.println("Encrypted(加密内容): " +
Base64.getEncoder().encodeToString(encrypted));
// 解密:
byte[] decrypted = decrypt(key,encrypted);
System.out.println("Decrypted(解密内容): " + new String(decrypted));
}
// 加密:
public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 设置算法/工作模式CBC/填充
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 恢复秘钥对象
SecretKey secretKey =new SecretKeySpec(key,"AES");
// CBC模式需要生成一个16 bytes的initialization vector:
SecureRandom sr = SecureRandom.getInstanceStrong();
byte[] iv = sr.generateSeed(16);
System.out.println("iv字节数组内容:"+ Arrays.toString(iv));
System.out.println("iv字节数组长度:"+iv.length);
//随机数封装成IvParameterSpec参数对象
IvParameterSpec ivps =new IvParameterSpec(iv);
// 初始化秘钥:操作模式、秘钥、IV参数
cipher.init(Cipher.ENCRYPT_MODE,secretKey,ivps);
// 加密
byte[] bytes = cipher.doFinal(input);
// IV不需要保密,把IV和密文一起返回:
return join(iv,bytes);
}
// 解密:
public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 把input分割成IV和密文:
byte[] iv = new byte[16];
byte[] data = new byte[input.length - 16];
System.arraycopy(input, 0, iv, 0, 16); // IV
System.arraycopy(input, 16, data, 0, data.length); //密文
System.out.println(Arrays.toString(iv));
// 解密:
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // 密码对象
SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 恢复秘钥
IvParameterSpec ivps = new IvParameterSpec(iv); // 恢复IV
// 初始化秘钥:操作模式、秘钥、IV参数
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivps);
// 解密操作
return cipher.doFinal(data);
}
// 合并数组
public static byte[] join(byte[] bs1, byte[] bs2) {
byte[] r = new byte[bs1.length+bs2.length];
System.arraycopy(bs1,0,r,0,bs1.length);
System.arraycopy(bs2,0,r,bs1.length,bs2.length);
return r;
}
在CBC模式下,需要一个随机生成的16字节IV参数,必须使用SecureRandom生成。因此多了一个IvParameterSpec实例,因此初始化方法需要调用Cipher的一个重载方法并传入IvParameterSpec。