AES算法介绍:
AES算法是一种广泛使用的对称加密算法,支持多种工作模式。
对称加密算法的特点:加密和解密需要的密钥是相同的。密钥的长度可以是128,192或256位。
对称加密的过程:用函数接受密码和明文,然后输出密文。
对称加密的解密过程:用函数接受密码和密文,然后输出明文。
AES的常用工作模式有:ECB、CBC、PCBC、CTR....
ECB模式和CBC模式
AES比较常用的工作模式是ECB模式和CBC模式,CBC模式由于会生成一个随机参数作为盐,这样使每次加密结果都不同,使安全性得到了提高。
ECB模式:
这种模式需要一个固定长度的密文,如果密文被获取,则加密的结果就不安全了。
public class Work01 {
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);
}
}
CBC模式:
CBC模式相比较ECB模式,他会生成一个随机参数,这个参数和加密的密文合并后一起被返回,然后解密时被手动分开,获得密文和参数。大大提高了加密的安全性。
public class Work02 {
public static void main(String[] args) throws Exception {
// 原文:
String message = "Hello World!";
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 keySpec=new SecretKeySpec(key,"AES");
// CBC模式需要生成一个16 bytes的initialization vector:
SecureRandom sr=SecureRandom.getInstanceStrong();
byte[] iv=sr.generateSeed(16);//生成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, keySpec,ivps);
// 加密
byte[] data=cipher.doFinal(input);
// IV不需要保密,把IV和密文一起返回:
return join(iv,data);
}
// 解密:
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); //密文
// 解密:
//密码对象
Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
//恢复密钥
SecretKey keySpec=new SecretKeySpec(key,"AES");
//恢复IV
IvParameterSpec ivps=new IvParameterSpec(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;
}
}
ECB模式和CBC模式的区别:
ECB模式简单,处理速度快,但由于使用的密钥简单,所以安全性弱。
CBC模式是基于ECB模式,通过加入了随机数iv,提高了安全性,但是处理的速度会慢一些。