共计 3629 个字符,预计需要花费 10 分钟才能阅读完成。
package com.JUtils.encrypt;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* DES 加解密工具类
*
* @Author:chenssy
* @date:2016 年 5 月 20 日 下午 5:19:00
*
*/
class DESUtils {
/** 默认 key */
protected final static String KEY = "ScAKC0XhadTHT3Al0QIDAQAB";
/**
* DES 加密
*
* @author : chenssy
* @date : 2016 年 5 月 20 日 下午 5:51:37
*
* @param data
* 待加密字符串
* @param key
* 校验位
* @return
*/
@SuppressWarnings("restriction")
protected static String encrypt(String data,String key) {
String encryptedData = null;
try {
// DES 算法要求有一个可信赖的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(key.getBytes());
// 创立一个密匙工厂,而后用它把 DESKeySpec 转换成一个 SecretKey 对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(deskey);
// 加密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
// 加密,并把字节数组编码成字符串
encryptedData = new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes()));
} catch (Exception e) {throw new RuntimeException("加密谬误,错误信息:", e);
}
return encryptedData;
}
/**
* DES 解密
*
* @author : chenssy
* @date : 2016 年 5 月 20 日 下午 5:52:23
*
* @param cryptData
* 待解密密文
* @param key
* 校验位
* @return
*/
@SuppressWarnings("restriction")
protected static String decrypt(String cryptData,String key) {
String decryptedData = null;
try {
// DES 算法要求有一个可信赖的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(key.getBytes());
// 创立一个密匙工厂,而后用它把 DESKeySpec 转换成一个 SecretKey 对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(deskey);
// 解密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
// 把字符串解码为字节数组,并解密
decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData)));
} catch (Exception e) {throw new RuntimeException("解密谬误,错误信息:", e);
}
return decryptedData;
}
}
package com.JUtils.encrypt;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
/**
* AES 加解密
*
* @Author:chenssy
* @date:2016 年 5 月 21 日 上午 9:01:41
*/
class AESUtils {
/** 默认秘钥 */
protected static final String KEY = "NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0";
/**
* AES 解密
*
* @author:chenssy
* @date : 2016 年 5 月 21 日 上午 9:48:12
*
* @param encryptValue
* 待解密内容
* @param key
* 秘钥
* @return
* @throws Exception
*/
protected static String decrypt(String encryptValue, String key) throws Exception {return aesDecryptByBytes(base64Decode(encryptValue), key);
}
/**
* AES 加密
*
* @author:chenssy
* @date : 2016 年 5 月 21 日 上午 9:48:42
*
* @param value
* 待加密内容
* @param key
* 秘钥
* @return
* @throws Exception
*/
protected static String encrypt(String value, String key) throws Exception {return base64Encode(aesEncryptToBytes(value, key));
}
private static String base64Encode(byte[] bytes){return Base64Utils.encrypt(bytes);
}
@SuppressWarnings("static-access")
private static byte[] base64Decode(String base64Code) throws Exception{return base64Code == null ? null : new Base64Utils().decrypt(base64Code);
}
private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(encryptKey.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(decryptKey.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
}
正文完