package com.grid.service.common.util; import lombok.extern.slf4j.Slf4j; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; /** * AES加解密工具類 */ @Slf4j public class AESUtil { /** * 加密 * 1.構造密鑰生成器 * 2.根據ecnodeRules規則初始化密鑰生成器 * 3.產生密鑰 * 4.創建和初始化密碼器 * 5.內容加密 * 6.返回字符串 */ public static String aesEncode(String content) { try { //1.構造密鑰生成器,指定為AES算法,不區分大小寫 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); //2.根據ecnodeRules規則初始化密鑰生成器 //生成一個128位的隨機源,根據傳入的字節數組 SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(Constant.ENCODE_RULES.getBytes()); keyGenerator.init(128, random); //3.產生原始對稱密鑰 SecretKey originalKey = keyGenerator.generateKey(); //4.獲得原始對稱密鑰的字節數組 byte[] raw = originalKey.getEncoded(); //5.根據字節數組生成AES密鑰 SecretKey key = new SecretKeySpec(raw, "AES"); //6.根據指定算法AES自成密碼器 Cipher cipher = Cipher.getInstance("AES"); //7.初始化密碼器,第一個參數為加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二個參數為使用的KEY cipher.init(Cipher.ENCRYPT_MODE, key); //8.獲取加密內容的字節數組(這里要設置為utf-8)不然內容中如果有中文和英文混合中文就會解密為亂碼 byte[] byteEncode = content.getBytes("utf-8"); //9.根據密碼器的初始化方式--加密:將數據加密 byte[] byteAES = cipher.doFinal(byteEncode); //10.將加密后的數據轉換為字符串 //這里用Base64Encoder中會找不到包 //解決辦法: //在項目的Build path中先移除JRE System Library,再添加庫JRE System Library,重新編譯后就一切正常了。 String aesEncode = new String(new BASE64Encoder().encode(byteAES)); //11.將字符串返回 return aesEncode; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //如果有錯就返加nulll return null; } /** * 解密 * 解密過程: * 1.同加密1-4步 * 2.將加密后的字符串反紡成byte[]數組 * 3.將加密內容解密 */ public static String aesDecode(String content) { try { //1.構造密鑰生成器,指定為AES算法,不區分大小寫 KeyGenerator keygen = KeyGenerator.getInstance("AES"); //2.根據ecnodeRules規則初始化密鑰生成器 //生成一個128位的隨機源,根據傳入的字節數組 SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(Constant.ENCODE_RULES.getBytes()); keygen.init(128, random); //3.產生原始對稱密鑰 SecretKey originalKey = keygen.generateKey(); //4.獲得原始對稱密鑰的字節數組 byte[] raw = originalKey.getEncoded(); //5.根據字節數組生成AES密鑰 SecretKey key = new SecretKeySpec(raw, "AES"); //6.根據指定算法AES自成密碼器 Cipher cipher = Cipher.getInstance("AES"); //7.初始化密碼器,第一個參數為加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二個參數為使用的KEY cipher.init(Cipher.DECRYPT_MODE, key); //8.將加密並編碼后的內容解碼成字節數組 byte[] byteContent = new BASE64Decoder().decodeBuffer(content); /* * 解密 */ byte[] byteDecode = cipher.doFinal(byteContent); String aesDecode = new String(byteDecode, "utf-8"); return aesDecode; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { throw new RuntimeException("兄弟,配置文件中的密碼需要使用AES加密,請使用com.zheng.common.util.AESUtil工具類修改這些值!"); //e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } //如果有錯就返加nulll return null; } public static void main(String[] args) { String[] keys = { "123456" }; log.warn("key | AESEncode | AESDecode"); String s = aesDecode("6x9mfxP8HvsL/Y93pKzu4A=="); log.warn(s); /*for (String key : keys) { log.warn(key + " | "); String encryptString = aesEncode(key); log.warn("加密:" + encryptString); String decryptString = aesDecode(encryptString); log.warn("解密" + decryptString); }*/ } }