1
13693261870
2025-01-14 2421ab222448e9db9bba26315c2bd4b7419e7a42
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,6 +226,34 @@
        }
    }
    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(String[] args) {
        Integer a = null;
        System.out.println(isNotEmpty(a));