From d028c9bff98612c3daadf47afb2542a53158282e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=82=85=E5=93=A5?= <184172133@qq.com> Date: Wed, 29 Nov 2023 07:39:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E5=82=85=E5=93=A5=EF=BC=8Cfeat?= =?UTF-8?q?=EF=BC=9A=E6=9E=B6=E6=9E=84=E7=9A=84=E6=9C=AC=E8=B4=A8=E4=B9=8B?= =?UTF-8?q?=20DDD=20=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 34 ---------- .../cn/bugstack/xfg/frame/test/AESTest.java | 65 +++++++++++++++++++ 2 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 xfg-frame-app/src/test/java/cn/bugstack/xfg/frame/test/AESTest.java diff --git a/pom.xml b/pom.xml index 936d551..454b62e 100644 --- a/pom.xml +++ b/pom.xml @@ -170,40 +170,6 @@ prod - - - - maven-enforcer-plugin - - - org.codehaus.mojo - extra-enforcer-rules - 1.4 - - - - - enforce-banned-dependencies-app - install - - enforce - - - - - true - - *:*:[,0.0.1) - - - - true - - - - - - diff --git a/xfg-frame-app/src/test/java/cn/bugstack/xfg/frame/test/AESTest.java b/xfg-frame-app/src/test/java/cn/bugstack/xfg/frame/test/AESTest.java new file mode 100644 index 0000000..24ff002 --- /dev/null +++ b/xfg-frame-app/src/test/java/cn/bugstack/xfg/frame/test/AESTest.java @@ -0,0 +1,65 @@ +package cn.bugstack.xfg.frame.test; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.util.Base64; + +/** + * @author Fuzhengwei bugstack.cn @小傅哥 + * @description + * @create 2023-07-18 22:08 + */ +public class AESTest { + + /** + * 密钥,必须是16位 + */ + private static final String KEY = "1143456789abudef"; + /** + * 偏移量,必须是16位 + */ + private static final String IV = "abcdef9806543210"; + + public static void main(String[] args) throws Exception { + System.out.println(encrypt("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")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + System.out.println(encrypt("")); + } + + public static String encrypt(String content) throws Exception { + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + byte[] raw = KEY.getBytes(); + SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES"); + IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes()); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); + byte[] encrypted = cipher.doFinal(content.getBytes()); + return Base64.getEncoder().encodeToString(encrypted); + } + + /** + * AES解密 + * + * @param content 密文 + * @return 明文 + * @throws Exception 异常 + */ + public String decrypt(String content) throws Exception { + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + byte[] raw = KEY.getBytes(); + SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES"); + IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes()); + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); + byte[] encrypted = Base64.getDecoder().decode(content); + byte[] original = cipher.doFinal(encrypted); + return new String(original); + } + +} -- GitLab