Spring Boot Jasypt 3.0.4 报错---算法加解密使用不一致

大家好,我是神韵,是一个技术&生活博主。出文章目的主要是两个,一是好记忆不如烂笔头,记录总结中提高自己。二是希望我的文章可以帮到大家。欢迎来点赞打卡,你们的行动将是我无限的动力。
本篇主题是:Spring Boot Jasypt 3.0.4 报错---算法加解密使用不一致

目录

场景

错误描述如下

感悟

错误原因结论

错误分析

两种解决方案


场景

对yml中的数据库和密码进行密文加密,通过Jasypt技术实现,Jasypt是3.0版本及以上

spring:
  datasource:
    # 二、非嵌入式数据库配置--MySQL
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&useSSL=true&serverTimezone=GMT%2B8
    username: ENC(hEc7NPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)
    password: ENC(MaF3AwPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)

错误描述如下

Description:

Failed to bind properties under 'spring.datasource.password' to java.lang.String:

    Reason: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.password' to java.lang.String

Action:

Update your application's configuration

感悟

说下感悟,网上的文章认真看了一下,没有一篇是答到点子上,基本上都是抄来抄去,连验证一下估计都觉得累,无语死...

错误原因结论

造成这个原因是因为生成密文使用的算法和解密时使用的算法不一致。

出现这个错误意味着解密失败,造成这个错误的原因主要可能是算法用错了。因为Jasypt 3.0以上版本默认都是使用 PBEWITHHMACSHA512ANDAES_256算法,但是我们可能用了PBEWithMD5AndDES去加密得到密文,这样子自然会出现错误。

错误分析

也就是你可能用下面的代码默认是PBEWithMD5AndDES加密得到的密文,然后在3.0版本以上使用,算法不匹配~

public static void main(String[] args) {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        // 默认是PBEWithMD5AndDES加密
        // 盐值
        standardPBEStringEncryptor.setPassword("salt20220907");
        // 加密明文
        String encryptValue1 = standardPBEStringEncryptor.encrypt("shenyun");
        String encryptValue2 = standardPBEStringEncryptor.encrypt("shenyun20220907");
        // 解密密文
        String decryptValue = standardPBEStringEncryptor.decrypt("1PcipFvXoDg95eW3ZP1MOw==");
        System.out.println(encryptValue1);
        System.out.println(encryptValue2);
        System.out.println(decryptValue);
    }

两种解决方案

1、使用正确的代码生成密文,然后放到yml上就可以

    public static void main1(String[] args) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        // 加密方式
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        // 盐值
        config.setPassword("salt20220907");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        //config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        // 加密明文
        String encryptValue1 = encryptor.encrypt("shenyun");
        String encryptValue2 = encryptor.encrypt("shenyun20220907");
        // 解密密文
//        String decryptValue = encryptor.decrypt("MaF3AwiV/aXCCDMEx+nOgAtXreq62JMgRM+2M9NIpUe0vko3fahZ+IxnvocfaGnJ");
        System.out.println(encryptValue1);
        System.out.println(encryptValue2);
//        System.out.println(decryptValue);

    }

2、替换默认的算法为你生成密文的算法---PBEWithMD5AndDES

yml配置加上下面配置(password盐是通过vm传入的)

jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

当然,你还可以将你的版本降至3.0以下,因为3.0以下默认用的是PBEWithMD5AndDES算法。

结论:造成这个原因是因为生成密文使用的算法和解密时使用的算法不一致。


版权声明:本文为qq_41055045原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。