jasypt-spring-boot-starter 密码加密

发布时间 2023-08-18 11:07:38作者: KeepSmiling_me

引入依赖:

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

配置spring 加密

import com.iflytek.quality.qualityinspection.entity.Constant;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JasyptConfig {

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(Constant.PBE_WITH_MD_5_AND_DES); // 设置加密密码,需要妥善保管
        return encryptor;
    }
}

public interface Constant {
//加密password
String PBE_WITH_MD_5_AND_DES = "iflytek";
}
 

application.yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: dm.jdbc.driver.DmDriver
    url: jdbc:dm://localhost:5236/SYSDBA?serverTimezone=UTC&useSSL=false&useUnicode=true&schema=SYSDBA&characterEncoding=utf-8&columnNameUpperCase=false
    username: SYSDBA
    password: ENC(TxSE/CTS0ytslkea2MoBjQ==)

 

加密脚本window:

@echo off
chcp 65001
:Main
cls
set str=""
echo 请输入要加密的明文:
set /p str=
::echo %str%
if defined str (
    java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="%str%" password=iflytek algorithm=PBEWithMD5AndDES
)
pause 
goto Main

解密脚本windows:

@echo off
chcp 65001
:Main
cls
set str=""
echo 请输入要解密密文:
set /p str=
::echo %flag%
if defined str (
    java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="%str%" password=iflytek algorithm=PBEWithMD5AndDES
)
pause 
goto Main

加密脚本linux:

# 加密
echo "请输入要加密的明文:"
read pwd
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="${pwd}" password=iflytek algorithm=PBEWithMD5AndDES

解密脚本linux:

# 解密
echo "请输入要解密的密文:"
read pwd
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="${pwd}" password=iflytek algorithm=PBEWithMD5AndDES