jasypt加密解密

1. 导入依赖

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

2. yml配置

导入依赖后,就可以进行jasypt的加密配置

# jasypt 配置加密
jasypt:
  encryptor:
    # 密码盐值(自定义)
    password: jasypt
    # 加密算法设置
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.RandomIvGenerator
    salt-generator-classname: org.jasypt.salt.RandomSaltGenerator

进行完上面操作,就可以在需要加密的地方进行配置了。

比如,配置数据库的账号密码,使用格式ENC(加密后的密码)进行加密。

username: ENC(vyxdS47pJdWBF38TFdmjKmMm4zEO0FQP)
password: ENC(vyxdS47pJdWBF38TFdmjKmMm4zEO0FQP)

但是如何获取这些加密值呢?接下来写一个加密解密的工具类。

3. 加密解密工具类

package com.example.util;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtils {

    /**
     * 加密
     * @param password 配置文件中设定的加密盐值
     * @param value 加密值
     * @return 加密后的字符串
     */
    public static String encrypt(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        return  encryptor.encrypt(value);
    }

    /**
     * 解密
     * @param password 配置文件中设定的加密盐值
     * @param value 解密密文
     * @return 解密后的字符串
     */
    public static String decrypt(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        return encryptor.decrypt(value);
    }

    /**
     * 配置,对应yml中的配置
     * @param password 盐值
     * @return SimpleStringPBEConfig
     */
    public static SimpleStringPBEConfig cryptor(String password){
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        //设置盐值
        config.setPassword(password);
        //设置算法
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }


    public static void main(String[] args) {
        // 加密
        String encryptStr = encrypt("jasypt", "root");
        // 解密
        String decryptStr = decrypt("jasypt", encryptStr);
        System.out.println("加密后:" + encryptStr);
        System.out.println("解密后:" + decryptStr);
    }
}

在main方法中执行可以得到加密前后的字符串

加密后:b1tEArsy2JkB3h29nD5qs9Kx1qdXYmK8
解密后:root

4. 测试一下

application.yml新加一个配置

hello:
  port: ENC(wqy7gE7IBhupjgdPI/FuOTwOQ9p1NAIf)

新建一个TestConfig.java来获取yml中的值

package com.example.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
public class TestConfig {
    @Value("${hello.port}")
    private String port;
}

创建测试类测试:

package com.example.jasypt;

import com.example.config.TestConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
@SpringBootTest
public class JasyptTests {
    @Resource
    private TestConfig testConfig;

    @Test
    void getPort(){
        System.out.println("testConfig.getPort() = " + testConfig.getPort());
    }
}

启动后输出:testConfig.getPort() = root

5.隐藏设置的盐值

那么问题来了,盐值肯定是不能被别人知道的。
去掉配置文件中设置的盐值,并在idea的启动配置项的“VM options”添加-Djasypt.encryptor.password=盐值
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-780l4p4S-1656068504206)(E:\要整理的笔记\new\image-20210508111230392.png)]

打包后启动方式:java -jar -Djasypt.encryptor.password=盐值 test.jar

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐