-
概述:
对称加密算法时使用相同的密钥用于加密数据和解密数据。发送方使用密钥对明文进行加密生成密文,接收方使用密钥进行解密还原原始的明文。
-
特点:
对称加密算法因为使用同一个密钥,所以它的实现相对简单、执行速度快;只需要一个密钥和加密解密算法。但是他也存在一些安全隐患,发送方和接受方定要共享密钥,所以密钥的安全分发和和管理都是一个有挑战的问题。
-
AES算法:
AES算法是目前应用最广泛的加密算法,有两种工作模式:
CBC模式的安全性高于ECB,因为CBC模式通过引入块之间的依赖性,使用误差传播等机制增加了攻击者破解密文的难度。ECB模式由于每个明文块之间独特加密的特点,安全性较低。
1、 ECB
将明文分成固定大小的块,然后对每个块进行加密。
代码案例如下:
public class main{
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);
}
//合并数组
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;
}
}
2、 CBC
引入了块之间的依赖关系和反馈机制,每个明文块在加密之前都会与前一个密文块进行异或运算,然后再进行加密。
代码案例如下:
public class main {
public static void main(String[] args) throws GeneralSecurityException {
// 原文:
String message = "天生我材必有用飞流直下三千尺";
System.out.println("Message(原始信息): " + message);
// 128位密钥 = 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 {
// 创建密码对象,需要传入算法/工作模式/填充模式
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 根据key的字节内容,"恢复"秘钥对象
SecretKey keySpec = new SecretKeySpec(key,"AES");
//CBC模式需要生成一个16字节的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);
// 初始化秘钥:设置加密模式ENCRYPT_,密钥,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);//密文
System.out.println(Arrays.toString(iv));
// 创建密码对象,需要传入算法/工作模式/填充模式
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 根据key的字节内容,"恢复"秘钥对象
SecretKey keySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivps = new IvParameterSpec(iv);
// 初始化秘钥:设置解密模式DECRYPT_MODE
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。