自定义AES加解密算法(采用对称加密算法)
Base64格式使用RFC4648进行编码和解码
-
自定义加解密KEY
Key:来源:使用秘钥生成器生成一个随机秘钥,然后使用base64的Encoder进行编码
在使用的时候为固定值,即为私钥,不可丢失,不可泄露
-
开始加密:
a) 解码key:使用base64进行解码获取字节数组,获取原始秘钥
b) 获取需要进行加密的数据的字节数组
c) AES加密,获取构造的秘钥,和需要进行加密的字节数组
d) AES加密使用默认值,ECB,此时没有偏移量(当使用CBC时,存在偏移量)
e) 得到字节数组,使用base64进行编码
-
解密:
a) 解码key:使用base64进行解码获取字节数组,获取原始秘钥
b) 对需要进行解密的数据进行base64解码
c) 使用AES进行解码,得到字节数组
d) 直接将字节数组转换为字符串
创建AESUtil.java
package com.decode;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
/**
* 生成AES秘钥,然后Base64编码
* @return Base64编码
* @throws Exception
*/
public static String genKeyAES() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();
return Base64.getEncoder().encodeToString(key.getEncoded());
}
/**
* 将Base64编码后的AES秘钥转换成SecretKey对象
* @param base64Key
* @return SecretKey对象
* @throws Exception
*/
public static SecretKey loadKeyAES(String base64Key) throws Exception {
byte[] bytes = Base64.getDecoder().decode(base64Key);
return new SecretKeySpec(bytes, "AES");
}
/**
* AES加密
* @param source 加密内容
* @param key SecretKey对象
* @return 加密后的字节数组
* @throws Exception
*/
public static byte[] encryptAES(byte[] source, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(source);
}
/**
* AES解密
* @param source 解密内容
* @param key SecretKey对象
* @return 解密后的字节数组
* @throws Exception
*/
public static byte[] decryptAES(byte[] source, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(source);
}
}
创建EncryptUtil.java
package com.decode;
import java.util.Base64;
import javax.crypto.SecretKey;
/**
* AES加密解密算法
* @author xxyf55
*
*/
public class EncryptUtil {
//加密解密秘钥KEY
public static final String aesKey = "a3z4v/RxfgHTRZtlHlrw+5Q==";
public static void main(String[] args) {
try {
long start = System.currentTimeMillis();
String context = "123123";
String aesData = appEncrypt(context);
System.out.println(aesData);
context = appDecrypt(aesData);
System.out.println(context);
long end = System.currentTimeMillis();
System.out.println(end - start);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密内容
* @param content 需要加密的内容
* @return 返回String
* @throws Exception
*/
public static String appEncrypt(String content) throws Exception {
SecretKey secretKey = AESUtil.loadKeyAES(aesKey);
// 用AES秘钥加密请求内容
byte[] encryptContentRequest = AESUtil.encryptAES(content.getBytes("utf-8"), secretKey);
String data = Base64.getEncoder().encodeToString(encryptContentRequest);
return data;
}
/**
* 解密内容
* @param content 内容
* @return
* @throws Exception
*/
public static String appDecrypt(String content) throws Exception {
// 用AES秘钥解密请求内容
SecretKey secretKey = AESUtil.loadKeyAES(aesKey);
byte[] response = AESUtil.decryptAES(Base64.getDecoder().decode(content), secretKey);
return new String(response);
}
}
运行main方法即可获得加密之后的数据,进行解密之后得到原始的数据