Use jasypt to encrypt and decrypt configuration in Spring Boot microservices

Records : 423

Scenario : In Spring Boot microservices, use jasypt-1.9.3 to encrypt and decrypt configuration.

Versions : JDK 1.8, Spring Boot 2.6.3, jasypt-1.9.3.

J asypt open source address : https://github.com/jasypt/jasypt

Jasypt: (Java Simplified Encryption) is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort and without the need of having deep knowledge on how cryptography works.

1. Introduce dependencies in pom.xml

<dependency>
  <groupId>org.jasypt</groupId>
  <artifactId>jasypt</artifactId>
  <version>1.9.3</version>
</dependency>

2.Jasypt core class

2.1Jasypt core tool class

When using Jasypt to complete encryption and decryption business scenarios, the following tool classes can be used.

TextEncryptor interface: org.jasypt.util.text.TextEncryptor.

BasicTextEncryptor implementation class: org.jasypt.util.text.BasicTextEncryptor.

AES256TextEncryptor implementation class: org.jasypt.util.text.AES256TextEncryptor.

StrongTextEncryptor implementation class: org.jasypt.util.text.StrongTextEncryptor.

2.2Jasypt core class

The specific implementation classes of BasicTextEncryptor, AES256TextEncryptor and StrongTextEncryptor operations are as follows:

org.jasypt.encryption.pbe.StandardPBEStringEncryptor

3. Corresponding algorithm of Jasypt core tool class

BasicTextEncryptor algorithm: PBEWithMD5AndDES.

AES256 Text Encryptor algorithm: PBEWithHMACSHA512AndAES_256.

StrongTextEncryptor algorithm: PBEWithMD5AndTripleDES.

4. Examples

4.1 Example of using BasicTextEncryptor

/**
 * 加密工具类: org.jasypt.util.text.BasicTextEncryptor
 * 加密算法: PBEWithMD5AndDES
 */
public static void f1_BasicTextEncryptor() {
  BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
  System.out.println("当前加密方式: 加密类: BasicTextEncryptor, 加密算法: PBEWithMD5AndDES ");
  // 1.设置秘钥
  String salt = "U3buwRJdQ2023";
  textEncryptor.setPassword(salt);
  // 2.加密
  // 2.1加密内容
  String pd = "Hangzhou20230427";
  System.out.println("加密前:  " + pd);
  // 2.2加密操作
  String pdAfterEncrypt = textEncryptor.encrypt(pd);
  System.out.println("加密后:  " + pdAfterEncrypt);
  // 3.解密操作
  String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
  System.out.println("解密后:  " + pdAfterDecrypt);
}

4.2 Example using AES256TextEncryptor

/**
 * 加密工具类: org.jasypt.util.text.AES256TextEncryptor
 * 加密算法: PBEWithHMACSHA512AndAES_256
 */
public static void f2_AES256TextEncryptor() {
    AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
    System.out.println("当前加密方式: 加密类: AES256TextEncryptor, 加密算法: PBEWithHMACSHA512AndAES_256 ");
    // 1.设置秘钥
    String salt = "U3buwRJdQ2023";
    textEncryptor.setPassword(salt);
    // 2.加密
    // 2.1加密内容
    String pd = "Hangzhou20230427";
    System.out.println("加密前:  " + pd);
    // 2.2加密操作
    String pdAfterEncrypt = textEncryptor.encrypt(pd);
    System.out.println("加密后:  " + pdAfterEncrypt);
    // 3.解密操作
    String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
    System.out.println("解密后:  " + pdAfterDecrypt);
}

4.3 Example using StrongTextEncryptor


/**
 * 加密工具类: org.jasypt.util.text.StrongTextEncryptor
 * 加密算法: PBEWithMD5AndTripleDES
 */
public static void f3_StrongTextEncryptor() {
    StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
    System.out.println("当前加密方式: 加密类: StrongTextEncryptor, 加密算法: PBEWithMD5AndTripleDES ");
    // 1.设置秘钥
    String salt = "U3buwRJdQ2023";
    textEncryptor.setPassword(salt);
    // 2.加密
    // 2.1加密内容
    String pd = "Hangzhou20230427";
    System.out.println("加密前:  " + pd);
    // 2.2加密操作
    String pdAfterEncrypt = textEncryptor.encrypt(pd);
    System.out.println("加密后:  " + pdAfterEncrypt);
    // 3.解密操作
    String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
    System.out.println("解密后:  " + pdAfterDecrypt);
}

Above, thanks.

April 27, 2023

Guess you like

Origin blog.csdn.net/zhangbeizhen18/article/details/130416302