首页 > 文章列表 > spring boot怎么对敏感信息进行加解密

spring boot怎么对敏感信息进行加解密

springboot
141 2023-05-11

spring boot怎么对敏感信息进行加解密

我们使用jasypt最新版本对敏感信息进行加解密。

1.在项目pom文件中加入如下依赖:

   <dependency>

            <groupId>com.github.ulisesbocchio</groupId>

            <artifactId>jasypt-spring-boot-starter</artifactId>

            <version>3.0.3</version>

   </dependency>

2.创建加解密公用类:

package com.myproject.common.utils;

 

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;

import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

 

public class JasyptUtil {

    /*

     * textToEncrypt,需要加密的明文

     * salt,加密的盐,需要与解密保持一致

     * algorithm,加密算法,需要与解密算法保持一致

     */

    public static String encrypt(String textToEncrypt, String salt, String algorithm) {

        // 1. 创建加解密工具实例

        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();

        // 2. 加解密配置

        SimpleStringPBEConfig config = new SimpleStringPBEConfig();

        config.setPassword(salt);

        // 3. 加密算法,需要与解密算法一致

        config.setAlgorithm(algorithm);

        // 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置

        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);

        // 4. 加密

        return encryptor.encrypt(textToEncrypt);

    }

 

    /*

     * textToDecrypt,需要解密的密文

     * salt,解密的盐,需要与加密保持一致

     * algorithm,解密算法,需要与加密算法保持一致

     */

    public static String decrypt(String textToDecrypt, String salt, String algorithm){

        // 1. 创建加解密工具实例

        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();

        // 2. 加解密配置

        SimpleStringPBEConfig config = new SimpleStringPBEConfig();

        config.setPassword(salt);

        // 3. 解密算法,必须与加密算法一致

        config.setAlgorithm(algorithm);

        // 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置

        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);

        // 4. 解密

        return encryptor.decrypt(textToDecrypt);

    }

}

3.在对yml文件中的敏感信息进行加密除采用以上方法外,还可采用如下方法。

进入jasypt依赖目录,这里使用jasypt的最新版本jasypt-1.9.3.jar,运行cmd命令

java -cp  jasypt-1.9.3.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="mypassword" password=mypassword algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator

图片中OUTPUT下的密文即是加密后的密文,将其copy到配置文件即可,解密时使用2.中代码中进行解密即可。

其中:

input:明文密码

password:要加的盐,秘钥

algorithm:加密算法

加密算法包括以下几种:

PBEWITHHMACSHA1ANDAES_128

PBEWITHHMACSHA1ANDAES_256

PBEWITHHMACSHA224ANDAES_128

PBEWITHHMACSHA224ANDAES_256

PBEWITHHMACSHA256ANDAES_128

PBEWITHHMACSHA256ANDAES_256

PBEWITHHMACSHA384ANDAES_128

PBEWITHHMACSHA384ANDAES_256

PBEWITHHMACSHA512ANDAES_128

PBEWITHHMACSHA512ANDAES_256

PBEWITHMD5ANDDES

PBEWITHMD5ANDTRIPLEDES

PBEWITHSHA1ANDDESEDE

PBEWITHSHA1ANDRC2_128

PBEWITHSHA1ANDRC2_40

PBEWITHSHA1ANDRC4_128

PBEWITHSHA1ANDRC4_40

 4.如果使用jdk的版本是java1.8或者java8,那么需要升级一下对应jre和jdk

需要下载jce-policy-8.zip,下载完毕后,进行解压,里面包含升级方式。