AI编程助手
AI免费问答

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

PHPz   2023-05-10 14:46   1440浏览 转载

我们使用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

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

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

其中:

input:明文密码
password:要加的盐,秘钥
algorithm:加密算法

加密算法包括以下几种:

PBEWITHHMACSHA1ANDAES_128PBEWITHHMACSHA1ANDAES_256PBEWITHHMACSHA224ANDAES_128PBEWITHHMACSHA224ANDAES_256PBEWITHHMACSHA256ANDAES_128PBEWITHHMACSHA256ANDAES_256PBEWITHHMACSHA384ANDAES_128PBEWITHHMACSHA384ANDAES_256PBEWITHHMACSHA512ANDAES_128PBEWITHHMACSHA512ANDAES_256PBEWITHMD5ANDDESPBEWITHMD5ANDTRIPLEDESPBEWITHSHA1ANDDESEDEPBEWITHSHA1ANDRC2_128PBEWITHSHA1ANDRC2_40PBEWITHSHA1ANDRC4_128PBEWITHSHA1ANDRC4_40

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

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

声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除