public class AES { private static final Logger logger = LoggerFactory.getLogger(AES.class); private static final String KEY_ALGORITHM = "AES"; private static final String CHAR_SET = "UTF-8"; /** * AES的密钥长度 */ private static final Integer SECRET_KEY_LENGTH = 128; /** * 加解密算法/工作模式/填充方式 */ private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /** * 算法 */ private static final String ALGORITHM = "SHA1PRNG"; /** * 加密 * @param sourceText 原文 * @param key 加密密钥 * @return 返回Base64转码后的加密数据 */ public static String encrypt(String sourceText, String key) throws Exception { if (StringUtils.isAnyEmpty(sourceText, key)) { logger.error("AES encryption params is null"); return null; } Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); byte[] byteContent = sourceText.getBytes(CHAR_SET); cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key)); byte[] encryptByte = cipher.doFinal(byteContent); return Base64.encodeBase64String(encryptByte); } /** * 加密 * @param sourceText 原文 * @param key 加密密钥 * @return 返回hex转码后的加密数据 */ public static String encryptHex(String sourceText, String key) throws Exception { if (StringUtils.isAnyEmpty(sourceText, key)) { logger.error("AES encryption params is null"); return null; } Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); byte[] byteContent = sourceText.getBytes(CHAR_SET); cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key)); byte[] encryptByte = cipher.doFinal(byteContent); return Hex.encodeHexString(encryptByte); } /** * 解密操作 * @param cipherText 加密的密文 * @param key 解密密钥 * @return */ public static String decrypt(String cipherText, String key) throws Exception{ if (StringUtils.isAnyEmpty(cipherText, key)) { logger.error("AES decryption params is null"); return null; } Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key)); byte[] result = cipher.doFinal(Base64.decodeBase64(cipherText)); return new String(result, CHAR_SET); } /** * 解密操作 * @param cipherText 加密的密文 * @param key 解密密钥 * @return */ public static String decryptHex(String cipherText, String key) throws Exception{ if (StringUtils.isAnyEmpty(cipherText, key)) { logger.error("AES decryption params is null"); return null; } Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key)); byte[] result = cipher.doFinal(Hex.decodeHex(cipherText)); return new String(result, CHAR_SET); } private static SecretKeySpec getSecretKey(final String key) throws NoSuchAlgorithmException, NoSuchProviderException { KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); SecureRandom secureRandom = SecureRandom.getInstance(ALGORITHM); secureRandom.setSeed(key.getBytes()); keyGenerator.init(SECRET_KEY_LENGTH, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM); } public static void main(String[] args) throws Exception { String str = "Hello World"; String str1 = "Minor versions (like 2.1 and 2.2) are backwards compatible with respect to public API: old code should work without recompilation, if (but only if) it relies on external API; public methods, annotations. When overriding internal functionality"; System.out.println("str:" + str); // String encryptStr = encrypt(str, "aa7889d3-435b-4ed2-99f9-08035661eda9"); // System.out.println("encrypt:" + encryptStr); // // String decryptStr = decrypt(encryptStr, "aa7889d3-435b-4ed2-99f9-08035661eda9"); String key = "YbBLMHz3P5DPaapYg1hU5yHTLJuEZ5PUT4lBBzut1lUIScfBB5Rhrxn3Gyf3PTAb"; // String encryptStr = encrypt("jw0katgo1gh75yztg3o", key); long start = System.currentTimeMillis(); String index = EncryptStringUtil.getRandomString(18); System.out.println("index:" + index); // String key = EncryptStringUtil.getRandomStringKey(64); System.out.println("key:" + key); String encryptStr1 = encrypt(str, key); System.out.println("encrypt:" + encryptStr1); // String decryptStr = decrypt(encryptStr, key); // System.out.println("decryptStr:" + decryptStr); long end = System.currentTimeMillis(); System.out.println("total spendtime:" + (end-start)); long start2 = System.currentTimeMillis(); String str2 = EncryptStringUtil.getRandomString(19); System.out.println("str2:"+ str2); String encryptStr2 = encryptHex(str, key); System.out.println("encryptStr2:" + encryptStr2); String source = decryptHex(encryptStr2, key); System.out.println("source:" + source); long end2 = System.currentTimeMillis(); System.out.println("total spendtime:" + (end2-start2)); } }
AES 对称算法
于 2022-02-16 13:51:23 首次发布