static class AESUtils {
// 加密后是Hex格式
public static String encoding(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, datakey);
byte[] encryptData = cipher.doFinal(data);
String result = parseByte2HexStr(encryptData);
System.out.println("AES 加密后 " + result);
return result;
}
// Hex格式解密
public static String decoding(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] bytes = parseHexStr2Byte(data);
final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, datakey);
byte[] encryptData = cipher.doFinal(bytes);
String result = new String(encryptData);
System.out.println("AES 解密后 " + result);
return result;
}
/**
* 将二进制转换成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.toLowerCase());
}
return sb.toString();
}
/**
* 16 进制转 二进制
*
* @param buf
* @return
*/
public static byte[] parseHexStr2Byte(byte[] buf) {
String hexStr = new String(buf);
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;
}
// Base64格式
public static String encodingBase64(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InstantiationException, IllegalAccessException {
final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, datakey);
byte[] encryptData = cipher.doFinal(data);
String result = parseByte2Base64Str(encryptData);
System.out.println("AES 加密后 " + result);
return result;
}
// Base64格式解密
public static String decodingBase64(byte[] data, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException, InstantiationException, IllegalAccessException {
data = parseBase642Byte(data);
final Key datakey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, datakey);
byte[] encryptData = cipher.doFinal(data);
String result = new String(encryptData,"UTF-8").trim();
System.out.println("AES 解密后 " + result);
return result;
}
public static String parseByte2Base64Str(byte[] bytes) throws InstantiationException, IllegalAccessException {
String encode = BASE64Encoder.class.newInstance().encode(bytes);
return encode;
}
public static byte[] parseBase642Byte(byte[] base64) throws InstantiationException, IllegalAccessException, IOException {
BASE64Decoder base64Decoder = BASE64Decoder.class.newInstance();
byte[] bytes = base64Decoder.decodeBuffer(new String(base64));
return bytes;
}
}
AES加密工具
于 2022-12-01 14:14:10 首次发布