参考:
AES简介
高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥,具体的加密流程如下图:
工具类
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class EncrypAES {
/** 默认密钥,实际项目中可配置注入或数据库读取 */
private static String defaultKey = "helloworld";
/** 加密工具 */
private Cipher encryptCipher = null;
/** 解密工具 */
private Cipher decryptCipher = null;
private static EncrypAES instance = new EncrypAES(defaultKey);
public static EncrypAES getInstance() {
return instance;
}
public EncrypAES(String keyvalue) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
if (keyvalue == null)
keyvalue = defaultKey;
byte[] arrBTmp = null;
try {
arrBTmp = keyvalue.getBytes("utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 创建一个空的length位字节数组(默认值为0)
byte[] arrB = new byte[16];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "AES");
// 生成Cipher对象,指定其支持的DES算法
try {
encryptCipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("AES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
/**
* 对字符串加密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public String encrytor(String str) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
byte[] src = null;
try {
src = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parseByte2HexStr(encryptCipher.doFinal(src));
}
/**
* 对字符串解密
*
* @param buff
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public String decryptor(String buff) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
return new String(decryptCipher.doFinal(parseHexStr2Byte(buff)));
}
/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws Exception {
EncrypAES de1 = new EncrypAES("woshichuanqi");
String msg = "9A5C1E41066458D50F91636A111FED89.1560843070936";
String demsg = null;
System.out.println("明文是:" + msg);
long start = System.currentTimeMillis();
demsg = de1.encrytor(msg);
long end = System.currentTimeMillis();
System.out.println("加密后:" + demsg);
System.out.println("加密耗时:" + (end - start));
start = System.currentTimeMillis();
String value = de1.decryptor(demsg);
end = System.currentTimeMillis();
System.out.println("解密后:" + value);
System.out.println("解密耗时:" + (end - start));
}
public 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 String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
}
//明文是:9A5C1E41066458D50F91636A111FED89.1560843070936
//加密后:5E4D047CC156FABE4BC6C1781440AF1FEC33B8D0876EF36318BD94A4F83B874D34DC98F54F9EAB3AB9A107747D83932D
//加密耗时:1
//解密后:9A5C1E41066458D50F91636A111FED89.1560843070936
//解密耗时:0