| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.pipIrrSell.util; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.crypto.Cipher; | 
|---|
|  |  |  | import javax.crypto.NoSuchPaddingException; | 
|---|
|  |  |  | import javax.crypto.spec.GCMParameterSpec; | 
|---|
|  |  |  | import javax.crypto.spec.SecretKeySpec; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.security.GeneralSecurityException; | 
|---|
|  |  |  | import java.security.InvalidAlgorithmParameterException; | 
|---|
|  |  |  | import java.security.InvalidKeyException; | 
|---|
|  |  |  | import java.security.NoSuchAlgorithmException; | 
|---|
|  |  |  | import java.util.Base64; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author ZhuBaoMin | 
|---|
|  |  |  | * @date 2024-03-06 11:46 | 
|---|
|  |  |  | * @LastEditTime 2024-03-06 11:46 | 
|---|
|  |  |  | * @Description | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class AesUtil { | 
|---|
|  |  |  | static final int KEY_LENGTH_BYTE = 32; | 
|---|
|  |  |  | static final int TAG_LENGTH_BIT = 128; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 解密 | 
|---|
|  |  |  | * @param apiV3Key apiV3密钥 | 
|---|
|  |  |  | * @param associatedData 附加数据 | 
|---|
|  |  |  | * @param nonce 随机串 | 
|---|
|  |  |  | * @param ciphertext 数据密文 | 
|---|
|  |  |  | * @return 解密后字符串 | 
|---|
|  |  |  | * @throws GeneralSecurityException | 
|---|
|  |  |  | * @throws IOException | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String decryptToString(byte[] apiV3Key, byte[] associatedData, byte[] nonce, String ciphertext) throws GeneralSecurityException, IOException { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | 
|---|
|  |  |  | SecretKeySpec key = new SecretKeySpec(apiV3Key, "AES"); | 
|---|
|  |  |  | GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | cipher.init(Cipher.DECRYPT_MODE, key, spec); | 
|---|
|  |  |  | cipher.updateAAD(associatedData); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8"); | 
|---|
|  |  |  | } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { | 
|---|
|  |  |  | throw new IllegalStateException(e); | 
|---|
|  |  |  | } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { | 
|---|
|  |  |  | throw new IllegalArgumentException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|