Encryption and Decryption Method by jasypt in SpringBoot
- 2021-06-28 12:23:12
- OfStack
1. Purpose
In SpringBoot, encryption and decryption are possible through jasypt. This is bidirectional and the key can be configured.
2. Use:
2.1 Create tool classes from UT and recognize jasypt
import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;
public class UtilTests {
@Test
public void jasyptTest() {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
// application.properties, jasypt.encryptor.password
encryptor.setPassword("abc");
// encrypt root
System.out.println(encryptor.encrypt("root"));
System.out.println(encryptor.encrypt("root"));
System.out.println(encryptor.encrypt("root"));
// decrypt, the result is root
System.out.println(encryptor.decrypt("UP/yojB7ie3apnh3mLTU7w=="));
System.out.println(encryptor.decrypt("ik9FE3GiYLiHwchiyHg9QQ=="));
System.out.println(encryptor.decrypt("9Obo/jq9EqmTE0QZaJFYrw=="));
}
}
You can see that the passwords generated are not the same each time, but the key can be used to decrypt the same plain text.
2.2 Configuring jasypt in SpringBoot
2.2.1 Configuration Key
jasypt.encryptor.password:abc
2.2.2 Use
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/tmp?useSSL=false & useUnicode=true & characterEncoding=utf-8
spring.datasource.username: ENC(ik9FE3GiYLiHwchiyHg9QQ==)
spring.datasource.password: ENC(ik9FE3GiYLiHwchiyHg9QQ==)
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
2.2.3 Configure key at startup
java -jar -Djasypt.encryptor.password=abc xxx.jar
summary