55 lines
1.1 KiB
Java
55 lines
1.1 KiB
Java
package com.newyetai.common.util;
|
|
|
|
import org.jasypt.encryption.StringEncryptor;
|
|
|
|
import com.newyetai.common.exception.util.Assert;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
/**
|
|
* 配置信息加解密工具
|
|
* @author sr on 2021-11-11
|
|
*/
|
|
@Slf4j
|
|
public class JasyptUtils {
|
|
|
|
private static StringEncryptor stringEncryptor;
|
|
|
|
public JasyptUtils(StringEncryptor stringEncryptor) {
|
|
Assert.isNull(JasyptUtils.stringEncryptor, "已存在加密对象");
|
|
JasyptUtils.stringEncryptor = stringEncryptor;
|
|
}
|
|
|
|
/**
|
|
* 加密
|
|
* @param str
|
|
* @return
|
|
* @author sr on 2021-11-11
|
|
*/
|
|
public static String encrypt(String str) {
|
|
try {
|
|
return stringEncryptor.encrypt(str);
|
|
} catch (Exception e) {
|
|
log.info("加密失败:", e);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 解密
|
|
* @param encodedStr
|
|
* @return
|
|
* @author sr on 2021-11-11
|
|
*/
|
|
public static String decrypt(String encodedStr) {
|
|
try {
|
|
return stringEncryptor.decrypt(encodedStr);
|
|
} catch (Exception e) {
|
|
log.info("解密失败:", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
}
|