什么是对称加密算法
对称加密算法就是传统的用一个秘钥进行加密和解密。对称加密算法都有一个共同的特点:加密和解密所用的密钥是相同的。例如,我们常用的WinZIP和WinRAR对压缩包的加密和解密,就是使用对称加密算法。
从程序的角度看:
所谓加密,就是这样一个函数,它接收密码和明文,然后输出密文:secret = encrypt(key, message);
而解密则相反,它接收密码和密文,然后输出明文: plain = decrypt(key, secret);
常见的对称加密算法
DES(Data Encryption Standard):密钥长度:54/64位,工作模式:ECB/CBC/PCBC/CTR/…;
AES(Advanced Encryption Standard):密钥长度:128/192/256位,工作模式:ECB/CBC/PCBC/CTR/…;
IDEA(International Data Encryption Algorithm):密钥长度:128位,工作模式:ECB。
密钥长度直接决定加密强度,DES算法由于密钥过短,可以在短时间内被暴力破解,所以现在已经不安全了。而AES算法是目前应用最广泛的加密算法。比较常见的工作模式是ECB和CBC。
使用AES加密
1.ECB模式
ECB模式是最简单的AES加密模式,它需要一个固定长度的密钥,固定的明文会生成固定的密文。 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();
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模式
ECB模式是最简单的AES加密模式,这种一对一的加密方式会导致安全性降低,所以,更好的方式是通过CBC模式,它需要一个随机数作为IV参数,这样对于同一份明文,每次生的密文都不相同:
//AES + CBC
public class Work02 {
public static void main(String[] args) throws Exception {
// 原文:
String message = "daxigua2024";
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 = new SecureRandom();
byte[] iv = sr.generateSeed(16);
IvParameterSpec ivPar =new IvParameterSpec(iv);
// 初始化秘钥:操作模式、秘钥、IV参数
cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivPar);
// 加密
byte[] ret =cipher.doFinal(input);
// IV不需要保密,把IV和密文一起返回:
return join(iv,ret);
}
// 解密:
public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
// 把input分割成IV和密文:
byte[] iv = new byte[16];
byte[] ret = new byte[input.length-16];
System.arraycopy(input, 0, iv, 0, 16);
System.arraycopy(input, 16, ret, 0, ret.length);
// 解密:
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey keySpec = new SecretKeySpec(key,"AES");
IvParameterSpec ivPar = new IvParameterSpec(iv);
// 初始化秘钥:操作模式、秘钥、IV参数
cipher.init(Cipher.DECRYPT_MODE, keySpec,ivPar);
// 解密操作
return cipher.doFinal(ret);
}
// 合并数组
public static byte[] join(byte[] bs1, byte[] bs2) {
byte[] ret = new byte[bs1.length+bs2.length];
System.arraycopy(bs1, 0, ret, 0, bs1.length);
System.arraycopy(bs2, 0, ret, bs1.length, bs2.length);
return ret;
}
}
运行结果:
使用AES算法的注意事项
在我们使用AES算法进行加密时,不管是基于ECB工作模式还是基于CBC工作模式,都要使用密钥用来加密或者解密,在AES算法中,密钥的长度是固定的,不能多,也不能少,具体密钥的长度根据不同的工作模式而不同。
总结
Java标准库提供的对称加密接口非常简单,使用时按以下步骤编写代码:
- 根据算法名称/工作模式/填充模式获取Cipher实例;
- 根据算法名称初始化一个SecretKey实例,密钥必须是指定长度;
- 使用SerectKey初始化Cipher实例,并设置加密或解密模式;
- 传入明文或密文,获得密文或明文。