From cc3fe1ad9b366533d184d9e239b45f731f014e74 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期四, 23 四月 2026 11:22:38 +0800
Subject: [PATCH] Merge branch 'master' into master-手术随访
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/AesUtils.java | 129 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 129 insertions(+), 0 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/AesUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/AesUtils.java
new file mode 100644
index 0000000..902650d
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/AesUtils.java
@@ -0,0 +1,129 @@
+package com.ruoyi.common.utils;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * AES 鍔犺В瀵嗗伐鍏风被
+ * 绠楁硶锛欰ES/CBC/PKCS5Padding锛堢瓑鏁� PKCS7锛�128 浣嶆暟鎹潡锛�
+ * 杈撳嚭锛欻EX 澶у啓瀛楃涓�
+ * 瀛楃闆嗭細UTF-8
+ */
+public class AesUtils {
+
+ /** 绠楁硶/妯″紡/濉厖 */
+ private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
+
+ /** 榛樿 Key锛圚EX锛�32瀛楄妭鈫�256浣嶅瘑閽ワ級 */
+ private static final String DEFAULT_KEY = "0F471C56362408AF8DB929C38EDFD23C";
+
+ /** 榛樿 IV锛圚EX锛�16瀛楄妭鈫�128浣嶅亸绉婚噺锛� */
+ private static final String DEFAULT_IV = "11BEE6E35B881A33CF1649607295D1A7";
+
+ private AesUtils() {
+ }
+
+ // -------------------------------------------------------------------------
+ // 鍏叡 API
+ // -------------------------------------------------------------------------
+
+ /**
+ * 浣跨敤榛樿 Key / IV 鍔犲瘑
+ *
+ * @param plainText 鏄庢枃
+ * @return HEX 澶у啓瀵嗘枃
+ */
+ public static String encrypt(String plainText) {
+ return encrypt(plainText, DEFAULT_KEY, DEFAULT_IV);
+ }
+
+ /**
+ * 浣跨敤榛樿 Key / IV 瑙e瘑
+ *
+ * @param hexCipherText HEX 澶у啓瀵嗘枃
+ * @return 鏄庢枃
+ */
+ public static String decrypt(String hexCipherText) {
+ return decrypt(hexCipherText, DEFAULT_KEY, DEFAULT_IV);
+ }
+
+ /**
+ * AES-CBC 鍔犲瘑
+ *
+ * @param plainText 鏄庢枃
+ * @param hexKey HEX 鏍煎紡鐨勫瘑閽ワ紙16/24/32瀛楄妭瀵瑰簲128/192/256浣嶏級
+ * @param hexIv HEX 鏍煎紡鐨勫亸绉婚噺锛�16瀛楄妭锛�
+ * @return HEX 澶у啓瀵嗘枃
+ */
+ public static String encrypt(String plainText, String hexKey, String hexIv) {
+ try {
+ byte[] keyBytes = hexToBytes(hexKey);
+ byte[] ivBytes = hexToBytes(hexIv);
+ SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
+ IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
+ Cipher cipher = Cipher.getInstance(ALGORITHM);
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
+ byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
+ return bytesToHex(encrypted).toUpperCase();
+ } catch (Exception e) {
+ throw new RuntimeException("AES 鍔犲瘑澶辫触", e);
+ }
+ }
+
+ /**
+ * AES-CBC 瑙e瘑
+ *
+ * @param hexCipherText HEX 澶у啓瀵嗘枃
+ * @param hexKey HEX 鏍煎紡鐨勫瘑閽�
+ * @param hexIv HEX 鏍煎紡鐨勫亸绉婚噺
+ * @return 鏄庢枃
+ */
+ public static String decrypt(String hexCipherText, String hexKey, String hexIv) {
+ try {
+ byte[] keyBytes = hexToBytes(hexKey);
+ byte[] ivBytes = hexToBytes(hexIv);
+ byte[] cipherBytes = hexToBytes(hexCipherText);
+ SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
+ IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
+ Cipher cipher = Cipher.getInstance(ALGORITHM);
+ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
+ byte[] decrypted = cipher.doFinal(cipherBytes);
+ return new String(decrypted, StandardCharsets.UTF_8);
+ } catch (Exception e) {
+ throw new RuntimeException("AES 瑙e瘑澶辫触", e);
+ }
+ }
+
+ // -------------------------------------------------------------------------
+ // 绉佹湁杈呭姪鏂规硶
+ // -------------------------------------------------------------------------
+
+ /**
+ * HEX 瀛楃涓茶浆瀛楄妭鏁扮粍锛堝ぇ灏忓啓鍧囧彲锛�
+ */
+ private static byte[] hexToBytes(String hex) {
+ if (hex == null || hex.length() % 2 != 0) {
+ throw new IllegalArgumentException("闈炴硶 HEX 瀛楃涓诧細" + hex);
+ }
+ int len = hex.length();
+ byte[] data = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ + Character.digit(hex.charAt(i + 1), 16));
+ }
+ return data;
+ }
+
+ /**
+ * 瀛楄妭鏁扮粍杞� HEX 瀛楃涓诧紙灏忓啓锛岃皟鐢ㄦ柟缁熶竴杞ぇ鍐欙級
+ */
+ private static String bytesToHex(byte[] bytes) {
+ StringBuilder sb = new StringBuilder(bytes.length * 2);
+ for (byte b : bytes) {
+ sb.append(String.format("%02x", b));
+ }
+ return sb.toString();
+ }
+}
--
Gitblit v1.9.3