123 lines
3.8 KiB
Java
123 lines
3.8 KiB
Java
package com.engine.salary.encrypt;
|
|
|
|
import com.engine.salary.sys.constant.SalarySysConstant;
|
|
import com.engine.salary.sys.entity.po.SalarySysConfPO;
|
|
import com.engine.salary.sys.enums.OpenEnum;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import weaver.general.AES;
|
|
import weaver.general.BaseBean;
|
|
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* 此AES加密工具类仅用于关联应用设置中是否开启加密的应用使用
|
|
* 如另有需要,请重写工具类
|
|
*/
|
|
public class AESEncryptUtil {
|
|
|
|
static BaseBean bb = new BaseBean();
|
|
|
|
static String aesEncryptScrect = bb.getPropValue("hrmSalary", "AESEncryptScrect");
|
|
|
|
/**
|
|
* AES加密
|
|
*
|
|
* @param source 原始数据
|
|
* @return 加密数据
|
|
*/
|
|
public static String encrypt(String source) {
|
|
//防止初始化老数据时二次加密
|
|
if (StringUtils.isNotBlank(source) && !source.startsWith(SalarySysConstant.PRE_SIGN_ENCRYPT)) {
|
|
return SalarySysConstant.PRE_SIGN_ENCRYPT + AES.encrypt(source, aesEncryptScrect);
|
|
}
|
|
return source;
|
|
}
|
|
|
|
|
|
/**
|
|
* AES解密
|
|
*
|
|
* @param encryptStr 加密字符串
|
|
* @return 解密字符串
|
|
*/
|
|
public static String decrypt(String encryptStr) {
|
|
if (StringUtils.isNotBlank(encryptStr)) {
|
|
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(".") ||
|
|
"null".equals(encryptStr) ||
|
|
StringUtils.isBlank(encryptStr)) {
|
|
return encryptStr;
|
|
} else {
|
|
return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
}
|
|
}
|
|
return encryptStr;
|
|
}
|
|
|
|
/**
|
|
* 用于关闭加密设置后AES解密
|
|
*
|
|
* @param encryptStr 加密字符串
|
|
* @return 解密字符串
|
|
*/
|
|
public static String closeEncryptSetting(String encryptStr, SalarySysConfPO sysConfPo) {
|
|
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(".") ||
|
|
"null".equals(encryptStr) ||
|
|
StringUtils.isBlank(encryptStr)) {
|
|
return encryptStr;
|
|
} else if (sysConfPo == null || sysConfPo.getConfValue().equals(OpenEnum.OFF.getValue())) {
|
|
return AES.decrypt(encryptStr, aesEncryptScrect);
|
|
} else {
|
|
return encryptStr;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 判断字符串是否为整数或者小数或者负数
|
|
*/
|
|
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;
|
|
|
|
}
|
|
}
|