要求对数据进行AES加密
具体要求:
加密模式用: ECB(电码本模式)
填充用: pkcs7padding
密码长度: 128位
秘钥: 约定是CHINA-FUJIAN
AES在线加密地址: 在线AES加密解密 - 拉米工具
因为AES是对称加密,所以得约定具体的方式。 AES具体说明看 《AES详解》
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.util.UUID;
public class AESUtils {
private static final int KEY_BIT = 128;
private static final String TYPE = "AES";
private static final String DEFAULT_MODE = "ECB";
private static final String DEFAULT_PADDING = "PKCS7Padding";
// private static final String DEFAULT_PADDING = "ZeroBytePadding";
static
{
Security
.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
private static Key getKey(String key)
{
byte[] keys = key.getBytes();
byte[] keyBTmp = new byte[KEY_BIT / 8];
for (int i = 0; i < keys.length && i < keyBTmp.length; i++)
{
keyBTmp[i] = keys[i];
}
return new SecretKeySpec(keyBTmp, TYPE);
}
private static String getSecurityType(String mode, String padding)
{
return TYPE + "/" + mode +
"/" + padding;
}
public static String encode(String str, String key, String mode,
String padding) throws Exception
{
Key secretKey = getKey(key);
Cipher cipher;
try
{
cipher = Cipher.getInstance(getSecurityType(mode, padding));
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] b = cipher.doFinal(str.getBytes());
return byte2hex(b);
}
catch (Exception e)
{
throw new Exception("AES加密错误", e);
}
}
public static String decode(String str, String key, String mode,
String padding) throws Exception
{
Key secretKey = getKey(key);
Cipher cipher;
try
{
byte[] b = hex2byte(str);
cipher = Cipher.getInstance(getSecurityType(mode, padding));
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(b));
}
catch (Exception e)
{
throw new Exception("AES解密错误", e);
}
}
public static String encode(String str, String key)
throws Exception
{
return encode(str, key, DEFAULT_MODE, DEFAULT_PADDING);
}
public static String decode(String str, String key)
throws Exception
{
return decode(str, key, DEFAULT_MODE, DEFAULT_PADDING);
}
public static String decode(String str, String key, String mode,
String padding,String encodeType) throws Exception
{
Key secretKey = getKey(key);
Cipher cipher;
try
{
byte[] b = hex2byte(str,encodeType);
cipher = Cipher.getInstance(getSecurityType(mode, padding));
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(b));
}
catch (Exception e)
{
throw new Exception("AES解密错误", e);
}
}
public static String byte2hex(byte[] src)
{
String tmp;
int iLen = src.length;
StringBuilder str = new StringBuilder(iLen * 2);
for (byte aSrc : src) {
tmp = (Integer.toHexString(aSrc & 0XFF));
if (tmp.length() == 1) {
str.append("0");
str.append(tmp);
} else {
str.append(tmp);
}
}
return (str.toString()).toUpperCase();
}
public static byte[] hex2byte(String src)
{
byte[] bytes = src.getBytes();
int iLen = bytes.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2)
{
String strTmp = new String(bytes, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
private static byte[] hex2byte(String src, String encodeType) throws UnsupportedEncodingException
{
byte[] bytes = src.getBytes(encodeType);
int iLen = bytes.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2)
{
String strTmp = new String(bytes, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
public static void main(String[] args) throws Exception {
// 随机生成秘钥
String key = UUID.randomUUID().toString().replaceAll("-", "");
// 创建秘钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
// 初始化秘钥
keyGenerator.init(new SecureRandom(key.getBytes()));
// 生成秘钥
SecretKey secretKey = keyGenerator.generateKey();
System.out.println("秘钥: " + byte2hex(secretKey.getEncoded()));
String privateKey = "CHINA-FUJIAN";
String date = "2022-04-30";
String secretDate = AESUtils.encode(date, privateKey);
System.out.println("加密后: " + secretDate);
System.out.println("解密后: " + AESUtils.decode(secretDate, privateKey));
}
}
@Value("${aes.key:CHINA-FUJIAN}")
private String aesKey;
String decodeUserName = AESUtils.decode(userName, aesKey);
使用AES对称加密,需要约定具体的内容才能保持一致。