package com.webmanage.util; import com.webmanage.common.BusinessException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.crypto.Cipher; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * AES加密工具类 * * @author August * @date 2022-07-29 */ @Slf4j public class AESUtil { private AESUtil() { } /** * AES_KEY */ private static final String AES_KEY = "sD6i0YYW1a5FnzF6vRf13VtHocZabRcI"; /** * GCM_IV */ private static final String GCM_IV = "1SzB4YR0c0E9"; /** * GCM_TAG长度 */ private static final int GCM_TAG_LENGTH = 16; /** * 加密 * * @param plaintext 待加密文本 * @param key AES_KEY * @param iv GCM_IV * @return 密文 */ public static String encrypt(String plaintext, byte[] key, byte[] iv) { if (StringUtils.isBlank(plaintext) || key == null || iv == null) { throw new BusinessException("加密参数不能为空"); } try { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv); cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); // Perform Encryption byte[] cipherText = cipher.doFinal(plaintext.getBytes()); StringBuilder sb = new StringBuilder(); for (byte b : cipherText) { String hex = Integer.toHexString(b & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex); } return sb.toString(); } catch (Exception e) { throw new BusinessException("加密失败"); } } /** * 解密 * * @param cipherText 密文 * @param key AES_KEY * @param iv GCM_IV * @return 解密结果 */ public static String decrypt(String cipherText, byte[] key, byte[] iv) { if (StringUtils.isBlank(cipherText) || key == null || iv == null) { throw new BusinessException("解密参数不能为空"); } try { byte[] cipherTextByte = new byte[cipherText.length() / 2]; for (int i = 0; i < cipherText.length() / 2; i++) { int high = Integer.parseInt(cipherText.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt( cipherText.substring(i * 2 + 1, i * 2 + 2), 16); cipherTextByte[i] = (byte) (high * 16 + low); } Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv); cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec); byte[] decryptedText = cipher.doFinal(cipherTextByte); return new String(decryptedText); } catch (Exception e) { throw new BusinessException("解密失败"); } } /** * 使用默认密钥和IV进行解密 * @param cipherText 以十六进制字符串表示的密文 * @return 明文 */ public static String decryptWithDefaultKey(String cipherText) { return decrypt(cipherText, AES_KEY.getBytes(), GCM_IV.getBytes()); } public static void main(String[] args) { // String plainText = "zynlpt2024"; String plainText = "postgres"; log.info("Original Text : " + plainText); String cipherText = encrypt(plainText, AES_KEY.getBytes(), GCM_IV.getBytes()); log.info("Encrypted Text : " + cipherText); String decryptedText = decrypt(cipherText, AES_KEY.getBytes(), GCM_IV.getBytes()); log.info("DeCrypted Text : " + decryptedText); } }