public class CryptionAESUtil{
// 申明加密标准
private final static String AES = "AES/CBC/PKCS5Padding";
private static final String INSTANCE_NAME = "SHA1PRNG";
private static final String ENCODE = "UTF-8";
// 该矢量必须要8字节,可由编程人员自己设置
private static byte[] EncryptionIV = "12345678abcdefgh".getBytes();
private static String pwd=PathUtil.getValue("pwd");
public CryptionAESUtil(){
try{
EncryptionIV =PathUtil.getValue("miyao").getBytes(ENCODE);
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密一个字节数组
*
* @param 原数组
* byte[]
* @throws Exception
* @return 加密后的数组 byte[]
*/
public byte[] EncryptionByteData(byte[] SourceData) throws Exception{
byte[] retByte = null;
try {
Cipher cipher = Cipher.getInstance(AES);
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
//防止linux下 随机生成key
SecureRandom secureRandom = SecureRandom.getInstance(INSTANCE_NAME);
secureRandom.setSeed(EncryptionIV);
keyGen.init(128, secureRandom);
SecretKey secretKey = keyGen.generateKey();
// byte[] encode_secretKey = parseHexStr2Byte(pwd);
// SecretKeySpec secretKey = new SecretKeySpec(encode_secretKey, "AES");
IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
cipher.init(Cipher.ENCRYPT_MODE, secretKey , iv);
retByte = cipher.doFinal(SourceData);
} catch (InvalidKeyException e) {
e.printStackTrace();
}catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return retByte;
}
/**
* 解密一个字节数组
*
* @param 原数据
* byte[]
* @throws Exception
* @return byte[]
*/
public byte[] DecryptionByteData(byte[] SourceData) throws Exception{
byte[] retByte = null;
try {
Cipher cipher = Cipher.getInstance(AES);
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance(INSTANCE_NAME,"SUN");
secureRandom.setSeed(EncryptionIV);
keyGen.init(128, secureRandom);
SecretKey sercetKey = keyGen.generateKey();
// byte[] encode_secretKey = parseHexStr2Byte(pwd);
// SecretKeySpec secretKey = new SecretKeySpec(encode_secretKey, "AES");
IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
cipher.init(Cipher.DECRYPT_MODE, secretKey ,iv);
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return retByte;
}
/**
* 加密一个字符串
*
* @param 原数据
* String
* @throws Exception
* @return 加密后的字符串 String
*/
public String encrypAES(String SourceData){
try{
String retStr = null;
byte[] retByte = null;
byte[] sorData = SourceData.getBytes(ENCODE);
retByte = EncryptionByteData(sorData);
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
/**
* 加密一个字符串
* @param SourceData 原数据
* @param charsetName 字符集
* @throws Exception
* @return 加密后的字符串 String
*/
public String encrypAES(String SourceData,String charsetName){
try{
String retStr = null;
byte[] retByte = null;
byte[] sorData = SourceData.getBytes(charsetName);
retByte = EncryptionByteData(sorData);
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
/**
* 字符串解密的方法
*
* @param 原机密后的字符串
* String
* @throws Exception
* @return 解密后的字符串 String
*/
public String decrypAES(String SourceData){
try{
String retStr = null;
byte[] retByte = null;
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData);
retByte = DecryptionByteData(sorData);
retStr = new String(retByte,ENCODE);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
/**
* 字符串解密的方法
*
* @param SourceData 原数据
* @param charsetName 字符集
* @throws Exception
* @return 解密后的字符串 String
*/
public String decrypAES(String SourceData, String charsetName){
try{
String retStr = null;
byte[] retByte = null;
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData);
retByte = DecryptionByteData(sorData);
retStr = new String(retByte,charsetName);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
// private static byte[] parseHexStr2Byte(String hexStr) {
// if (hexStr.length() < 1)
// return null;
// 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;
// }
}
// 申明加密标准
private final static String AES = "AES/CBC/PKCS5Padding";
private static final String INSTANCE_NAME = "SHA1PRNG";
private static final String ENCODE = "UTF-8";
// 该矢量必须要8字节,可由编程人员自己设置
private static byte[] EncryptionIV = "12345678abcdefgh".getBytes();
private static String pwd=PathUtil.getValue("pwd");
public CryptionAESUtil(){
try{
EncryptionIV =PathUtil.getValue("miyao").getBytes(ENCODE);
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密一个字节数组
*
* @param 原数组
* byte[]
* @throws Exception
* @return 加密后的数组 byte[]
*/
public byte[] EncryptionByteData(byte[] SourceData) throws Exception{
byte[] retByte = null;
try {
Cipher cipher = Cipher.getInstance(AES);
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
//防止linux下 随机生成key
SecureRandom secureRandom = SecureRandom.getInstance(INSTANCE_NAME);
secureRandom.setSeed(EncryptionIV);
keyGen.init(128, secureRandom);
SecretKey secretKey = keyGen.generateKey();
// byte[] encode_secretKey = parseHexStr2Byte(pwd);
// SecretKeySpec secretKey = new SecretKeySpec(encode_secretKey, "AES");
IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
cipher.init(Cipher.ENCRYPT_MODE, secretKey , iv);
retByte = cipher.doFinal(SourceData);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();} catch (InvalidKeyException e) {
e.printStackTrace();
}catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return retByte;
}
/**
* 解密一个字节数组
*
* @param 原数据
* byte[]
* @throws Exception
* @return byte[]
*/
public byte[] DecryptionByteData(byte[] SourceData) throws Exception{
byte[] retByte = null;
try {
Cipher cipher = Cipher.getInstance(AES);
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance(INSTANCE_NAME,"SUN");
secureRandom.setSeed(EncryptionIV);
keyGen.init(128, secureRandom);
SecretKey sercetKey = keyGen.generateKey();
// byte[] encode_secretKey = parseHexStr2Byte(pwd);
// SecretKeySpec secretKey = new SecretKeySpec(encode_secretKey, "AES");
IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
cipher.init(Cipher.DECRYPT_MODE, secretKey ,iv);
retByte = cipher.doFinal(SourceData);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return retByte;
}
/**
* 加密一个字符串
*
* @param 原数据
* String
* @throws Exception
* @return 加密后的字符串 String
*/
public String encrypAES(String SourceData){
try{
String retStr = null;
byte[] retByte = null;
byte[] sorData = SourceData.getBytes(ENCODE);
retByte = EncryptionByteData(sorData);
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
/**
* 加密一个字符串
* @param SourceData 原数据
* @param charsetName 字符集
* @throws Exception
* @return 加密后的字符串 String
*/
public String encrypAES(String SourceData,String charsetName){
try{
String retStr = null;
byte[] retByte = null;
byte[] sorData = SourceData.getBytes(charsetName);
retByte = EncryptionByteData(sorData);
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
/**
* 字符串解密的方法
*
* @param 原机密后的字符串
* String
* @throws Exception
* @return 解密后的字符串 String
*/
public String decrypAES(String SourceData){
try{
String retStr = null;
byte[] retByte = null;
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData);
retByte = DecryptionByteData(sorData);
retStr = new String(retByte,ENCODE);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
/**
* 字符串解密的方法
*
* @param SourceData 原数据
* @param charsetName 字符集
* @throws Exception
* @return 解密后的字符串 String
*/
public String decrypAES(String SourceData, String charsetName){
try{
String retStr = null;
byte[] retByte = null;
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData);
retByte = DecryptionByteData(sorData);
retStr = new String(retByte,charsetName);
return retStr;
}catch (Exception e) {
e.printStackTrace();
return "ERROR";
}
}
// private static byte[] parseHexStr2Byte(String hexStr) {
// if (hexStr.length() < 1)
// return null;
// 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;
// }
}