博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java加密——Jasypt开源工具包
阅读量:4041 次
发布时间:2019-05-24

本文共 5213 字,大约阅读时间需要 17 分钟。

转载地址:https://blog.csdn.net/fanxiaobin577328725/article/details/51707882

jasypt开源项目主页

项目地址:http://www.jasypt.org/

下载地址:(SourceForge.net)https://sourceforge.net/projects/jasypt/files/

 Jasypt也即Java Simplified Encryption是Sourceforge.net上的一个开源项目。

Jasypt这个Java类包为开发人员提供一种简单的方式来为项目增加加密功能,包括:密码Digest认证,文本和对象加密,集成hibernate,Spring Security(Acegi)来增强密码管理。Jasypt开发团队推出了Java加密工具Jasypt 1.4,它可与Spring Framework、Hibernate和Acegi Security集成。

Jasypt 1.4的新特征包括:加密属性文件(encryptable properties files)、Spring Framework集成、加密Hibernate数据源配置、新的命令行工具、URL加密的Apache wicket集成以及升级文档。

根据Jasypt文档,该技术可用于加密任务与应用程序,例如加密密码、敏感信息和数据通信、创建完整检查数据的sums. 其他性能包括高安全性、基于标准的加密技术、可同时单向和双向加密的加密密码、文本、数字和二进制文件。Jasypt也可以与Acegi Security整合也即Spring Security。Jasypt亦拥有加密应用配置的集成功能,而且提供一个开放的API从而任何一个Java Cryptography Extension都可以使用Jasypt。

  Jasypt还符合RSA标准的基于密码的加密,并提供了无配置加密工具以及新的、高可配置标准的加密工具。

1.简单示例

[java]   
  1. import org.jasypt.util.text.BasicTextEncryptor;  
  2. import org.jasypt.util.text.StrongTextEncryptor;  
  3.   
  4. public class EncypterTest {  
  5.   
  6.     public static void main(String[] args) {  
  7.         // 加密  
  8.         BasicTextEncryptor textEncryptor = new BasicTextEncryptor();  
  9.         textEncryptor.setPassword("password");  
  10.         String newPassword = textEncryptor.encrypt("123456");  
  11.         System.out.println(newPassword);  
  12.         // 解密  
  13.         BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor();  
  14.         textEncryptor2.setPassword("password");  
  15.         String oldPassword = textEncryptor2.decrypt(newPassword);  
  16.         System.out.println(oldPassword);  
  17.         System.out.println("--------------------------");  
  18.         /** 
  19.          * Utility class for easily performing high-strength encryption of 
  20.          * texts. This class internally holds a StandardPBEStringEncryptor 
  21.          * configured this way: Algorithm: PBEWithMD5AndTripleDES. Key obtention 
  22.          * iterations: 1000. The required steps to use it are: Create an 
  23.          * instance (using new). Set a password (using setPassword(String)). 
  24.          * Perform the desired encrypt(String) or decrypt(String) operations. To 
  25.          * use this class, you may need to download and install the Java 
  26.          * Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy 
  27.          * Files. This class is thread-safe. 
  28.          */  
  29.         StrongTextEncryptor ste = new StrongTextEncryptor();  
  30.         // 加密  
  31.         ste.setPassword("password");  
  32.         String encyptedResult = ste.encrypt("123456");  
  33.         System.out.println("encyptedResult:" + encyptedResult);  
  34.         // 解密  
  35.         String dencyptedResult = ste.decrypt(encyptedResult);  
  36.         System.out.println(dencyptedResult);  
  37.   
  38.     }  
  39. }  
  40. /* 
  41. 加解密BasicTextEncryptor时正常,但是加解密 StrongTextEncryptor 时,提示缺少jce 
  42. Java中安全组件被分成了两部分: 不含加密功能的JCA(Java Cryptography Architecture )和含加密功能的JCE(Java Cryptography Extension)。 
  43. 需要重新下载相应版本的jce进行安装。 
  44. */  

2.jce(Java Cryptography Extension)

由于受美国的密码出口条例约束,Java中涉及加解密功能的API被限制出口,所以Java中安全组件被分成了两部分: 不含加密功能的JCA(Java Cryptography Architecture )和含加密功能的JCE(Java Cryptography Extension)。

jce的详解请参考——

jre\lib\security目录下的local_policy.jar和US_export_policy.jar这两个文件起着至关重要的作用。

通常我们下载的jdk安装后,这两个文件都是2.4k左右,事实上无JCE限制的应该是5k左右。

jce的安装:

首先要下载相应的jce版本。

JDK8对应的版本下载链接:

JDK7对应的版本下载链接:

JDK早期版本的下载链接:

下载对应版本的jce后就把本地的jdk备份了一下(以防出现问题),然后替换了local_policy.jar和US_export_policy.jar。其实我下载的反而比原来的还小,但是替换后可以正常使用。

3.示例二

[java]   
  1. //加密  
  2. import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;  
  3. import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;  
  4.   
  5. /** 
  6.  *把密文放到配置文件中的时候要注意: 
  7.  * ENC(密文) 
  8.  * @author 杨尚川 
  9.  */  
  10. public class ConfigEncryptUtils {  
  11.     public static void main(String[] args){  
  12.         //加密工具  
  13.         StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();  
  14.         //加密配置  
  15.         EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();  
  16.         config.setAlgorithm("PBEWithMD5AndDES");  
  17.         //自己在用的时候更改此密码  
  18.         config.setPassword("apdplat");  
  19.         //应用配置  
  20.         encryptor.setConfig(config);  
  21.         String plaintext="root";  
  22.         //加密  
  23.         String ciphertext=encryptor.encrypt(plaintext);  
  24.         System.out.println(plaintext + " : " + ciphertext);  
  25.     }  
  26. }  
运行输出结果如下:
root : azL9Cyp9H62r3eUgZ+TESw==

注意:每次运行出现的加密结果是完全不同的,但是解密出来是一样的。

[java]   
  1. //解密  
  2. import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;  
  3. import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;  
  4.   
  5. /** 
  6.  *把密文放到配置文件中的时候要注意: 
  7.  * ENC(密文) 
  8.  * @author 杨尚川 
  9.  */  
  10. public class ConfigEncryptUtils {  
  11.     public static void main(String[] args){  
  12.         //加密工具  
  13.         StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();  
  14.         //加密配置  
  15.         EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();  
  16.         config.setAlgorithm("PBEWithMD5AndDES");  
  17.         //自己在用的时候更改此密码  
  18.         config.setPassword("apdplat");  
  19.         //应用配置  
  20.         encryptor.setConfig(config);  
  21.         String ciphertext="azL9Cyp9H62r3eUgZ+TESw==";  
  22.         //解密  
  23.         String plaintext=encryptor.decrypt(ciphertext);  
  24.         System.out.println(ciphertext + " : " + plaintext);  
  25.     }  
  26. }  
运行输出结果如下:
azL9Cyp9H62r3eUgZ+TESw== : root
从上面我们可以看到,
加密和解密的代码的唯一差别是encrypt和decrypt。

4.与Spring整合

参考资料:

********************************************************************************结束语********************************************************************************************

  我在写这篇博客的时候也是一名初学者,有任何疑问或问题请留言,或发邮件也可以,邮箱为:577328725@qq.com,我会尽早的进行更正及更改。
在我写过的博客中有两篇博客是对资源的整理,可能对大家都有帮助,大家有兴趣的话可以看看!!
下载资料整理——目录
  这篇博客里面是我关于我见到的感觉不错的好资源的整理,里面包含了书籍及源代码以及个人搜索的一些资源,如果有兴趣的可以看看,我会一直对其进行更新和添加。
优秀的文章&优秀的学习网站之收集手册
  这篇博客里面是我对于我读过的,并且感觉有意义的文章的收集整理,纯粹的个人爱好,大家感觉有兴趣的可以阅读一下,我也会时常的对其进行更新。
********************************************************************************感谢********************************************************************************************

你可能感兴趣的文章
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
PaperDownloader——文献命名6起来
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>