jasypt加密解密

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

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小辰~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值