加密算法AES
一、实验目的
通过使用Java标准库的密码学算法调用AES对称加密算法,能够编写简单的实验代码进行正确的AES加密和解密。
二、实验要求
1. 熟悉AES对称加密算法;
2. 掌握AES对称加密算法的简单代码实验。
三、开发环境
JDK1.6,Java相关开发环境(本实验以Windows平台为例)
四、实验内容
【1-1】AES对称加密实验
4、 给定一个String类型的参数,使用如下的函数进行密钥初始化(这里使用无初始化向量IV的AES-128加密模式):
private static KeyGenerator getKeyGenerator(String key) throws NoSuchAlgorithmException{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
keygen.init(128, secureRandom);
return keygen;
}
5、 给定String类型的密钥和待加密消息,使用如下的函数进行加密:
public static String encryptData(String key, String message) throws Exception{
KeyGenerator keygen = getKeyGenerator(key);
SecretKey secretKey = new SecretKeySpec(keygen.generateKey().getEncoded(),"AES");
return Base64.getEncoder().encodeToString(encrypt(secretKey, message.getBytes("UTF-8")));
}
6、 使用如下的函数进行实际的算法初始化和加密操作:
private static byte[] encrypt(Key key, byte[] messBytes) {
if(key != null) {
try {
//生成一个Cipher对象基于AES加密算法进行加密
Cipher cipher = Cipher.getInstance("AES");
//对Cipher对象初始化
cipher.init(Cipher.ENCRYPT_MODE, key);
//加密消息并返回密文
return cipher.doFinal(messBytes);
}catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
7、 给定String类型的密钥和待解密的密文,使用如下的函数进行解密:
public static String decryptData(String key, String ciphertext) throws Exception{
KeyGenerator keygen = getKeyGenerator(key);
SecretKey secretKey = new SecretKeySpec(keygen.generateKey().getEncoded(),"AES");
return new String(decrypt(secretKey, Base64.getDecoder().decode(ciphertext)),"UTF-8");
}
8、 使用如下的函数进行实际的算法初始化和解密操作:
private static byte[] decrypt(Key key, byte[] cipherBytes) {
if(key != null) {
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(cipherBytes);
}catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
【1-4】参考代码
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtils {
private static KeyGenerator getKeyGenerator(String key) throws NoSuchAlgorithmException{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
keygen.init(128, secureRandom);
return keygen;
}
private static byte[] encrypt(Key key, byte[] messBytes) {
if(key != null) {
try {
//生成一个Cipher对象基于AES加密算法进行加密
Cipher cipher = Cipher.getInstance("AES");
//对Cipher对象初始化
cipher.init(Cipher.ENCRYPT_MODE, key);
//加密消息并返回密文
return cipher.doFinal(messBytes);
}catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
public static String encryptData(String key, String message) throws Exception{
KeyGenerator keygen = getKeyGenerator(key);
SecretKey secretKey = new SecretKeySpec(keygen.generateKey().getEncoded(),"AES");
return Base64.getEncoder().encodeToString(encrypt(secretKey, message.getBytes("UTF-8")));
}
private static byte[] decrypt(Key key, byte[] cipherBytes) {
if(key != null) {
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(cipherBytes);
}catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
public static String decryptData(String key, String ciphertext) throws Exception{
KeyGenerator keygen = getKeyGenerator(key);
SecretKey secretKey = new SecretKeySpec(keygen.generateKey().getEncoded(),"AES");
return new String(decrypt(secretKey, Base64.getDecoder().decode(ciphertext)),"UTF-8");
}
public static void main(String[] args) throws Exception{
String key = "testKey";
String message = "testMessage";
Scanner sc=new Scanner(System.in);
sc.useDelimiter("\n")
System.out.print("请输入密码回车结束: ");
if(sc.hasNext()) {
key=sc.next();
}
System.out.print("请输入需要加密的文字回车结束: ");
if(sc.hasNext()) {
message=sc.next();
}
String ciphertext = encryptData(key, message);
// System.out.println("密钥: "+ key + " 消息: " + message);
System.out.println("加密后密文为: " + ciphertext);
System.out.print("输入密钥按回车后可对密文数据进行解密:");
String key1="";
if(sc.hasNext())
{
key1=sc.next();
}
System.out.println("解密后明文为:" + decryptData(key1, ciphertext));
sc.close();
}
}
扩展参考资料
2、 关于AES的几种加密模式:https://www.cnblogs.com/liangxuehui/p/4651351.html
3、 关于在代码中设置AES的不同加密模式以及填充模式:https://www.cnblogs.com/gne-hwz/p/9553502.html、https://blog.csdn.net/u013871100/article/details/80100992、https://blog.csdn.net/qq_25816185/article/details/81626499、https://www.jianshu.com/p/0b739942cea5