2022-05-24 09:23:17 +08:00
|
|
|
|
package com.engine.salary.encrypt;
|
|
|
|
|
|
|
2022-10-10 09:43:57 +08:00
|
|
|
|
import com.engine.salary.sys.constant.SalarySysConstant;
|
|
|
|
|
|
import com.engine.salary.sys.entity.po.SalarySysConfPO;
|
|
|
|
|
|
import com.engine.salary.sys.enums.OpenEnum;
|
|
|
|
|
|
import com.engine.salary.sys.service.impl.SalarySysConfServiceImpl;
|
2022-05-24 09:23:17 +08:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import weaver.general.AES;
|
|
|
|
|
|
import weaver.general.BaseBean;
|
|
|
|
|
|
|
2023-03-30 11:44:21 +08:00
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
2022-10-10 09:43:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 此AES加密工具类仅用于关联应用设置中是否开启加密的应用使用
|
|
|
|
|
|
* 如另有需要,请重写工具类
|
|
|
|
|
|
*/
|
2022-05-24 09:23:17 +08:00
|
|
|
|
public class AESEncryptUtil {
|
|
|
|
|
|
|
2022-07-13 13:55:31 +08:00
|
|
|
|
static BaseBean bb = new BaseBean();
|
|
|
|
|
|
|
|
|
|
|
|
static String aesEncryptScrect = bb.getPropValue("hrmSalary", "AESEncryptScrect");
|
2022-10-10 09:43:57 +08:00
|
|
|
|
static SalarySysConfServiceImpl salarySysConfService = new SalarySysConfServiceImpl();
|
2022-07-13 13:55:31 +08:00
|
|
|
|
|
2022-05-24 09:23:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* AES加密
|
2022-07-13 13:55:31 +08:00
|
|
|
|
*
|
2022-05-24 09:23:17 +08:00
|
|
|
|
* @param source 原始数据
|
|
|
|
|
|
* @return 加密数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String encrypt(String source) {
|
2022-10-18 18:02:04 +08:00
|
|
|
|
String isEncrypt = getSalarySysConfigValue();
|
|
|
|
|
|
//防止初始化老数据时二次加密
|
|
|
|
|
|
if (StringUtils.isNotBlank(source) && OpenEnum.OPEN.getValue().equals(isEncrypt) && !source.startsWith(SalarySysConstant.PRE_SIGN_ENCRYPT)) {
|
|
|
|
|
|
return SalarySysConstant.PRE_SIGN_ENCRYPT + AES.encrypt(source, aesEncryptScrect);
|
2022-05-24 09:23:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
return source;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-10 09:43:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 应用设置是否开启加密
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static String getSalarySysConfigValue() {
|
2022-10-11 17:41:00 +08:00
|
|
|
|
SalarySysConfPO sysConfPO = salarySysConfService.getOneByCode(SalarySysConstant.OPEN_APPLICATION_ENCRYPT);
|
2023-03-03 14:55:26 +08:00
|
|
|
|
if (sysConfPO == null) {
|
2022-10-11 09:40:23 +08:00
|
|
|
|
return "1";
|
|
|
|
|
|
}
|
2022-10-11 17:41:00 +08:00
|
|
|
|
return sysConfPO.getConfValue();
|
2022-10-10 09:43:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-24 09:23:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* AES解密
|
2022-07-13 13:55:31 +08:00
|
|
|
|
*
|
2022-05-24 09:23:17 +08:00
|
|
|
|
* @param encryptStr 加密字符串
|
|
|
|
|
|
* @return 解密字符串
|
|
|
|
|
|
*/
|
2022-10-18 18:02:04 +08:00
|
|
|
|
public static String decrypt(String encryptStr) {
|
|
|
|
|
|
SalarySysConfPO sysConfPo = salarySysConfService.getOneByCode(SalarySysConstant.OPEN_APPLICATION_ENCRYPT);
|
|
|
|
|
|
if (StringUtils.isNotBlank(encryptStr)) {
|
|
|
|
|
|
if (encryptStr.startsWith(SalarySysConstant.PRE_SIGN_ENCRYPT)) {
|
|
|
|
|
|
encryptStr = encryptStr.substring(4, encryptStr.length());
|
|
|
|
|
|
return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
|
|
|
|
}
|
2022-10-19 09:25:45 +08:00
|
|
|
|
//第一版没有加AES_前缀为加密标识,所以初始解密时,需要根据是否有配置判断是否需要解密
|
|
|
|
|
|
//未配置加密设置时,需要解密
|
2023-03-03 14:55:26 +08:00
|
|
|
|
if (sysConfPo == null) {
|
2022-10-18 18:02:04 +08:00
|
|
|
|
return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return encryptStr;
|
|
|
|
|
|
}
|
2022-05-24 09:23:17 +08:00
|
|
|
|
|
2022-10-10 09:43:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用于关闭加密设置后AES解密
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param encryptStr 加密字符串
|
|
|
|
|
|
* @return 解密字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String closeEncryptSetting(String encryptStr) {
|
2022-10-18 18:02:04 +08:00
|
|
|
|
SalarySysConfPO sysConfPo = salarySysConfService.getOneByCode(SalarySysConstant.OPEN_APPLICATION_ENCRYPT);
|
2023-03-30 11:44:21 +08:00
|
|
|
|
// if (StringUtils.isNotBlank(encryptStr) && encryptStr.startsWith(SalarySysConstant.PRE_SIGN_ENCRYPT)) {
|
|
|
|
|
|
// encryptStr = encryptStr.substring(4, encryptStr.length());
|
|
|
|
|
|
// return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
|
|
|
|
// } else if (sysConfPo == null && StringUtils.isNotBlank(encryptStr)) {
|
|
|
|
|
|
// return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return encryptStr;
|
|
|
|
|
|
if (encryptStr == null) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
//AES_前缀的密文
|
|
|
|
|
|
if (encryptStr.startsWith(SalarySysConstant.PRE_SIGN_ENCRYPT)) {
|
|
|
|
|
|
encryptStr = encryptStr.substring(4, encryptStr.length());
|
|
|
|
|
|
return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
|
|
|
|
} else if (isNumeric(encryptStr) || checkHaveChinese(encryptStr) || encryptStr.startsWith("{") || encryptStr.contains("-") || encryptStr.contains(".")) {
|
|
|
|
|
|
return encryptStr;
|
|
|
|
|
|
} else if ("null".equals(encryptStr) || " ".equals(encryptStr) || "".equals(encryptStr)) {
|
|
|
|
|
|
return encryptStr;
|
|
|
|
|
|
} else if (sysConfPo == null || sysConfPo.getConfValue().equals(OpenEnum.OFF.getValue())) {
|
|
|
|
|
|
return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return encryptStr;
|
|
|
|
|
|
}
|
2022-10-18 18:02:04 +08:00
|
|
|
|
}
|
2023-03-30 11:44:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 判断字符串是否为整数或者小数或者负数
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean isNumeric(String str) {
|
|
|
|
|
|
|
|
|
|
|
|
Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
|
|
|
|
|
|
Matcher isNum = pattern.matcher(str);
|
|
|
|
|
|
if (!isNum.matches()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 判断字符串是否包含中文字符
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static boolean checkHaveChinese(String countname) {
|
|
|
|
|
|
|
|
|
|
|
|
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
|
|
|
|
|
|
Matcher m = p.matcher(countname);
|
|
|
|
|
|
if (m.find()) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2022-10-10 09:43:57 +08:00
|
|
|
|
}
|
2022-05-24 09:23:17 +08:00
|
|
|
|
}
|