一、项目搭建
1.新建一个springBoot项目
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiao</groupId> <artifactId>springboot-demo001</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-demo001</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.配置文件application.yml
jasypt: encryptor: password: Octv_DL1218
3.创建测试类
@RunWith(SpringRunner.class) @SpringBootTest public class JasyptPBEStringEncryptionUtil { @Resource private StringEncryptor stringEncryptor; @Test public void test(){ String root=stringEncryptor.encrypt("39.106.57.234"); System.out.println(root); System.out.println(stringEncryptor.decrypt("VR23Ty8MRL/i8WmhM6sBK0JSqsUDTAtpgxaEO3CkoSd6BPb6spIp0+Xfjn0xdDBf")); } }
运行测试类即可
遇到的问题:
运行后老是出现错误:
org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
网上的解决方案是jdk8要下载
,然后把里面的2个jar包替换到D:\java\jdk1.8\jre\lib\security即可,但是我替换之后还是报一样的错误
后面怀疑没有生效,重启电脑和重启jvm都行
最后换了jdk8的最新版本就可以了
我电脑jdk最开始版本是:
此jdk默认会限制秘钥的长度导致报上述错误
重新下载了jdk8最新版的
此jdk默认不会限制秘钥的长度报上述错误
在默认情况下,jdk8u361 AES加解密密钥长度不限制,使用的是jre/lib/security/unlimited目录中的策略文件。其策略机制为:
如果java.security文件中配置了crypto.policy,会使用配置的机制,只允许两个值limited | unlimited,如果配置错误,则会报异常;
如果java.security文件中没有配置crypto.policy,那么会像jdk之前版本一样,加载jre/lib/security目录下的local_policy.jar, US_export_policy.jar,并使用其代表的策略;
如果java.security文件中没有配置crypto.policy并且jre/lib/security目录下没有local_policy.jar, US_export_policy.jar,那么会启用默认不限制密钥长度机制。