github地址:?https://github.com/ulisesbocchio/jasypt-spring-boot#update-11242019-version-300-release-includes
基于JDK8,?
pom.xml依赖
<dependency> ? ? <groupId>com.github.ulisesbocchio</groupId> ? ? <artifactId>jasypt-spring-boot-starter</artifactId> ? ? <version>3.0.4</version> </dependency>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>${jasypt}</version>
</dependency>
工具类:
核心注意:??PBEWITHHMACSHA512ANDAES_256? ? 3.0.0上默认使用此加密算法, ,?
? ? ? ? ? ? 使用旧的算法会解密失败。
旧的解密失败解决方法
jasypt: ? encryptor: ? ? algorithm: PBEWithMD5AndDES ? ? iv-generator-classname: org.jasypt.iv.NoIvGenerator
新的加密算法:
package com.glo.task.utils;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.util.text.AES256TextEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.UnsupportedEncodingException;
public class JasyptUtil {
public static final StringEncryptor encryptor = stringEncryptor();
public static void main(String[] args) throws UnsupportedEncodingException {
String account = "root";
String password = "123456";
String url= "jdbc:mysql://xxx:xxx/xxx?characterEncoding=utf8&connectTimeout=5000&socketTimeout=5000&autoReconnect=true&useUnicode=true&useSSL=false";
//秘钥
String my = "xxxx";
//密码进行加密
System.out.println("加密后账号:" + encrypt(account, my));
System.out.println("加密后密码:" + encrypt(password, my));
System.out.println("加密后url:" + encrypt(url, my));
//解密
System.out.println("解密后账号:" + decrypt("i6+yH+laK8qvOnJS47vL3UAvB11wrRgyGTBKZAHY7UxsG0lKiqVaK/R7M5EhFsU9", my));
System.out.println("解密后密码:" + decrypt("uHPu/flryc5x9bkTsExTqAii7u3pVRf9VWxgOQNyCBSB6mqP5vcOOWo9ukYR4RXJ", my));
System.out.println("解密后url:" + decrypt("lI8FhCUR1kZ+PhnSKu5qrds2v7sHRrfgk8RU1+LGypPqexWy85iusHCtnpU/iBHg7WY+4pVfstxv5+L9T8uqmjEJsSgFfeQVWA0xnu+cDdrzNywdPdpchYsj6BpVxZ5N2JfHr0i9wyh/xSx9MivX/NsqhV5rSXpmDuVauJYCcfTWNLHVBcmF2dNSy3IoLoH5R5P5fUhJNBDJAmioyQ7W2LxMzP+4m8YRgI0Znf8fvuc=", my));
}
public static String encrypt(String source, String my) throws UnsupportedEncodingException {
return encryptor.encrypt(source);
}
public static String decrypt(String source, String my) throws UnsupportedEncodingException {
return encryptor.decrypt(source);
}
public static StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("password");
config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
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);
return encryptor;
}
}
截图:
数据库连接使用, 将ENC(上面生成的密钥)
如果旧的加密出现解密失败,?
或者? 出现如下图的问题, 使用 springtest调试会出现此问题, application启动则不会出现
Failed to bind properties under 'server.ssl.key-password' to java.lang.String:
解决方法: 使用旧的加密算法, 加密时也需要用此加密算法加密生成加密串
jasypt: ? encryptor: ? ? algorithm: PBEWithMD5AndDES ? ? iv-generator-classname: org.jasypt.iv.NoIvGenerator
JCE 安装:
https://www.oracle.com/java/technologies/javase-jce8-downloads.html
|