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.简单示例
- import org.jasypt.util.text.BasicTextEncryptor;
- import org.jasypt.util.text.StrongTextEncryptor;
-
- public class EncypterTest {
-
- public static void main(String[] args) {
-
- BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
- textEncryptor.setPassword("password");
- String newPassword = textEncryptor.encrypt("123456");
- System.out.println(newPassword);
-
- BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor();
- textEncryptor2.setPassword("password");
- String oldPassword = textEncryptor2.decrypt(newPassword);
- System.out.println(oldPassword);
- System.out.println("--------------------------");
-
-
-
-
-
-
-
-
-
-
-
- StrongTextEncryptor ste = new StrongTextEncryptor();
-
- ste.setPassword("password");
- String encyptedResult = ste.encrypt("123456");
- System.out.println("encyptedResult:" + encyptedResult);
-
- String dencyptedResult = ste.decrypt(encyptedResult);
- System.out.println(dencyptedResult);
-
- }
- }
-
-
-
-
-
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.示例二
-
- import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
- import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
-
-
-
-
-
-
- public class ConfigEncryptUtils {
- public static void main(String[] args){
-
- StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
-
- EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
- config.setAlgorithm("PBEWithMD5AndDES");
-
- config.setPassword("apdplat");
-
- encryptor.setConfig(config);
- String plaintext="root";
-
- String ciphertext=encryptor.encrypt(plaintext);
- System.out.println(plaintext + " : " + ciphertext);
- }
- }
运行输出结果如下:root : azL9Cyp9H62r3eUgZ+TESw== 注意:每次运行出现的加密结果是完全不同的,但是解密出来是一样的。
-
- import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
- import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
-
-
-
-
-
-
- public class ConfigEncryptUtils {
- public static void main(String[] args){
-
- StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
-
- EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
- config.setAlgorithm("PBEWithMD5AndDES");
-
- config.setPassword("apdplat");
-
- encryptor.setConfig(config);
- String ciphertext="azL9Cyp9H62r3eUgZ+TESw==";
-
- String plaintext=encryptor.decrypt(ciphertext);
- System.out.println(ciphertext + " : " + plaintext);
- }
- }
运行输出结果如下:azL9Cyp9H62r3eUgZ+TESw== : root 从上面我们可以看到,
加密和解密的代码的唯一差别是encrypt和decrypt。 4.与Spring整合
参考资料:
********************************************************************************结束语********************************************************************************************
我在写这篇博客的时候也是一名初学者,有任何疑问或问题请留言,或发邮件也可以,邮箱为:577328725@qq.com,我会尽早的进行更正及更改。
在我写过的博客中有两篇博客是对资源的整理,可能对大家都有帮助,大家有兴趣的话可以看看!!
下载资料整理——目录:
这篇博客里面是我关于我见到的感觉不错的好资源的整理,里面包含了书籍及源代码以及个人搜索的一些资源,如果有兴趣的可以看看,我会一直对其进行更新和添加。
优秀的文章&优秀的学习网站之收集手册:
这篇博客里面是我对于我读过的,并且感觉有意义的文章的收集整理,纯粹的个人爱好,大家感觉有兴趣的可以阅读一下,我也会时常的对其进行更新。
********************************************************************************感谢********************************************************************************************