SpringBoot结合jasypt进行配置加解密

一、前言

随着当前安全局势愈发严峻,各行业安全要求越来越高,本文重点介绍SpringBoot结合jasypt进行数据库等配置文件进行加解密的方法。

二、基础使用

2.1 引入依赖

	<dependency>
		<groupId>com.github.ulisesbocchio</groupId>
		<artifactId>jasypt-spring-boot-starter</artifactId>
		<version>3.0.5</version>
	</dependency>

2.2 增加配置

jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES  #指定解密算法
    password: wno704 #加密的密钥,自定义即可,必填项.
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
    property: # 自定义的前后缀标记,格式为ENC(加密结果),此处我们修改为 wno704-密文-end
      prefix: wno704-
      suffix: -end

2.3 获取加密密文

在本地maven仓库中找到 org/jasypt/jasypt/1.9.3 目录(根据版本不同,目录不同),然后执行下面命令:

加密

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=${password} algorithm=${algorithm} input=${input}

解密

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI password=${password} algorithm=${algorithm} input=${input}

password: 加密的salt,解密时还需要它,项目自定义
algorithm: 加密的类型,默认 PBEWithMD5AndDES
input: 待加密的明文
执行结果中 OUTPUT 下方的内容时明文对应的密文或者明文,如果是加密过程多次执行同一个命令 OUTPUT 输出的内容不同,但不影响。
实际执行如下:
b686a8de-fddf-4e56-98d7-1b6fae989f5e.png#487px #460px

三、进阶使用

加解密工具JasyptUtil.java

package com.wno704.system.util;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 * @CalssName JasyptUtil
 * @Description TODO
 * @Author wno704
 * @Date 2024/10/14 下午3:22
 * @Version 1.0
 */
public class JasyptUtil {
    /**
     * 加密
     *
     * @param plaintext 明文
     * @return
     */
    public static String encrypt(String plaintext) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        // 指定算法
        config.setAlgorithm("PBEWithMD5AndDES");
        // 指定秘钥,和yml配置文件中保持一致
        config.setPassword("wno704");
        encryptor.setConfig(config);
        // 生成加密数据
        return encryptor.encrypt(plaintext);
    }

    /*
     * @Description: 加密
     * @Param: [algorithm, password, plaintext]
     * @return: java.lang.String
     * @Author: wno704
     * @Date: 2024/10/14
     */
    public static String encrypt(String algorithm,String password,String plaintext) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        // 指定算法
        config.setAlgorithm(algorithm);
        // 指定秘钥,和yml配置文件中保持一致
        config.setPassword(password);
        encryptor.setConfig(config);
        // 生成加密数据
        return encryptor.encrypt(plaintext);
    }

    /**
     * 解密
     *
     * @param data 加密后数据
     * @return
     */
    public static String decrypt(String data) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("wno704");
        encryptor.setConfig(config);
        // 解密数据
        return encryptor.decrypt(data);
    }

    /**
     * 解密
     *
     * @param data 加密后数据
     * @return
     */
    public static String decrypt(String algorithm,String password,String data) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(algorithm);
        config.setPassword(password);
        encryptor.setConfig(config);
        // 解密数据
        return encryptor.decrypt(data);
    }

    /*
     * @Description: 字符串前后缀取消
     * @Param: [prefix, suffix, str]
     * @return: java.lang.String
     * @Author: wno704
     * @Date: 2024/10/14
     */
    public static String dealStrFix(String prefix, String suffix, String str){
        String reStr = str;
        if (str.indexOf(prefix)==0){
            reStr = str.replaceFirst(prefix, "");
        }
        if (reStr.lastIndexOf(suffix) == reStr.length() - suffix.length()) {
            reStr = reStr.substring(0, reStr.length() - suffix.length());
        }
        return reStr;
    }

    /*
     * @Description: 是否符合前后缀要求
     * @Param: [prefix, suffix, str]
     * @return: java.lang.String
     * @Author: wno704
     * @Date: 2024/10/14
     */
    public static boolean isContainFix(String prefix, String suffix, String str){
        String reStr = str;
        if (str.indexOf(prefix)==0){
            reStr = str.replaceFirst(prefix, "");
        }
        if (reStr.lastIndexOf(suffix) == reStr.length() - suffix.length()) {
            reStr = reStr.substring(0, reStr.length() - suffix.length());
        }
        return reStr.length()+prefix.length()+suffix.length() == str.length();
    }
}

工具使用

    @Test
    public void testJasypt() throws Exception {
        String oldStr = "19920503";
        String enStr = JasyptUtil.encrypt(oldStr);
        System.out.println(enStr);
        System.out.println(JasyptUtil.decrypt(enStr));

        enStr = JasyptUtil.encrypt("PBEWithMD5AndDES","wno704",oldStr);
        System.out.println(enStr);
        System.out.println(JasyptUtil.decrypt("PBEWithMD5AndDES","wno704", enStr));

        String deStr = "wno7041-2hudACDDwno704-v5Wh-endFSZkTp5rFw==-end";
        String prefix = "wno704-";
        String suffix = "-end";

        System.out.println(JasyptUtil.isContainFix(prefix,suffix,deStr));
        System.out.println(JasyptUtil.dealStrFix(prefix,suffix,deStr));

        System.out.println(JasyptUtil.decrypt("PBEWithMD5AndDES","wno704", "jWKJXVnYWwB51ftdWVKYTx2IjqQeuWMy"));
    }

posted on 2026-08-10 15:27  爱河  阅读(1)  评论(0)    收藏  举报

导航