目录
1. AES算法
AES算法属于对称加密算法的一种,另外常用的对称加密算法还有DES和IDEA;以下是这三种对称加密算法使用的相关参数。
算法 | 秘钥长度 | 工作模式 | 填充模式 |
DES | 56/64 | ECB/CBC/PCBC/CTR/... | NoPadding/PKCS5Padding/... |
AES | 128/192/256 | ECB/CBC/PCBC/CTR/... | NoPadding/PKCS5Padding/PKCS7Padding/... |
IDEA | 128 | ECB | PKCS5Padding/PKCS7Padding/... |
秘钥长度直接决定加密强度,秘钥长度越长,加密越强。工作模式和填充模式是加密算法的格式和参数的原则。DES算法由于秘钥长度过短,已经不安全了。
AES算法属于对称加密算法,所以AES算法是可逆的,加密和解密用的是用一把秘钥,他是目前使用最广泛地加密算法。
2. AES工作模式
对于AES算法的工作模式,常见的有两种:ECB和CBC;
2.1 ECB模式
在使用AES算法的ECB模式时需要我们自己定义一个长度为128位的秘钥。
因为ECB模式相同的原文经过加密后会得到相同的密文,所以安全性较弱,这意味着如果攻击者获取到密文,就可以根据相同的密文块反推出原文,从而可能导致信息泄露问题。
// AES + 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();
//System.out.println(Arrays.toString(data));
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 secretKey = new SecretKeySpec(key, "AES");
//System.out.println(secretKey);
// 初始化秘钥:设置加密模式ENCRYPT_
cipher.init(cipher.ENCRYPT_MODE, secretKey);
// 根据原始内容(字节),进行加密
return cipher.doFinal(input);
}
// 解密:
public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 创建密码对象,需要传入算法/工作模式/填充模式
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 根据key的字节内容,"恢复"秘钥对象
SecretKey secretKey = new SecretKeySpec(key, "AES");
//System.out.println(secretKey);
// 初始化秘钥:设置解密模式DECRYPT_MODE
cipher.init(cipher.DECRYPT_MODE,secretKey);
// 根据原始内容(字节),进行解密
return cipher.doFinal(input);
}
}
如代码所示,我们对原文“天生我材必有用飞流直下三千尺”进行加密解密操作,在代码中使用16字节的“1234567890abcdef”秘钥,对原文进行加密。
首先我们定义了两个方法,分别是encrypt()方法用来加密,decrypt()方法用来解密,两个方法在调用时都需要传入秘钥及原文。在加密解密方法中都分别通过Cipher.getInstance("AES/ECB/PKCS5Padding")方法传入参数分别为算法/工作模式/填充模式,创建了各自的密码对象Cipher实例,接着根据算法名称及秘钥创建了SecretKey实例,然后初始化秘钥,通过init()方法分别设置加密和解密模式(加密模式为cipher.ENCRYPT_MODE,解密模式为cipher.DECRYPT_MODE)。最后通过doFinal()方法返回加密解密的内容。在主函数中我们分别通过调用这两个方法对原文内容进行加密解密操作。
运行结果:(加密内容采用了Base64编码)
Message(原始信息): 天生我材必有用飞流直下三千尺
Encrypted(加密内容): a8MJCOSonzcdWEZ6suh6kLbiHdf5K1SQxuwZHrUJNdM=
Decrypted(解密内容): 天生我材必有用飞流直下三千尺
2.2 CBC模式
与ECB模式不同的是CBC需要一个随机数作为IV参数,因为IV参数是随机的所以,每次对相同的明文加密后的密文都不同。
//AES + CBC
public class Work02 {
public static void main(String[] args) throws Exception {
// 原文:
String message = "wbjxxmy";
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 = new SecureRandom();
byte[] iv = sr.generateSeed(16);
IvParameterSpec ivps = new IvParameterSpec(iv);
System.out.println(ivps.toString());
// 初始化秘钥:操作模式、秘钥、IV参数
cipher.init(cipher.ENCRYPT_MODE,secretKey , ivps);
// 加密
byte[] buff = cipher.doFinal(input);
// IV不需要保密,把IV和密文一起返回:
return join(iv, buff);
}
// 解密:
public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 把input分割成IV和密文:
byte[] iv = new byte[16];
byte[] b = new byte[input.length-16];
System.arraycopy(input, 0,iv,0,16);
System.arraycopy(input, 16, b, 0, b.length);
// 解密:
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey secretKey = new SecretKeySpec(key,"AES");
IvParameterSpec ivps = new IvParameterSpec(iv);
System.out.println(ivps.toString());
// 初始化秘钥:操作模式、秘钥、IV参数
cipher.init(Cipher.DECRYPT_MODE,secretKey , ivps);
// 解密操作
return cipher.doFinal(b);
}
// 合并数组
public static byte[] join(byte[] bs1, byte[] bs2) {
byte[] buf = new byte[bs1.length+bs2.length];
System.arraycopy(bs1, 0, buf, 0, bs1.length);
System.arraycopy(bs2, 0, buf, bs1.length, bs2.length);
return buf;
}
}
加密和解密与ECB模式的不同之处在于,CBC需要用SecureRandom 对象的generateSeed()方法产生一个iv参数(类型为byte[]),在init()方法中需要多传入一个参数即为iv参数,但类型必须是IvParameterSpec 类型,所以需要用将byte[]转换成IvParameterSpec 类型,最后将iv参数与密文合并之后的数组返回。在解密时需要将数组中的iv参数提取出来,与秘钥结合在一起对密文进行解密。
运行结果:
Message(原始信息): wbjxxmy
加密时iv参数:[12, -92, -14, 59, -96, -120, -70, -47, -103, -59, -49, 62, 12, -100, 118, -76]
Encrypted(加密内容): DKTyO6CIutGZxc8+DJx2tI1oz9w4AWmCRryUsbh5k4I=
解密时iv参数:[12, -92, -14, 59, -96, -120, -70, -47, -103, -59, -49, 62, 12, -100, 118, -76]
Decrypted(解密内容): wbjxxmy
2.3 ECB与CBC的区别
CBC模式加密相比于ECB模式较为安全,CBC模式需要一个随机参数(iv参数),每次生成的密文不同,而ECB模式使用相同的秘钥加密结果相同,所以安全性较弱。