AES(Advanced Encryption Standard,高级加密标准)是一种对称加密算法,广泛应用于数据加密和安全通信中。Java提供了丰富的安全库,使得在Java中实现AES加密变得相对简单。本文将详细介绍如何在Java中实现AES加密和解密,并提供完整的示例代码。

什么是AES?
AES是一种对称加密算法,由NIST(美国国家标准技术研究所)在2001年选定为高级加密标准,用于替代之前的DES算法。AES算法的优点包括安全性高、效率高、易于实现、不需要专利授权等。AES支持128位、192位和256位的密钥长度,分别对应AES-128、AES-192和AES-256。
AES的工作原理
AES算法通过一系列复杂的数学运算(如字节代换、行移位、列混合和轮密钥加等)对数据进行加密和解密。具体步骤如下:
-
字节代换:使用S盒对每个字节进行替换。
-
行移位:对每个分组的行进行循环左移。
-
列混合:对每个分组的列进行矩阵乘法。
-
轮密钥加:将每个分组与当前轮密钥进行按位异或运算。
Java中的AES实现
Java提供了javax.crypto包,其中包含了一些用于加密和解密的类和接口。下面我们将详细介绍如何在Java中实现AES加密和解密。
导入必要的包
首先,我们需要导入一些必要的包:
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; import java.util.Base64;
生成密钥
AES需要一个固定长度的密钥。我们可以使用KeyGenerator类来生成密钥:
public static SecretKey generateKey(int keySize) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(keySize, new SecureRandom());
return keyGen.generateKey();
}
生成初始化向量(IV)
为了提高安全性,AES通常使用CBC(Cipher Block Chaining)模式,这需要一个初始化向量(IV)。我们可以生成一个随机的IV:
public static IvParameterSpec generateIv() {
byte[] iv = new byte[16]; // AES block size is 16 bytes
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
}
加密方法
接下来,我们编写一个方法来实现AES加密:
public static String encrypt(String plainText, SecretKey key, IvParameterSpec iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptedData = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
解密方法
同样,我们编写一个方法来实现AES解密:
public static String decrypt(String encryptedText, SecretKey key, IvParameterSpec iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decodedData = Base64.getDecoder().decode(encryptedText);
byte[] decryptedData = cipher.doFinal(decodedData);
return new String(decryptedData);
}
完整示例代码
下面是一个完整的示例代码,展示了如何生成密钥、初始化向量、加密和解密字符串:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class AesExample {
public static void main(String[] args) {
try {
// 生成密钥
SecretKey key = generateKey(256);
// 生成初始化向量
IvParameterSpec iv = generateIv();
// 待加密的明文
String plainText = "Hello, World!";
// 加密
String encryptedText = encrypt(plainText, key, iv);
System.out.println("Encrypted Text: " + encryptedText);
// 解密
String decryptedText = decrypt(encryptedText, key, iv);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SecretKey generateKey(int keySize) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(keySize, new SecureRandom());
return keyGen.generateKey();
}
public static IvParameterSpec generateIv() {
byte[] iv = new byte[16]; // AES block size is 16 bytes
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
}
public static String encrypt(String plainText, SecretKey key, IvParameterSpec iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptedData = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String encryptedText, SecretKey key, IvParameterSpec iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decodedData = Base64.getDecoder().decode(encryptedText);
byte[] decryptedData = cipher.doFinal(decodedData);
return new String(decryptedData);
}
}
代码详解
-
生成密钥:
-
generateKey方法使用KeyGenerator类生成指定长度的AES密钥。 -
生成初始化向量:
-
generateIv方法生成一个16字节的随机IV。 -
加密方法:
-
encrypt方法使用Cipher类的getInstance方法获取一个AES加密器实例。 -
使用
init方法初始化加密器为加密模式,并传入生成的密钥和IV。 -
使用
doFinal方法对明文进行加密,并将结果转换为Base64编码的字符串。 -
解密方法:
-
decrypt方法与encrypt方法类似,但初始化加密器为解密模式。 -
使用
doFinal方法对Base64解码后的数据进行解密,并将结果转换为字符串。
注意事项
-
密钥管理:密钥的安全存储和传输非常重要。建议使用安全的方式(如环境变量、配置文件加密等)来管理密钥。
-
异常处理:在实际应用中,应添加更多的异常处理逻辑,以应对各种可能的错误情况。
-
性能考虑:AES虽然安全性较高,但计算复杂度较大,可能会对性能产生一定影响。在高性能要求的应用中,可以考虑使用其他更高效的加密算法。
参考资料
结论
本文详细介绍了如何在Java中实现AES加密和解密,并提供了完整的示例代码。通过本文的学习,读者应该能够理解AES的工作原理,并能够在实际项目中应用这一加密技术。希望本文对大家有所帮助。

王子主页






















