package com.vispractice.vpshop.util;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
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.SecretKeySpec;
public class CryptoUtil {
private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public static String aesEncrypt(String origin, String key) {
try {
SecretKey secretKey = getSecretKey(key);
SecretKeySpec keySpec = getSecretKeySpec(secretKey);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(1, keySpec);
byte[] result = cipher.doFinal(origin.getBytes("UTF-8"));
return byteArrayToHexString(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
private static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString().toUpperCase();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String aesDecrypt(String origin, String key) {
try {
SecretKey secretKey = getSecretKey(key);
SecretKeySpec keySpec = getSecretKeySpec(secretKey);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, keySpec);
byte[] result = cipher.doFinal(parseHexStr2Byte(origin));
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
return null;
}
private static SecretKeySpec getSecretKeySpec(SecretKey secretKey) {
byte[] keyEncoded = secretKey.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(keyEncoded, "AES");
return keySpec;
}
private static SecretKey getSecretKey(String secretKey)
throws NoSuchAlgorithmException {
KeyGenerator _generator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secretKey.getBytes());
_generator.init(128, secureRandom);
return _generator.generateKey();
}
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;
}
public static void main(String[] args) {
//
System.out.println(aesDecrypt("A76199CA8307801FD33FB811191FE137",
//
"vispractice"));
System.out.println(aesEncrypt("HNhuinong@888", "vispractice"));
}
}