From ef225d856cdaf8c5f5b7c08db4b1a3e01ffca90f Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 14 一月 2025 16:57:37 +0800 Subject: [PATCH] 1 --- se-modules/se-system/src/main/java/com/se/system/utils/CommonUtils.java | 35 ++++++++++++++++++++++++++++++++++- 1 files changed, 34 insertions(+), 1 deletions(-) diff --git a/se-modules/se-system/src/main/java/com/se/system/utils/CommonUtils.java b/se-modules/se-system/src/main/java/com/se/system/utils/CommonUtils.java index 38d29b0..c12ba02 100644 --- a/se-modules/se-system/src/main/java/com/se/system/utils/CommonUtils.java +++ b/se-modules/se-system/src/main/java/com/se/system/utils/CommonUtils.java @@ -1,5 +1,8 @@ package com.se.system.utils; +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.security.MessageDigest; import java.util.*; @SuppressWarnings("ALL") @@ -10,6 +13,8 @@ public CommonUtils() { } + + private static final String key = "Secret_Ke?y_02_95"; public static String validStringValue(Object object) { return object == null ? "" : object.toString(); @@ -221,7 +226,35 @@ } } - public static void test(String[] args) { + + public static String encrypt(String json) throws Exception { + byte[] keyBytes = key.getBytes(); + MessageDigest sha = MessageDigest.getInstance("SHA-256"); + keyBytes = sha.digest(keyBytes); + keyBytes = java.util.Arrays.copyOf(keyBytes, 16); + SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); + + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + byte[] encryptedBytes = cipher.doFinal(json.getBytes()); + return Base64.getEncoder().encodeToString(encryptedBytes); + } + + public static String decrypt(String encryptedJson) throws Exception { + byte[] keyBytes = key.getBytes(); + MessageDigest sha = MessageDigest.getInstance("SHA-256"); + keyBytes = sha.digest(keyBytes); + keyBytes = java.util.Arrays.copyOf(keyBytes, 16); + SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); + + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + byte[] encryptedBytes = Base64.getDecoder().decode(encryptedJson); + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); + return new String(decryptedBytes); + } + + public static void test() { Integer a = null; System.out.println(isNotEmpty(a)); String str = "[\"11\",\"222\"]"; -- Gitblit v1.9.3