package com.ue.call.api.biz;
import org.apache.commons.codec.digest.DigestUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class EncryptUtil {
private static final String CHARSET = "utf-8";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
private static final String ALGORITHM = "AES";
public static void main(String[] args) {
String accountId = "1090";
String customerNumber = "10086";
String customerNUmberMix = encrypt(accountId, customerNumber);
System.out.println("加密后的号码:"+ customerNUmberMix);
System.out.println("解密后的号码:"+ decrypt(accountId, customerNUmberMix));
System.out.println("校验结果==================>"+ customerNumber.equals(decrypt(accountId, customerNUmberMix)));
}
public static String encrypt(String accountId, String customerNumber){
return AESEncrypt(customerNumber, get16MD5(accountId));
}
public static String decrypt(String accountId, String customerNumber){
try {
return AESDecrypt(customerNumber, get16MD5(accountId));
} catch (Exception e) {
System.out.println("解密失败");;
}
return null;
}
public static String get16MD5(String input) {
try {
String hexString = DigestUtils.md5Hex(input);
// 截取第8到24位(共16位)
return hexString.substring(8, 24);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String AESEncrypt(String sSrc, String sKey) {
try {
if (sKey == null) {
System.out.print("Key为空null");
return null;
} else if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
} else {
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
String result = hexStringToByte(encrypted);
return result;
}
} catch (Exception var7) {
var7.printStackTrace();
return null;
}
}
public static String AESDecrypt(String sSrc, String sKey) throws Exception {
try {
if (sKey == null) {
System.out.print("Key为空null");
return null;
} else if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
} else {
byte[] raw = hexStringToByte(sSrc);
SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, skeySpec);
byte[] original = cipher.doFinal(raw);
return new String(original);
}
} catch (Exception var6) {
System.out.println(var6.toString());
return null;
}
}
public static String hexStringToByte(byte[] encrypted) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < encrypted.length; ++i) {
String s = Integer.toHexString(encrypted[i] & 255);
if (s.length() < 2) {
str.append(0);
}
str.append(s);
}
return str.toString();
}
public static byte[] hexStringToByte(String hex) {
int len = hex.length() / 2;
byte[] result = new byte[len];
String up = hex.toUpperCase();
char[] achar = up.toCharArray();
for(int i = 0; i < len; ++i) {
int pos = i * 2;
result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
String hexString = "0123456789ABCDEF";
byte b = (byte)hexString.indexOf(c);
return b;
}
}
AES加密示例
2025-11-14