packagexxx.common.util;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjavax.crypto.BadPaddingException;importjavax.crypto.Cipher;importjavax.crypto.IllegalBlockSizeException;importjavax.crypto.NoSuchPaddingException;importjavax.crypto.spec.SecretKeySpec;importjava.io.UnsupportedEncodingException;importjava.security.InvalidAlgorithmParameterException;importjava.security.InvalidKeyException;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjava.security.SecureRandom;importjava.util.Base64;/*** Created by windwant on 2016/12/13.*/
public classEncryptUtil {private static final Logger logger = LoggerFactory.getLogger(EncryptUtil.class);private static final String DEFAULT_CHARSET = "UTF-8";private static final String EMPTY_STR = "";private static final int AES_KEY_SIZE = 16;//256/192/128~32/24/16
public static voidmain(String[] args) {
String tempkey= "!@#$%^&*()_+";
String ming= "at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_77]";long begin =System.currentTimeMillis();for (int i = 0; i < 100000; i++) {
String en=tripleDesEncrypt(ming, tempkey);
String den=tripleDesDecrypt(en, tempkey);
}long end =System.currentTimeMillis();
System.out.println("TripleDES: " + (end -begin));
System.out.println("TripleDES: " + (end - begin)/100000.0);
begin=System.currentTimeMillis();for (int i = 0; i < 100000; i++) {
String en=desEncrypt(ming, tempkey);
String den=desDecrypt(en, tempkey);
}
end=System.currentTimeMillis();
System.out.println("DES: " + (end -begin));
System.out.println("DES: " + (end - begin)/100000.0);
begin=System.currentTimeMillis();for (int i = 0; i < 100000; i++) {
String en=aesEncrypt(ming, tempkey);
String den=aesDecrypt(en, tempkey);
}
end=System.currentTimeMillis();
System.out.println("AES: " + (end -begin));
System.out.println("AES: " + (end - begin)/100000.0);/*100000次
key: !@#$%^&*()_+
src: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_77]
TripleDES: 3831
TripleDES: 0.03831
DES: 845
DES: 0.00845
AES: 888
AES: 0.00888*/}private static final String ENCRYPT = "AES";private static final String CIPHER = "AES/CBC/PKCS5Padding";/*** AES加密
*@paramkey 加密密钥
*@paramsrc 加密内容
*@return返回BASE64密文*/
public static finalString aesEncrypt(String src, String key) {if(key == null || src == null){returnEMPTY_STR;
}try{byte[] bs =getAESResult(key, src.getBytes(DEFAULT_CHARSET), Cipher.ENCRYPT_MODE);if (bs != null) {returnBase64.getEncoder().encodeToString(bs);
}
}catch(Exception e) {
logger.error(e.getMessage());
}returnsrc;
}/*** AES解密
*@paramkey 解密密钥
*@paramsrc 解密内容
*@return明文*/
public static finalString aesDecrypt(String src, String key) {if(key == null || src == null){returnEMPTY_STR;
}try{byte[] bs =getAESResult(key, Base64.getDecoder().decode(src.getBytes(DEFAULT_CHARSET)), Cipher.DECRYPT_MODE);if (bs != null) {return newString(bs, DEFAULT_CHARSET);
}
}catch(Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}returnsrc;
}/*** AES加解密结果
*@paramkey 密钥
*@paramtextBytes 明文 密文 字节数组
*@paramencryptMode 加密 解密
*@return
*/
private static byte[] getAESResult(String key, byte[] textBytes, final intencryptMode)throwsNoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
Key newKey= newSecretKeySpec(buildCLenKey(key, AES_KEY_SIZE), ENCRYPT);
Cipher cipher=Cipher.getInstance(ENCRYPT);
cipher.init(encryptMode, newKey,newSecureRandom());returncipher.doFinal(textBytes);
}//定义加密算法,有DES、DESede(3DES)
private static final String ALGORITHM = "DESede";//算法名称/加密模式/填充方式
private static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding";/*** TripleDES加密方法
*@paramsrc
*@paramkey
*@returnBASE64*/
public static finalString tripleDesEncrypt(String src, String key) {if(key == null || src == null){returnEMPTY_STR;
}try{byte[] des =getTripleDESResult(key, src.getBytes(), Cipher.ENCRYPT_MODE);if(des != null) {returnBase64.getEncoder().encodeToString(des);
}
}catch(Exception e) {
logger.error(e.getMessage());
}returnsrc;
}/*** TripleDES解密函数
*@paramsrc 密文的字节数组
*@paramkey 密钥
*@returnString 明文*/
public static finalString tripleDesDecrypt(String src, String key) {if(key == null || src == null){returnEMPTY_STR;
}try{byte[] srcb =Base64.getDecoder().decode(src);byte[] des =getTripleDESResult(key, srcb, Cipher.DECRYPT_MODE);return newString(des, DEFAULT_CHARSET);
}catch(Exception e) {
logger.error(e.getMessage());
}returnsrc;
}/*** TripleDES加解密结果
*@paramkey 密钥
*@paramtextBytes 明文 密文 字节数组
*@paramencryptMode 加密 解密
*@return
*/
private static byte[] getTripleDESResult(String key, byte[] textBytes, final intencryptMode)throwsNoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
Key newKey= new SecretKeySpec(buildCLenKey(key, 24), ALGORITHM);
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM_ECB);
cipher.init(encryptMode, newKey,newSecureRandom());returncipher.doFinal(textBytes);
}/*** 根据字符串生成密钥字节数组
*@paramkeyStr 密钥字符串
*@paramlgn 密钥长度
*@return长度密钥字节数组
*@throwsUnsupportedEncodingException*/
private static byte[] buildCLenKey(String keyStr, int lgn) throwsUnsupportedEncodingException {byte[] key = new byte[lgn]; //声明一个24位的字节数组,默认里面都是0
byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组
///执行数组拷贝
if (key.length >temp.length) {//如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
System.arraycopy(temp, 0, key, 0, temp.length);
}else{//如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
System.arraycopy(temp, 0, key, 0, key.length);
}returnkey;
}private static final String DES_ALGORITHM = "DES";public static final String DES_CIPHER_ALGORITHM = "DES";/*** DES 加密方法
*@paramsrc
*@paramkey
*@returnBASE64*/
public static finalString desEncrypt(String src, String key) {if(key == null || src == null){returnEMPTY_STR;
}try{byte[] des =getDESResult(key, src.getBytes(), Cipher.ENCRYPT_MODE);if(des != null) {returnBase64.getEncoder().encodeToString(des);
}
}catch(Exception e) {
logger.error(e.getMessage());
}returnsrc;
}/*** DES解密函数
*@paramsrc 密文的字节数组
*@paramkey 密钥
*@returnString 明文*/
public static finalString desDecrypt(String src, String key) {if(key == null || src == null){returnEMPTY_STR;
}try{byte[] srcb =Base64.getDecoder().decode(src);byte[] des =getDESResult(key, srcb, Cipher.DECRYPT_MODE);return newString(des, DEFAULT_CHARSET);
}catch(Exception e) {
logger.error(e.getMessage());
}returnsrc;
}private static byte[] getDESResult(String key, byte[] textBytes, final intencryptMode)throwsNoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
Key newKey= new SecretKeySpec(buildCLenKey(key, 8), DES_ALGORITHM);
Cipher cipher=Cipher.getInstance(DES_CIPHER_ALGORITHM);
cipher.init(encryptMode, newKey,newSecureRandom());returncipher.doFinal(textBytes);
}
}