package com.hai.common.security; import com.hai.common.Base64Util; import org.apache.commons.lang3.StringUtils; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; import java.math.BigInteger; import java.security.SecureRandom; import java.util.Base64; public class AESEncodeUtil { private static final String SEC_KEY="Skufk5oi85wDFGl888i6wsRSTkdd5df5"; public static String binary(byte[] bytes, int radix){ return new BigInteger(1, bytes).toString(radix); } public static String base64Encode(byte[] bytes){ return Base64.getEncoder().encodeToString(bytes);//.replaceAll("\\s*", ""); } public static byte[] base64Decode(String base64Code) throws Exception{ return StringUtils.isEmpty(base64Code) ? null : Base64.getDecoder().decode(base64Code); } public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception{ KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(encryptKey.getBytes()); kgen.init(128, secureRandom); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); return cipher.doFinal(content.getBytes("utf-8")); } public static String aesEncrypt(String content) throws Exception{ return base64Encode(aesEncryptToBytes(content, SEC_KEY)); //return content; } public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception{ KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(decryptKey.getBytes()); kgen.init(128, secureRandom); // byte[] key = {86, -42, -11, 12, 92, 41, 31, -48, 104, 31, -38, 106, -51, -16, -55, -96}; 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); } public static String aesDecrypt(String encryptStr) throws Exception{ return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), SEC_KEY); } public static void main(String[] args) throws Exception{ long currentTimeMillis = System.currentTimeMillis(); System.out.println(AESEncodeUtil.aesDecrypt(Base64Util.decode("ZStZUjRIcFl5SVo4MEhzSU96MldDUEFZa3lmbUhpNTdKMGhCVytIY3NJaz0"))); /* String content = "{\"create_time\":1573544092110,\"order_serial_no\":\"40280e816db49c0b016db4a2c31f0004\",\"product_code\":\"8a9e80045cf85e54015cf8809fcd\",\"product_name\":\"平安发票贷\",\"uscc\":\"91370781687233838E\"}"; System.out.println("加密前" + content); String encrypt = com.sun.org.apache.xml.internal.security.utils.Base64.encode(aesEncryptToBytes(content, "WdOjUqtRxcBshw")); System.out.println("加密后" + encrypt);*/ /* String decrypt = aesDecryptByBytes(base64Decode("i98CPRG2UI2kRzP9WE6WyF7hQQmZB5nOvc9s8BoISUJlrt59R3TPFiDYI9FNpj3BtKKR8P9JMoTSRP0/lP+SzA=="), "WdOjUqtRxcBshw"); System.out.println("解密后" + decrypt); System.out.println(System.currentTimeMillis() - currentTimeMillis);*/ } }