org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
/**
* jasypt 自定义数据库密码加密器 - SM4加密算法
*
* @author jiaqiankun
*/
@Component("customStringEncryptor"
)
public class CustomStringEncryptor
implements StringEncryptor {
@Value("${jasypt.encryptor.salt}"
)
private String salt;
@Override
public String encrypt(String message) {
SymmetricCrypto sm4 =
SmUtil.sm4(salt.getBytes());
String encryptHex =
sm4.encryptHex(message);
return encryptHex;
}
@Override
public String decrypt(String encryptedMessage) {
SymmetricCrypto sm41 =
SmUtil.sm4(salt.getBytes());
String decryptStr =
sm41.decryptStr(encryptedMessage, CharsetUtil.CHARSET_UTF_8);
return decryptStr;
}
}
在配置文件中指定自定义的加密器并定义一个salt
#属性文件加密
jasypt:
encryptor:
#salt必须为16位字符串
salt: ya024uspkidtclu3
bean: customStringEncryptor
springboot数据库密码加密-使用自定义加密算法
标签:ISE 集成 div import 简单 解密 salt alt group