package com.bas.util;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
public class AESUtil {
/**加密
* 因为Java默认不支持16位以上的密码,如果密码为16位以上,则会取中间的16位作为密码
* @param content 待加密内容
* @param password 加密密钥
* @return
*/
public static String encrypt(String input, String key){
byte[] crypted = null;
try{
if(key.length()>16){
key = disposeKey(key);
}
SecretKeySpec skey = new SecretKeySpec(key.getBytes("utf-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes("utf-8"));
}catch(Exception e){
e.printStackTrace();
}
return Hex.encodeHexString(crypted);
}
/**解密
* 因为Java默认不支持16位以上的密码,如果密码为16位以上,则会取中间的16位作为密码
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static String decrypt(Str
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
public class AESUtil {
/**加密
* 因为Java默认不支持16位以上的密码,如果密码为16位以上,则会取中间的16位作为密码
* @param content 待加密内容
* @param password 加密密钥
* @return
*/
public static String encrypt(String input, String key){
byte[] crypted = null;
try{
if(key.length()>16){
key = disposeKey(key);
}
SecretKeySpec skey = new SecretKeySpec(key.getBytes("utf-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes("utf-8"));
}catch(Exception e){
e.printStackTrace();
}
return Hex.encodeHexString(crypted);
}
/**解密
* 因为Java默认不支持16位以上的密码,如果密码为16位以上,则会取中间的16位作为密码
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static String decrypt(Str