1、密钥随机生成。
/@author wjh/
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
/**
* @ClassName: AESUtil
* @Description: 对cookie进行加密解密
* @author
* @date 2015-9-23 上午9:07:18
*
*/
public class AesUtils {
public static final String logalrithm = "AES/CBC/PKCS5Padding";
private static byte[] keyValue = new byte[] {
22,25,-35,-45,25,98,-55,-45,10,20,-45,25,
26,-95,25,-65,-11,-99,85,45,-62,10,-0,11,
-35,48,-98,65,-32,14,-78,25,36,-56,-45,-45,
12,15,-35,-75,15,-14,62,-25,33,-45,55,68,-88
};
private static byte[] iv = new byte[] {
-12,35,-25,65,45,-87,95,-22,-15,45,55,-66,32,5-4,84,55
};
private static SecretKey key;
private static AlgorithmParameterSpec paramSpec;
static{
KeyGenerator kgen;
try {
kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(keyValue));
key = kgen.generateKey();
paramSpec = new IvParameterSpec(iv);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* @Title: encrypt
* @Description: 加密,使用指定数据源生成密钥,使用用户数据作为算法参数进行AES加密
* @return String 返回类型
* @param msg 加密的数据
* @return
* @date 2015-9-23 上午9:09:20
* @throws
*/
public static