package com.mtkj.common.bpqutils;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
public class AESUtils {
/**
* @Description:AES 加密
*/
public static byte[] encrypt(String content, String password) {
try {
SecretKey key = new SecretKeySpec(HexUtil.decodeHex(password), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding"); // 创建密码器
byte[] byteContent = parseHexStr2Byte(content);
cipher.init(Cipher.ENCRYPT_MODE, key); // 初始化
assert byteContent != null;
return cipher.doFinal(byteContent);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
/**
* @Description:AES 解密
*/
public static byte[] decrypt(byte[] content, String password) {
try {
SecretKeySpec key = new SecretKeySpec(parseHexStr2Byte(password), "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding");
// 初始化
cipher.init(Cipher.DECRYPT_MODE, key);
// 加密
return cipher.doFinal(content);
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchPaddingException e) {
e.printStackTrace();
}
return null;
}
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static void main (String [] args) throws NoSuchAlgorithmException, ParseException {
String content = "2075907589680002115F8252031809476EA5E4E1852373402920210709101300";
//Hutool
//加密
SecretKey secretKey = SecureUtil.generateKey("AES", HexUtil.decodeHex("451FFA7B7CA7047BB51BB5F62FFA658BC8EC065CAABEB92857F0238DB62826E5"));
AES aes = new AES(Mode.ECB, Padding.NoPadding, secretKey);
String encrypt1 = aes.encryptHex(HexUtil.decodeHex(content));
System.out.println("加密后:" + encrypt1);
byte[] decrypt2 = aes.decrypt(encrypt1);
System.out.println("解密后:" +HexUtil.encodeHexStr(decrypt2));
byte[] encryptResult = encrypt(content, "451FFA7B7CA7047BB51BB5F62FFA658BC8EC065CAABEB92857F0238DB62826E5");
String encryptResultStr1 = parseByte2HexStr(encryptResult);
System.out.println("加密后:" + encryptResultStr1);
byte[] decrypt = decrypt(encryptResult, "451FFA7B7CA7047BB51BB5F62FFA658BC8EC065CAABEB92857F0238DB62826E5");
System.out.println("解密后:" +parseByte2HexStr(decrypt));
}
}
AES加密工具
于 2022-04-25 12:32:42 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)