From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001
From: sws <15810472099@163.com>
Date: 星期六, 26 十一月 2022 16:12:02 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/helper/AesHelper.java |  129 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 129 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/lf/server/helper/AesHelper.java b/src/main/java/com/lf/server/helper/AesHelper.java
new file mode 100644
index 0000000..78efbfc
--- /dev/null
+++ b/src/main/java/com/lf/server/helper/AesHelper.java
@@ -0,0 +1,129 @@
+package com.lf.server.helper;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.spec.SecretKeySpec;
+import java.util.Base64;
+
+/**
+ * AES鍔犲瘑甯姪
+ * @author WWW
+ */
+@SuppressWarnings("restriction")
+public class AesHelper {
+    /**
+     * 瀵嗛挜闀垮害蹇呴』鏄�16
+     */
+    private static final String DEFAULT_KEY = "A#s_lF_sErve_k.y";
+
+    private static final String KEY_ALGORITHM = "AES";
+
+    /**
+     * 绠楁硶
+     */
+    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
+
+    /**
+     * aes瑙e瘑
+     *
+     * @param encrypt 鍐呭
+     * @return
+     * @throws Exception
+     */
+    public static String decrypt(String encrypt) throws Exception {
+        return decrypt(encrypt, DEFAULT_KEY);
+    }
+
+    /**
+     * aes鍔犲瘑
+     *
+     * @param content 鍐呭
+     * @return
+     * @throws Exception
+     */
+    public static String encrypt(String content) throws Exception {
+        return encrypt(content, DEFAULT_KEY);
+    }
+
+    /**
+     * base 64 encode
+     *
+     * @param bytes 寰呯紪鐮佺殑byte[]
+     * @return 缂栫爜鍚庣殑base64 code
+     */
+    private static String base64Encode(byte[] bytes) {
+        return Base64.getEncoder().encodeToString(bytes);
+    }
+
+    /**
+     * base 64 decode
+     *
+     * @param base64Code 寰呰В鐮佺殑base64 code
+     * @return 瑙g爜鍚庣殑byte[]
+     * @throws Exception
+     */
+    private static byte[] base64Decode(String base64Code) {
+        return StringHelper.isEmpty(base64Code) ? null : Base64.getDecoder().decode(base64Code);
+    }
+
+    /**
+     * AES鍔犲瘑
+     *
+     * @param content    寰呭姞瀵嗙殑鍐呭
+     * @param encryptKey 鍔犲瘑瀵嗛挜
+     * @return 鍔犲瘑鍚庣殑byte[]
+     * @throws Exception
+     */
+    private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
+        // KeyGenerator kGen = KeyGenerator.getInstance(KEY_ALGORITHM)
+        // kGen.init(128)
+
+        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
+        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), KEY_ALGORITHM));
+
+        return cipher.doFinal(content.getBytes("utf-8"));
+    }
+
+    /**
+     * AES鍔犲瘑涓篵ase 64 code
+     *
+     * @param content    寰呭姞瀵嗙殑鍐呭
+     * @param encryptKey 鍔犲瘑瀵嗛挜
+     * @return 鍔犲瘑鍚庣殑base 64 code
+     * @throws Exception
+     */
+    public static String encrypt(String content, String encryptKey) throws Exception {
+        return base64Encode(aesEncryptToBytes(content, encryptKey));
+    }
+
+    /**
+     * 灏哹ase 64 code AES瑙e瘑
+     *
+     * @param encryptStr 寰呰В瀵嗙殑base 64 code
+     * @param decryptKey 瑙e瘑瀵嗛挜
+     * @return 瑙e瘑鍚庣殑string
+     * @throws Exception
+     */
+    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
+        return StringHelper.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
+    }
+
+    /**
+     * AES瑙e瘑
+     *
+     * @param encryptBytes 寰呰В瀵嗙殑byte[]
+     * @param decryptKey   瑙e瘑瀵嗛挜
+     * @return 瑙e瘑鍚庣殑String
+     * @throws Exception
+     */
+    private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
+        // KeyGenerator kGen = KeyGenerator.getInstance(KEY_ALGORITHM)
+        // kGen.init(128)
+
+        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
+        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), KEY_ALGORITHM));
+        byte[] decryptBytes = cipher.doFinal(encryptBytes);
+
+        return new String(decryptBytes);
+    }
+}

--
Gitblit v1.9.3