文章目錄
- 官網
- 功能概述
- Code
- 附

官網
http://www.jasypt.org/
功能概述
Jasypt 是一個 Java 庫,它允許開發人員以最小的努力添加基本的加密功能,并且不需要深入了解密碼學的工作原理。
- 高安全性、基于標準的加密技術,適用于單向和雙向加密。加密密碼、文本、數字、二進制文件…
- 與 Hibernate 的透明集成
- 適合集成到基于 Spring 的應用程序中,也可以與 Spring Security 透明集成
- 用于加密應用程序配置(即數據源)的集成功能
- 多處理器/多核系統中高性能加密的特定功能
- 開放 API,可與任何 JCE 提供商一起使用
Code
<dependency><groupId>com.iluwatar</groupId><artifactId>jaspyt</artifactId><version>1.9.3</version>
</dependency>
package com.artisan.jasypt.javaway;import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;import java.security.SecureRandom;/*** @author 小工匠* @version 1.0* @mark: show me the code , change the world** http://www.jasypt.org/download.html*/
public class JasyptExample {/*** 程序的主入口函數。* 該函數不接受參數,也不返回任何值。* 依次調用了以下幾個示例函數:* 1. basicExample - 展示基本示例。* 2. oneWayPasswordExample - 展示使用一次性密碼的示例。* 3. changeAlgorithmExample - 展示改變算法的示例。* 4. multiThreadDecryptExample - 展示多線程解密的示例。*/public static void main(String[] args) {basicExample();oneWayPasswordExample();changeAlgorithmExample();multiThreadDecryptExample();}/*** 生成一個安全的隨機密碼。* 該函數不接受任何參數。** @return 返回一個由隨機字符組成的密碼字符串。密碼由大寫字母、小寫字母和數字組成,長度為16個字符。*/private static String generateSecurePassword() {// 創建一個安全隨機數生成器SecureRandom random = new SecureRandom();// 定義密碼可能包含的字符集char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();// 使用StringBuilder來構建密碼字符串StringBuilder passwordBuilder = new StringBuilder();// 循環生成16個隨機字符for (int i = 0; i < 16; i++) {// 從字符集中隨機選擇一個字符,并將其添加到密碼字符串中passwordBuilder.append(chars[random.nextInt(chars.length)]);}System.out.println("key:" + passwordBuilder);// 返回構建好的密碼字符串return passwordBuilder.toString();}/*** 簡單文本加密示例* 該方法演示了如何使用BasicTextEncryptor對文本進行加密和解密。* 該示例不接受參數,也不返回值。*/protected static void basicExample() {BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();// 使用動態生成的密鑰,提高安全性String securePassword = generateSecurePassword();basicTextEncryptor.setPassword(securePassword);try {// 加密文本信息String encryptedText = basicTextEncryptor.encrypt("artisan*");System.out.println("encryptedText:" + encryptedText);// 解密已加密的文本信息String decryptedText = basicTextEncryptor.decrypt(encryptedText);System.out.println("decryptedText:" + decryptedText);} catch (Exception e) {// 處理加密/解密過程中可能出現的異常System.err.println("Error during encryption or decryption: " + e.getMessage());}}/*** 展示單向密碼加密的示例。* <p>* 兩種方案:* 一種方案是把數據庫中的密文解密成明文,再與用戶輸入的密碼進行對比;* 另一種方案是把用戶輸入的密碼進行加密,把加密后的密文與數據庫的密文進行對比。* <p>* 第二種方案是更合理的,一方面是因為加密比解密更容易,性能更好;* 另一方面是減少明文出現的次數,保證安全性。* 第二種方案完全不需要解密,所以我們只需要單向地密碼加密便可以了*/private static void oneWayPasswordExample() {// 創建BasicPasswordEncryptor實例用于密碼加密和驗證BasicPasswordEncryptor encryptor = new BasicPasswordEncryptor();// 加密密碼 "artisan*"String encryptedPassword = encryptor.encryptPassword("artisan*");// 檢查密碼 "artisan*" 是否與加密后的密碼匹配,并打印結果System.out.println(encryptor.checkPassword("artisan*", encryptedPassword));// 檢查密碼 "Artisan*" (大寫) 是否與加密后的密碼匹配,并打印結果,預期為不匹配System.out.println(encryptor.checkPassword("Artisan*", encryptedPassword));}/*** 該示例演示如何改變加密算法。* 該方法不接受參數且無返回值。* 主要步驟包括創建加密器、設置密碼和算法、加密數據以及解密數據。* <p>* 自定義地使用不同的算法進行加密解密*/private static void changeAlgorithmExample() {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();System.out.println("---------");// 設置加密器的密碼//encryptor.setPassword(generateSecurePassword());encryptor.setPassword("rQIVmVRhL7Zr2Kmu");// 設置加密器使用的加密算法encryptor.setAlgorithm("PBEWithMD5AndTripleDES");// 使用加密器對文本進行加密String encryptedText = encryptor.encrypt("artisan*");System.out.println("encryptedText:" + encryptedText);// 使用加密器對加密后的文本進行解密String decryptedText = encryptor.decrypt(encryptedText);System.out.println("decryptedText:" + decryptedText);}/*** 多線程解密示例* 該方法演示了如何使用多線程進行加密和解密操作。* 注意:該方法不接受任何參數,也不返回任何值。* <p>* Jasypt提供了多線程解密操作,可以并行解密,這樣可以提供更好的性能。一般建議可以設置與機器處理器核數一致的線程數進行解密*/private static void multiThreadDecryptExample() {// 創建并配置加密器,使用池化技術以支持多線程加密PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();// 設置線程池大小為6,即同時最多有6個線程執行加密操作encryptor.setPoolSize(6);// 設置加密使用的密碼encryptor.setPassword(generateSecurePassword());// 設置加密算法encryptor.setAlgorithm("PBEWithMD5AndTripleDES");// 加密明文字符串String encryptedText = encryptor.encrypt("artisan*#*");System.out.println("encryptedText:" + encryptedText);// 解密密文字符串String decryptedText = encryptor.decrypt(encryptedText);System.out.println("decryptedText:" + decryptedText);}}
- generateSecurePassword: 生成一個16字符長的安全隨機密碼。
- basicExample: 使用
BasicTextEncryptor
對文本進行加密和解密。 - oneWayPasswordExample: 使用
BasicPasswordEncryptor
進行單向密碼加密和驗證。 - changeAlgorithmExample: 使用
StandardPBEStringEncryptor
并設置自定義算法進行加密和解密。 - multiThreadDecryptExample: 使用
PooledPBEStringEncryptor
進行多線程的加密和解密操作。
附
Java 加密體系 (JCA)