天天看點

Springboot yml配置參數加密 ,jasypt自定義解密器(拓展篇)

前言

最近項目組開始關注一些敏感資料的明文相關的事宜 , 其實這些東西也是都有非常成熟的解決方案。 既然最近着手去解決這些事情,那麼也順便給還未了解的大夥普及一下。

這三篇其實已經輸出了,但是其實真正落地的時候,我考慮到加密算法的問題,我自己還是做了些許調整。

正文

我配置檔案裡面寫的參數是用的我們自己統一指定的加密算法,

是以解密的時候使用預設jasypt的解密是沒辦法成功的。

這時候我們就需要自己重寫 這個解密配置yml檔案參數的解析器了。

話不多說, 我們開始自定義起來。

先丢出一個我們自定義選擇的加解密工具類 AES的加密解密工具類:

MyEncryptUtil.java

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/10/29 16:52
 */
public class MyEncryptUtil {



    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }


    /**
     * 1.0 aes-128-gcm 加密
     *
     * @param originalData 為需加密資訊
     * @param key 32位的16進制key
     * @return
     */
    public static String encrypt(String originalData, String key) {
        try {
            //修改添加字元集
            byte[] sSrc = originalData.getBytes("UTF-8");
            byte[] sKey = MyEncryptUtil.parseHexStr2Byte(key);
            SecretKeySpec skeySpec = new SecretKeySpec(sKey, "AES");
            Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            //這邊是擷取一個随機的iv 預設為12位的
            byte[] iv = cipher.getIV();
            //執行加密
            byte[] encryptData = cipher.doFinal(sSrc);
            //這邊進行拼湊 為 iv + 加密後的内容
            byte[] message = new byte[12 + sSrc.length + 16];
            System.arraycopy(iv, 0, message, 0, 12);
            System.arraycopy(encryptData, 0, message, 12, encryptData.length);

            return Base64.getEncoder().encodeToString(message);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 1.0 aes-128-gcm 解密
     *
     * @param encryptData 已加密過的資料
     * @param key  32位的16進制key
     * @return
     */
    public static String decrypt(String encryptData, String key) {
        try {
            byte[] sSrc = Base64.getDecoder().decode(encryptData);
            byte[] sKey = MyEncryptUtil.parseHexStr2Byte(key);

            GCMParameterSpec iv = new GCMParameterSpec(128, sSrc, 0, 12);
            Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");
            SecretKey key2 = new SecretKeySpec(sKey, "AES");

            cipher.init(Cipher.DECRYPT_MODE, key2, iv);

            //這邊和nodejs不同的一點是 不需要移除後面的16位
            byte[] decryptData = cipher.doFinal(sSrc, 12, sSrc.length - 12);

            return new String(decryptData);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
}      

ok,現在我們使用我們的這個加密工具對一些資料進行加密:

Springboot yml配置參數加密 ,jasypt自定義解密器(拓展篇)

 加密後的結果 :

Springboot yml配置參數加密 ,jasypt自定義解密器(拓展篇)

直接替換掉yml配置檔案裡面的資料:

注意了,既然是自定義,那麼我們甚至都不需要遵守jasypt的解析規則,我自己定義了一下

格式 : ENC# + 加密串

Springboot yml配置參數加密 ,jasypt自定義解密器(拓展篇)

這時候,就到了我們的自定義解析器編碼環節了:

Springboot yml配置參數加密 ,jasypt自定義解密器(拓展篇)

Encryptor.java :

import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/12/14 17:05
 */
@Component
public class Encryptor {
    @Bean(name="encryptablePropertyResolver")
    EncryptablePropertyResolver encryptablePropertyResolver(){
        return new MyEncryptPropertyResolver();
    }
}      

MyEncryptPropertyResolver.java :

import com.demo.easytest.util.MyEncryptUtil;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import org.springframework.stereotype.Component;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/12/14 16:56
 */
@Component
public class MyEncryptPropertyResolver implements EncryptablePropertyResolver {

    private static final String ENC_PREFIX="ENC#";

    @Override
    public String resolvePropertyValue(String value) {
        if (value!=null && value.startsWith(ENC_PREFIX)){
            String str=value.substring(0, value.indexOf("#"));
            String result=value.substring(str.length()+1);
            return MyEncryptUtil.decrypt(result,"551b789c25cfbe731f723eab5906de5c");
        }
       return value;
    }
}      

其實說白了就是基于jasypt 架構,我們自己改造了一下加解密算法,但是流程還是用人家的。

到這就已經完成了,如果想看看,可以直接debug,看看這個讀取解析:

Springboot yml配置參數加密 ,jasypt自定義解密器(拓展篇)

 然後正常跑起來,調用一個mysql查詢接口,可以看到解析正常,資料庫連接配接正常: