import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec;import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
/**
* 加密解密工具,支持多种加密方式(AES,ARCFOUR,Blowfish,DES,RC2,DESede) * 可实现对数字、字母、特殊字符、汉子、常用转义字符等实现加密解密 * * :几乎所有的字符都可以实现加密解密,除了“\0”或“\u0000”这种 * * liyang * */ @SuppressWarnings("restriction") public class EncryptUtil { private static String Algorithm = "AES";/* 固定秘钥 */
private static byte[] DEFAULT_KEY = new byte[] { 67, -41, 90, -86, 77, 111, 43, 14, -115, -37, 95, -39, 0, -93, -63, 25 }; private static String VALUE_ENCODING = "UTF-8";public static void main(String args[]) throws Exception {
// 也可以动态生成随机秘钥
// DEFAULT_KEY = getSecretKey(); // 也可以生成指定秘钥 // System.out.println(DEFAULT_KEY = getSecretKey("kevin_Lee"));String str = "\\njakdhk\\f\t\r\b@#%^&*!`1`你好大幅度dfasd22?、、、999999`’;\"\';/.,><?:|}{[]1`234567890-=+_)(*&^%$#@!~";
System.out.println(EncryptUtil.encryptString(getConvert(str), DEFAULT_KEY)); System.out.println( EncryptUtil.decryptString(EncryptUtil.encryptString(getConvert(str), DEFAULT_KEY), DEFAULT_KEY)); }/**
* 加密给定的字符串 * * @param value * @return 加密后的base64字符串 */ public static String encryptString(String value) { return encryptString(value, DEFAULT_KEY); }/**
* 根据给定的密钥加密字符串 * * @param value * 待加密的字符串 * @param key * 以BASE64形式存在的密钥 * @return 加密后的base64字符串 * @throws IOException */ public static String encryptString(String value, String key) throws IOException { return encryptString(value, Base64.decode(key)); }/**
* 根据给定的密钥加密字符串 * * @param value * 待加密的字符串 * @param key * 字节数组形式的密钥 * @return 加密后的base64字符串 */ public static String encryptString(String value, byte[] key) { try { byte[] data = value.getBytes(VALUE_ENCODING); data = EncryptUtil.encryptData(data, key); return Base64.encode(data); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }/**
* 解密字符串 * * @param value * base64形式存在的密文 * @return 明文 */ public static String decryptString(String value) { return decryptString(value, DEFAULT_KEY); }/**
* 解密字符串 * * @param value * base64形式存在的密文 * @param key * base64形式存在的密钥 * @return 明文 * @throws IOException */ public static String decryptString(String value, String key) throws IOException { String s = decryptString(value, Base64.decode(key)); return s; }/**
* 解密字符串 * * @param value * base64形式存在的密文 * @param key * 字节数据形式存在的密钥 * @return 明文 */ public static String decryptString(String value, byte[] key) { try { byte[] data = Base64.decode(value); data = EncryptUtil.decryptData(data, key); return new String(data, VALUE_ENCODING); } catch (Exception e) { e.printStackTrace(); return null; } }/**
* 将指定的数据根据提供的密钥进行加密 * * @param input * 需要加密的数据 * @param key * 密钥 * @return byte[] 加密后的数据 * @throws Exception */ private static byte[] encryptData(byte[] input, byte[] key) throws Exception { SecretKey deskey = new SecretKeySpec(key, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); byte[] cipherByte = c1.doFinal(input); return cipherByte;}
private static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input, DEFAULT_KEY); }/**
* 生成随机密钥 * * @return byte[] 返回生成的密钥 * @throws exception * 扔出异常. */ private static byte[] getSecretKey() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); SecretKey deskey = keygen.generateKey(); return deskey.getEncoded();}
/**
* @Title: getSecretKey * @Description: 按指定字符串秘钥生成秘钥 * @return byte[] 返回类型 * @date 2016年8月18日 * @author liyang */ private static byte[] getSecretKey(String passWord) throws Exception { String newPassWord = passWord.replaceAll("-", ""); KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(newPassWord.getBytes()); keygen.init(secureRandom); return keygen.generateKey().getEncoded(); }/**
* 将给定的已加密的数据通过指定的密钥进行解密 * * @param input * 待解密的数据 * @param key * 密钥 * @return byte[] 解密后的数据 * @throws Exception */ private static byte[] decryptData(byte[] input, byte[] key) throws Exception { SecretKey deskey = new SecretKeySpec(key, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE, deskey); byte[] clearByte = c1.doFinal(input); return clearByte; }private static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input, DEFAULT_KEY); }/**
* 字节码转换成16进制字符串 * * @param byte[] * b 输入要转换的字节码 * @return String 返回转换后的16进制字符串 */ private static String byte2hex(byte[] bytes) { StringBuilder hs = new StringBuilder(); for (byte b : bytes) hs.append(String.format("%1$02X", b)); return hs.toString(); }private static byte[] hex2byte(String content) {
int l = content.length() >> 1; byte[] result = new byte[l]; for (int i = 0; i < l; i++) { int j = i << 1; String s = content.substring(j, j + 2); result[i] = Integer.valueOf(s, 16).byteValue(); } return result; }/**
* @Title: getConvert * @Description: 替换常用转义字符 * @return String 返回类型 * @author liyang */ private static String getConvert(String oldStr) { return oldStr.replaceAll("\\\\", "\\\\\\\\") // .replaceAll("\0", "\\\\0") // .replaceAll("\1", "\\\\1") // .replaceAll("\2", "\\\\2") // .replaceAll("\3", "\\\\3") // .replaceAll("\4", "\\\\4") // .replaceAll("\5", "\\\\5") // .replaceAll("\6", "\\\\6") // .replaceAll("\7", "\\\\7") // .replaceAll("\10", "\\\\10") // .replaceAll("\11", "\\\\11") // .replaceAll("\12", "\\\\12") // .replaceAll("\13", "\\\\13") // .replaceAll("\14", "\\\\14") // .replaceAll("\30", "\\\\30") .replaceAll("\77", "\\\\77") // .replaceAll("\100", "\\\\100") .replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r").replaceAll("\t", "\\\\t").replaceAll("\b", "\\\\b") .replaceAll("\f", "\\\\f").replaceAll("\"", "\\\\\"").replaceAll("\'", "\\\\\'"); } }