您好,欢迎访问一九零五行业门户网

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,下载完毕后,进行解压,里面包含升级方式。
以上就是spring boot怎么对敏感信息进行加解密的详细内容。
其它类似信息

推荐信息