AES算法是一种对称加密算法,可以使用不同的工作模式进行加密和解密。常见的工作模式包括CBC(Cipher Block Chaining)和ECB(Electronic Codebook)两种。
CBC模式
CBC模式是一种基于分组密码的工作模式。在CBC模式下,明文先被分组,每个分组通过AES算法进行加密之后再与前一个分组的密文进行异或操作,然后再进行下一个分组的加密。由于每个分组的加密都依赖于前一个分组的密文,因此CBC模式比ECB模式更加安全。但是,在加密的第一个分组时,需要提供一个初始化向量(IV),这个IV需要是随机的、不可预测的,并且每个分组都需要使用上一个分组的加密后的结果作为IV。因此,CBC模式需要保证IV的安全性,否则可能会遭受攻击。
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
//AES + CBC
public class Work02 {
public static void main(String[] args) throws Exception {
// 原文:
String message = "欧青辣少";
System.out.println("Message(原始信息): " + message);
// 256位密钥 = 32 bytes Key:
byte[] key = "asdfghjklaSDFGHJKLASDFGHJKL12345".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/ECB/PKCS5Padding");
// 恢复秘钥对象
SecretKey keyspec=new SecretKeySpec(key,"AES");
// CBC模式需要生成一个16 bytes的initialization vector:
SecureRandom sr=SecureRandom.getInstanceStrong();
byte[] iv=sr.generateSeed(16);
System.out.println(Arrays.toString(iv));
System.out.println(iv.length);
// 初始化秘钥:操作模式、秘钥、IV参数
IvParameterSpec ivps=new IvParameterSpec(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);
System.arraycopy(input, 16, data, 0, data.length);
System.out.println(Arrays.toString(iv));
// 解密:
Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKey keyspec=new SecretKeySpec(key,"AES");
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模式
ECB模式是一种最简单的工作模式。在ECB模式下,明文被分成固定长度的块,每个块都单独进行加密,然后再组合成密文。这种模式的主要问题是,由于每个块都被单独加密,因此如果明文中有相同的块,那么就会得到相同的密文。因此,ECB模式容易受到密码学攻击,不够安全。
import java.security.GeneralSecurityException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.omg.IOP.ENCODING_CDR_ENCAPS;
// 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模式易于实现,适用于加密非机密数据。在实际应用中,可以根据具体情况选择合适的加密模式。