代码语言:javascript复制
private static final String ENCODING = "UTF-8";
private static final String KEY_ALGORITHM = "AES";
/**
* 加解密算法/工作模式/填充方式
*/
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
/**
* 填充向量
*/
private static final String FILL_VECTOR = "5e8y6w45ju8w9jq8";
/**
* KEY
*/
private static final String KEY="f499f517f8243226";
/**
* 加密字符串
*
* @param content 字符串
* @return
* @throws Exception
*/
public static String encrypt(String content) {
if (StringUtils.isAnyEmpty(content)) {
log.error("AES encryption params is null");
return null;
}
String password = byte2hex(KEY.getBytes());
byte[] raw = hex2byte(password);
SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
Cipher cipher = null;
try {
cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(FILL_VECTOR.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] anslBytes = content.getBytes(ENCODING);
byte[] encrypted = cipher.doFinal(anslBytes);
return byte2hex(encrypted).toUpperCase();
} catch (Exception e) {
log.error("AES encryption operation has exception,content:{},password:{}", content, password, e);
}
return null;
}
/**
* 解密
*
* @param content 解密前的字符
* @return
* @throws Exception
* @author cdduqiang
* @date 2014年4月3日
*/
public static String decrypt(String content) {
if (StringUtils.isAnyEmpty(content)) {
log.error("AES decryption params is null");
return null;
}
String password = byte2hex(KEY.getBytes());
try {
byte[] raw = hex2byte(password);
SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(FILL_VECTOR.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = hex2byte(content);
byte[] original = cipher.doFinal(encrypted1);
return new String(original, ENCODING);
} catch (Exception e) {
log.error("AES decryption operation has exception,content:{},password:{}", content, password, e);
}
return null;
}
public static byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i ) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 2), 16);
}
return b;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n ) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs "0" stmp;
} else {
hs = hs stmp;
}
}
return hs.toUpperCase();
}
/**
* 解密秘钥
* @param encryptionKey 秘钥
* @return
*/
public static String getKey(String encryptionKey){
byte[] b=hex2byte(encryptionKey);
String key= new String(b);
if(KEY.equals(key)){
return "1";
}
return null;
}