下面是“Java实现的AES加密算法完整实例”的完整攻略:
一、概述
AES(Advanced Encryption Standard)是一种常用的对称加密算法,之前常用的DES算法已经不再安全。在Java中,可以通过javax.crypto包中的AES算法实现加密和解密。
二、实现步骤
- 生成AES密钥
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom();
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
- 加密明文
byte[] byteContent = plainText.getBytes("utf-8");
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
- 解密密文
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(cipherText);
三、完整实例示例
示例1
public class AESEncryptionExample {
private static String plainText = "Hello, World! This is AES encryption example.";
public static void main(String[] args) throws Exception {
// 1. 生成AES密钥
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom();
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
// 2. 加密明文
byte[] byteContent = plainText.getBytes("utf-8");
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
System.out.println("加密结果:" + Hex.encodeHexString(result));
// 3. 解密密文
cipher = Cipher.getInstance("AES");
key = new SecretKeySpec(enCodeFormat, "AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedResult = cipher.doFinal(result);
System.out.println("解密结果:" + new String(decryptedResult));
}
}
输出结果:
加密结果:f5d6c59d02faf570a07d2aa68942231170d5b3066decf40ef81f23455fe2aa909c6801fd2aad00017c1e2d7494791465
解密结果:Hello, World! This is AES encryption example.
示例2
public class AESEncryptionExample {
private static String plainText = "这是一段中文明文";
public static void main(String[] args) throws Exception {
// 1. 生成AES密钥
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom();
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
// 2. 加密明文
byte[] byteContent = plainText.getBytes("utf-8");
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
System.out.println("加密结果:" + Hex.encodeHexString(result));
// 3. 解密密文
cipher = Cipher.getInstance("AES");
key = new SecretKeySpec(enCodeFormat, "AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedResult = cipher.doFinal(result);
System.out.println("解密结果:" + new String(decryptedResult));
}
}
输出结果:
加密结果:cec96c19c049e5d664d1d9c7664099a7c8265a16a4344f43f952c03878cc8f09
解密结果:这是一段中文明文
以上就是Java实现的AES加密算法完整实例的攻略,包括了生成密钥、加密和解密三个步骤,并且给出了两个示例,一个是英文明文,一个是中文明文。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现的AES加密算法完整实例 - Python技术站