AES 高级加密标准(英语:Advanced Encryption Standard,缩写:AES)
AES加密是现在在对称加密中比较常用的一种加密算法。AES加密采用对称密钥体制,加密密钥与解密密钥是相同,密钥长度支持128、192、256。AES在安全方面DES更安全、灵活、高效率;
代码:
/**
*
* 说明 :AES加密
*
* @param stringToEncode
* 明文
* @param keyString
* 密钥
* @return Bses64格式密文
*/
public static String encrypt(String stringToEncode, String keyString)
throws NullPointerException {
if (keyString == null) {
return null;
}
if (stringToEncode == null) {
return null;
}
try {
SecretKeySpec skeySpec = getKey(keyString);
byte[] clearText = stringToEncode.getBytes("UTF8");
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
/*
* AES:加密算法; CBC:工作模式 ,AES有5中加密模式(CBC、ECB、CTR、OCF、CFB);
* PKCS7Padding:填充模式;
*/
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
String encrypedValue = Base64.encodeToString(
cipher.doFinal(clearText), Base64.DEFAULT);
return encrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
}
/**
*
* 说明 :AES解密
*
* @param text
* Base64格式密文
* @param password
* 密钥
* @return String格式明文
*/
public static String decrypt(String text, String password)
throws NullPointerException {
if (password == null) {
return null;
}
if (text == null) {
return null;
}
try {
SecretKey key = getKey(password);
// IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
byte[] encrypedPwdBytes = Base64.decode(text, Base64.DEFAULT);
// cipher is not thread safe
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
String decrypedValue = new String(decrypedValueBytes);
return decrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
}
/**
*
* 说明 :随机生成一组AES密钥
*
* @return 16进制AES密钥
*/
public static String getRawKey() {
KeyGenerator kgen = null;
SecureRandom sr = null;
try {
kgen = KeyGenerator.getInstance("AES");
// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
sr = SecureRandom.getInstance("SHA1PRNG");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
kgen.init(128, sr); // 256 bits or 128 bits,192bits
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
String result = bytes2Hex(raw);
return result;
}
/**
*
* 说明 :将密钥转行成SecretKeySpec格式
*
* @param password
* 16进制密钥
* @return SecretKeySpec格式密钥
*/
private static SecretKeySpec getKey(String password)
throws UnsupportedEncodingException {
// 如果为256将长度改为256即可
int keyLength = 128;
byte[] keyBytes = new byte[keyLength / 8];
Arrays.fill(keyBytes, (byte) 0x0);
byte[] passwordBytes = toByte(password);
int length = passwordBytes.length < keyBytes.length ? passwordBytes.length
: keyBytes.length;
System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
return key;
}
/**
* 将16进制转换为byte数组
*
* @param hexString
* 16进制字符串
* @return byte数组
*/
private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}
/**
* byte数组转换为16进制字符串
*
* @param bts
* 数据源
* @return 16进制字符串
*/
private static String bytes2Hex(byte[] bts) {
String des = "";
String tmp = null;
for (int i = 0; i < bts.length; i++) {
tmp = (Integer.toHexString(bts[i] & 0xFF));
if (tmp.length() == 1) {
des += "0";
}
des += tmp;
}
return des;
}