合并代码
This commit is contained in:
parent
bf8e337cf3
commit
d6d07ce7ee
|
|
@ -0,0 +1,21 @@
|
||||||
|
package net.xnzn.core.common.config.sys;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(
|
||||||
|
prefix = "system.encrypt"
|
||||||
|
)
|
||||||
|
public class EncryptProperties {
|
||||||
|
public static final String PREFIX = "system.encrypt";
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return this.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(final String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package net.xnzn.core.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.codec.Base64;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.crypto.Mode;
|
||||||
|
import cn.hutool.crypto.Padding;
|
||||||
|
import cn.hutool.crypto.symmetric.AES;
|
||||||
|
import net.xnzn.core.common.config.sys.EncryptProperties;
|
||||||
|
import net.xnzn.core.common.encrypt.SpringContextHolder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AesEncryptUtil {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(AesEncryptUtil.class);
|
||||||
|
private static final String KEY_ALGORITHM = "AES";
|
||||||
|
private static final String AES_ENCRYPT_KEY = "pigxpigxpigxpigx";
|
||||||
|
@Resource
|
||||||
|
private EncryptProperties encryptProperties;
|
||||||
|
|
||||||
|
public static AesEncryptUtil getInstance() {
|
||||||
|
return (AesEncryptUtil) SpringContextHolder.getBean(AesEncryptUtil.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String aesEncrypt(String encryptStr) {
|
||||||
|
if (StrUtil.isBlank(encryptStr)) {
|
||||||
|
return encryptStr;
|
||||||
|
} else {
|
||||||
|
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, new SecretKeySpec(AES_ENCRYPT_KEY.getBytes(), "AES"), new IvParameterSpec(AES_ENCRYPT_KEY.getBytes()));
|
||||||
|
return aes.encryptBase64(encryptStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String aesDecode(String decodeStr) {
|
||||||
|
if (StrUtil.isBlank(decodeStr)) {
|
||||||
|
return decodeStr;
|
||||||
|
} else {
|
||||||
|
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, new SecretKeySpec(AES_ENCRYPT_KEY.getBytes(), "AES"), new IvParameterSpec(AES_ENCRYPT_KEY.getBytes()));
|
||||||
|
|
||||||
|
byte[] resultByte;
|
||||||
|
try {
|
||||||
|
resultByte = aes.decrypt(Base64.decode(decodeStr.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
} catch (Exception var5) {
|
||||||
|
log.info("字段解密异常:" + var5.getMessage());
|
||||||
|
return decodeStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new String(resultByte, StandardCharsets.UTF_8)).trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(aesEncrypt("123"));
|
||||||
|
System.out.println(aesDecode("Ko0muw9mxpn4mKcd40W0gw=="));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue