CeDo
2021-04-01 48dd95b28f220f3cae4cae265f19862f4e874ced
bug fixed:解密异常处理
2个文件已修改
78 ■■■■ 已修改文件
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java 59 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/encrypt/DoEncrytDecrypt.java 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java
@@ -1,13 +1,12 @@
package com.panzhihua.common.model.helper;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
@@ -102,20 +101,11 @@
     * @param secret 秘钥
     * @return 加密后的字符串
     */
    public static String encrypt128(String strToEncrypt, String secret)
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
        }
        catch (Exception e)
        {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    public static String encrypt128(String strToEncrypt, String secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    }
    /**
@@ -124,20 +114,11 @@
     * @param secret 秘钥
     * @return 解密后的字符串
     */
    public static String decrypt128(String strToDecrypt, String secret)
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        }
        catch (Exception e)
        {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    public static String decrypt128(String strToDecrypt, String secret) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
}
@@ -165,14 +146,16 @@
        Arrays.stream(originalString).forEach(os ->{
            String encryptedString = AESUtil.encrypt128(os, key);
            String decryptedString = AESUtil.decrypt128(encryptedString, key);
            try {
                String encryptedString = AESUtil.encrypt128(os, key);
                String decryptedString = AESUtil.decrypt128(encryptedString, key);
            System.out.println(os);
            System.out.println(encryptedString);
            System.out.println(decryptedString);
                System.out.println(os);
                System.out.println(encryptedString);
                System.out.println(decryptedString);
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }
}
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/encrypt/DoEncrytDecrypt.java
@@ -32,8 +32,12 @@
                        log.debug("加密字段:" + field.getName());
                        Object fieldVal = field.get(parameterObject);
                        if(fieldVal!=null) {
                            String encryptedStr = AESUtil.encrypt128(fieldVal.toString(), aesKey);
                            field.set(parameterObject, encryptedStr);
                            try {
                                String encryptedStr = AESUtil.encrypt128(fieldVal.toString(), aesKey);
                                field.set(parameterObject, encryptedStr);
                            }catch (Exception e){
                                log.debug("加密失败");
                            }
                        }
                    }
                }
@@ -53,8 +57,15 @@
                        if (annotation.annotationType() == EncryptDecryptField.class) {
                            log.debug("解密密字段:" + field.getName());
                            String fieldVal = field.get(result).toString();
                            String decryptVal = AESUtil.decrypt128(fieldVal, aesKey);
                            field.set(result, decryptVal!=null?decryptVal:fieldVal);
                            try {
                                String decryptVal = AESUtil.decrypt128(fieldVal, aesKey);
                                field.set(result, decryptVal != null ? decryptVal : fieldVal);
                            }catch (Exception e){
                                /**
                                 * 兼容原始未加密数据
                                 */
                                field.set(result, fieldVal);
                            }
                        }
                    }
                }