[Java加密] Jasypt加密工具

博客总结了Java加密工具相关内容,介绍了开源加密工具包jasypt - spring - boot及其依赖,给出基础Demo代码,还提到使用256进行AES加密时需更新相关文件,对于加密算法限制问题,指出更新包可解决,并提供JCE下载地址和Oracle协议地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

前段时间研究加密工具. 怎么也想不起来, 最早的加密算法是怎么搞的. 今天把这部分简单总结一下.


加密工具包&相关依赖

开源项目地址: https://github.com/ulisesbocchio/jasypt-spring-boot

最新版本的加密工具需要JDK1.8及其以上版本才可以兼容.

# 官方教程中 单引包教程
  <dependencies>
  	  <dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>3.0.3</version>
	 </dependency>
  </dependencies>

基础Demo代码

/**
*
* Jasypt 加密工具类初次使用。
* 
* Author: sean
* Date:Apr 22, 202111:51:32 PM
*/

public class QuickStart {
	
	public static final StringEncryptor encryptor = stringEncryptor(); 
	
	 public static StringEncryptor stringEncryptor() {
	        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
	        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
	        config.setPassword("password");// 密钥 - SALT
	       // config.setAlgorithm("PBEWithMD5AndDES"); // 设置的加密算法
		    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;
	    }
	
	 public static void main(String[] args) {
		 String encrypt = encryptor.encrypt("HLLLLLLLLLLL");
		 System.out.println("ENCRYPT: "+ encrypt);
		 String decrypt = encryptor.decrypt(encrypt);
		 System.out.println("ENCRYPT: "+ decrypt);

	}
	
}

特别注意, 如果你需要使用PBEWITHHMACSHA512ANDAES_256, 也就是256进行加密的时候. 需要更新你的$Java_Home/jre/lib/security下的local_policy.jarUS_export_policy.jar文件.

在这里插入图片描述


Q&A

Exception in thread "main" >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
	at >org.jasypt.encryption.pbe.StandardPBEByteEncryptor.handleInvalidKeyException(StandardPBEByteEncryptor.java:1207)
	at >org.jasypt.encryption.pbe.StandardPBEByteEncryptor.encrypt(StandardPBEByteEncryptor.java:996)
	at >org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:655)
	at >org.jasypt.encryption.pbe.PooledPBEStringEncryptor.encrypt(PooledPBEStringEncryptor.java:465)
	at com.yanxml.arsenal.java.encrypt.jasypt.QuickStart.main(QuickStart.java:35)

这里说的是你加密算法需要有限制. 这和上方的提示一样. 更新包即可解决, 此处和当地的外贸出口协议有关系.

(JCE下载地址)Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files Download
在这里插入图片描述

https://www.oracle.com/java/technologies/java-archive-downloads-java-plat-downloads.html#jce_policy-1.5.0-oth-JPR
在这里插入图片描述
Oracle协议地址Oracle Binary Code License Agreement for the Java SE Platform Products and JavaFX

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值