【加密解密】明文加密解密-已实现【已应用】

目录:

一、加载jasypt包。

二、jasypt配置、数据库配置

三、项目正常运行

四、加密解密方法

五、报错及解决方法

具体内容:

一、加载jasypt包

gradle配置:

implementation group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.4'

或者:pom.xml文件:

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

二、jasypt配置、数据库配置

application.properties文件:

#123wes为自定义加密盐:(jasypt配置一定要放最上面)
jasypt.encryptor.password=123wes 
#加密方式  (切勿修改,若修改:同加密方法一并修改。)
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
jasypt.encryptor.algorithm=PBEWithMD5AndDES

#数据连接配置:
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/databasesName?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username= ENC(Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6)
spring.datasource.password= ENC(dfCDWWWEEgs4YXDDFKKK9kGW)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

或者:yml文件:

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

#数据库配置:
spring:
	datasource:
	jdbc-url:mysql://localhost:3306/databasesName?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
	username: ENC(Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6)
	password: ENC(dfCDWWWEEgs4YXDDFKKK9kGW)
	driver-class-name:com.mysql.cj.jdbc.Driver

三、正常运行

至此;点击Run运行项目。可正常运行。

四、加密解密方法

用户密码加密解密 :(注意:仅用来获取密码加密字符串,之后去掉,打包时不打此文件。)
TestJasypt.java文件:

package xxx;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class TestJasypt {
 //获取密文:
    public static void main(String[] args) {
        // 加密
        //password为自定义:加密盐,value为需要加密的字符串
        System.out.println(setJasypt("123wes", "root@667"));
        // 解密
        password为加密盐,value为需要解密的:加密密文。
        System.out.println(getJasypt("123wes", "Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6"));
    }

    /* 输出:密文:
    Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6
    //  放入配置文件如:password: ENC(Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6)
    解密后:
    root@667
    */

    //注意:发布时,不加载此文件。
    public static String setJasypt(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(ssPBEC(password));
        String result = encryptOr.encrypt(value);
        return result;
    }
    public static String getJasypt(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(ssPBEC(password));
        String result = encryptOr.decrypt(value);
        return result;
    }
    public static SimpleStringPBEConfig ssPBEC(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName(null);
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }
}

五、报错及解决方法

报错:Failed to bind properties under ‘spring.datasource.password’ to java.lang.String:

《【加密解密】明文加密解密-已实现【已应用】》

 

详细 简单明了 的解决方法

相关文章。

    原文作者:刘贵庆
    原文地址: https://blog.csdn.net/xysxlgq/article/details/126149585
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞