今天需要讀取一個properties文件,裏面存取用戶名和密碼,但了爲安全起見,採取加密的方式,在網上搜了了一下,發現有幾種方式,但個人覺得簡單的還是用下面的開源jar來實現。最新版本已經發布到了1.7了。
Jasypt這個Java類包爲開發人員提供一種簡單的方式來爲項目增加加密功能,包括:密碼Digest認證,文本和對象加密,集成hibernate,Spring Security(Acegi)來增強密碼管理。Jasypt開發團隊推出了Java加密工具Jasypt 1.4,它可與Spring Framework、Hibernate和Acegi Security集成。
與項目有關的一位開發者表示,Jasypt是一個Java庫,可以使開發者不需太多操作來給Java項目添加基本加密功能,而且不需要知道加密原理。
Jasypt也即Java Simplified Encryption是Sourceforge.net上的一個開源項目。在當地時間11月23號的通告中,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標準的基於密碼的加密,並提供了無配置加密工具以及新的、高可配置標準的加密工具。
jasypt開源項目主頁
項目地址:http://www.jasypt.org/
- 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("--------------------------");
- /**
- * Utility class for easily performing high-strength encryption of texts.
- * This class internally holds a StandardPBEStringEncryptor configured this way:
- * Algorithm: PBEWithMD5AndTripleDES.
- * Key obtention iterations: 1000.
- * The required steps to use it are:
- * Create an instance (using new).
- * Set a password (using setPassword(String)).
- * Perform the desired encrypt(String) or decrypt(String) operations.
- * To use this class, you may need to download and install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.
- * This class is thread-safe.
- */
- 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);
- }
- }
- //NbxTTz53iW0d1GUphknPqg==