package com.founder.mrp.util; import java.nio.charset.StandardCharsets; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import com.founder.mrp.web.jsonEntity.AccountJson; /** * 加密解密類 * @author xiongzq * */ public class EncryptionDecryption { private static String strDefaultKey = "UvVi-s`75X-d9fO3"; /** 加密工具 */ private ThreadLocal<Cipher> encryptCipher = new ThreadLocal<Cipher>() { @Override protected Cipher initialValue() { try { Cipher encryptCipher = Cipher.getInstance(algorithmName); encryptCipher.init(Cipher.ENCRYPT_MODE, key); return encryptCipher; } catch (Exception e) { e.printStackTrace(); return null; } } }; /** 解密工具 */ private ThreadLocal<Cipher> decryptCipher = new ThreadLocal<Cipher>() { @Override protected Cipher initialValue() { try { Cipher decryptCipher = Cipher.getInstance(algorithmName); decryptCipher.init(Cipher.DECRYPT_MODE, key); return decryptCipher; } catch (Exception e) { e.printStackTrace(); return null; } } }; private String algorithmName; private Key key; /** * 將byte數組轉換爲表示16進制值的字符串, 如:byte[]{8,18}轉換爲:0813, 和public static byte[] * hexStr2ByteArr(String strIn) 互爲可逆的轉換過程 * * @param arrB * 須要轉換的byte數組 * @return 轉換後的字符串 * @throws Exception * */ public static String byteArr2HexStr(byte[] arrB) throws Exception { // 每一個byte用兩個字符才能表示,因此字符串的長度是數組長度的兩倍 StringBuilder sb = new StringBuilder(arrB.length * 2); for (int b : arrB) { int intTmp = b; // 把負數轉換爲正數 while (intTmp < 0) { intTmp = intTmp + 256; } // 小於0F的數須要在前面補0 if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } /** * 將表示16進制值的字符串轉換爲byte數組, 和public static String byteArr2HexStr(byte[] arrB) * 互爲可逆的轉換過程 * * @param strIn 須要轉換的字符串 * @return 轉換後的byte數組 * */ public static byte[] hexStr2ByteArr(String strIn) { byte[] arrB = strIn.getBytes(); int iLen = arrB.length; // 兩個字符表示一個字節,因此字節數組長度是字符串長度除以2 byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } /** * 默認構造方法,使用默認密鑰 * * @throws Exception */ public EncryptionDecryption() throws Exception { this("AES/ECB/PKCS5Padding", strDefaultKey, 128); } /** * 指定密鑰構造方法 * * @param strKey 指定的密鑰 * @throws Exception */ public EncryptionDecryption(String algorithmName, String strKey, int keySizeInBits) throws Exception { this.algorithmName = algorithmName; this.key = getKey(strKey.getBytes(StandardCharsets.UTF_8), keySizeInBits); } /** * 加密字節數組 * * @param arrB * 需加密的字節數組 * @return 加密後的字節數組 * @throws Exception */ public byte[] encrypt(byte[] arrB) throws Exception { return encryptCipher.get().doFinal(arrB); } /** * 加密字符串 * * @param strIn * 需加密的字符串 * @return 加密後的字符串 * @throws Exception */ public String encrypt(String strIn) throws Exception { return byteArr2HexStr(encrypt(strIn.getBytes())); } /** * 解密字節數組 * * @param arrB * 需解密的字節數組 * @return 解密後的字節數組 * @throws Exception */ public byte[] decrypt(byte[] arrB) throws Exception { return decryptCipher.get().doFinal(arrB); } /** * 解密字符串 * * @param strIn * 需解密的字符串 * @return 解密後的字符串 * @throws Exception */ public String decrypt(String strIn) throws Exception { try { return new String(decrypt(hexStr2ByteArr(strIn))); } catch (Exception e) { return ""; } } /** * 從指定字符串生成密鑰,密鑰所需的字節數組長度爲8位 不足8位時後面補0,超出8位只取前8位 * * @param arrBTmp * 構成該字符串的字節數組 * @param keySizeInBits * @return 生成的密鑰 * @throws java.lang.Exception */ private Key getKey(byte[] arrBTmp, int keySizeInBits) throws Exception { // 建立一個空的字節數組(默認值爲0) byte[] arrB = new byte[keySizeInBits / 8]; // 將原始字節數組轉換爲8位 for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } // 生成密鑰 String keyAlg = algorithmName; int t = algorithmName.indexOf('/'); if (t != -1) { keyAlg = algorithmName.substring(0, t); } return new SecretKeySpec(arrB, keyAlg); } }