jasypt-spring-boot-starter3.x报错

1.报错截图

2.原因分析

Jasypt 3.0以上版本解密默认使用 PBEWITHHMACSHA512ANDAES_256算法

Jasypt 3.0以下版本解密默认使用PBEWithMD5AndDES算法

3.解决方法

3.1加密解密

加密和解密算法使用用一种算法(PBEWithMD5AndDES)

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

public class JasyptUtils {

    /**
     * 加密
     *
     * @param plaintext 明文
     * @return 密文
     */
    public static String encrypt(String plaintext) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        // 指定算法
        config.setAlgorithm("PBEWithMD5AndDES");
        // 指定秘钥,和jvm的jasypt.encryptor.password保持一致
        config.setPassword("xxxxxxxx");
        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");
        // 指定秘钥,和jvm的jasypt.encryptor.password保持一致
        config.setPassword("xxxxxxxx");
        encryptor.setConfig(config);
        // 解密数据
        return encryptor.decrypt(data);
    }

    public static void main(String[] args) {
        System.out.println(encrypt("密码"));
        System.out.println( decrypt("AXprrFFdFjdWt9yCvqnC9OVqYpWTehur"));
    }

}

3.2yml配置

springboot3.xiv-generator-classname和algorithm需要全部配置上

jasypt:
  encryptor:
#password  本地环境将密钥放入yml文件中,生产需要jvm携带 -Djasypt.encryptor.password=xxxxx
    password: xxxxx
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值