SpringBoot项目使用jasypt加解密

SpringBoot项目使用jasypt加解密


一、添加依赖

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>

二、生成加密数据

1.根据 jasypt-spring-boot-starter 的父级POM, 找到jasypt的依赖路径,我这里看到如下:

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>${jasypt.version}</version>
</dependency>

2.然后通过上面的依赖路径,在本地Maven仓库中找到jasypt的jar包,通过CMD执行下面操作:

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=原密码 password=salt algorithm=PBEWithMD5AndDES

3.结果如下:


D:\tool\java\maven\repository\org\jasypt\jasypt\1.9.3>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=Qwer6666! password=salt algorithm=PBEWithMD5AndDES

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.281-b09



----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: Qwer6666!
password: salt



----OUTPUT----------------------

YvsMMkJy7il7fHWYaUeOjGP0gY00laP6

说明:

  • algorithm: 加密算法
  • input:加密的内容
  • password: 盐值(后面解密也需使用这个盐值进行解密)
  • OUTPUT: 加密之后的内容
  • org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI 为加密使用的类

三、对加密数据进行解密

同样还是使用jasypt的jar包,在CMD中执行操作如下:

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input=YvsMMkJy7il7fHWYaUeOjGP0gY00laP6 password=salt algorithm=PBEWithMD5AndDES

结果如下:

D:\tool\java\maven\repository\org\jasypt\jasypt\1.9.3>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input=YvsMMkJy7il7fHWYaUeOjGP0gY00laP6 password=salt algorithm=PBEWithMD5AndDES

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.281-b09



----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: YvsMMkJy7il7fHWYaUeOjGP0gY00laP6
password: salt



----OUTPUT----------------------

Qwer6666!

说明:

  • algorithm:加密算法
  • input:要解密的内容
  • password:盐值
  • OUTPUT:解密后的内容
  • org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI 为解密使用的类

四:在springboot .yml 配置:

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/yunpi_mdm?zeroDateTimeBehavior=CONVERT_TO_NULL&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    # password: Qwer6666!
    password: ENC(22c8Ujie5G420hXSTN6iR5u8P0G6t14m)
    hikari:
      auto-commit: false
      minimum-idle: 2
      maximum-pool-size: 10
      connection-timeout: 10000
      max-lifetime: 600000
      idle-timeout: 60000
      validation-timeout: 1000
      leak-detection-threshold: 30000
jasypt:
  encryptor:
    password: salt
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
    property:
      prefix: ENC(
      suffix: )

你可能感兴趣的:(spring,boot,java,spring)