|
|
@ -28,8 +28,10 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.opsli.api.base.result.ResultVo;
|
|
|
|
import org.opsli.api.base.result.ResultVo;
|
|
|
|
import org.opsli.api.web.system.other.crypto.OtherCryptoAsymmetricRestApi;
|
|
|
|
import org.opsli.api.web.system.other.crypto.OtherCryptoAsymmetricRestApi;
|
|
|
|
|
|
|
|
import org.opsli.api.wrapper.system.options.OptionsModel;
|
|
|
|
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
|
|
|
|
import org.opsli.api.wrapper.system.other.crypto.OtherCryptoAsymmetricModel;
|
|
|
|
import org.opsli.common.enums.CryptoAsymmetricType;
|
|
|
|
import org.opsli.common.enums.CryptoAsymmetricType;
|
|
|
|
|
|
|
|
import org.opsli.common.enums.OptionsType;
|
|
|
|
import org.opsli.common.exception.ServiceException;
|
|
|
|
import org.opsli.common.exception.ServiceException;
|
|
|
|
import org.opsli.core.cache.local.CacheUtil;
|
|
|
|
import org.opsli.core.cache.local.CacheUtil;
|
|
|
|
import org.opsli.core.msg.CoreMsg;
|
|
|
|
import org.opsli.core.msg.CoreMsg;
|
|
|
@ -66,8 +68,8 @@ public class CryptoAsymmetricUtil {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 根据 cryptoAsymmetricType 枚举 获得参数
|
|
|
|
* 根据 cryptoAsymmetricType 枚举 获得参数
|
|
|
|
* @param cryptoAsymmetricType
|
|
|
|
* @param cryptoAsymmetricType type
|
|
|
|
* @return
|
|
|
|
* @return model
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static OtherCryptoAsymmetricModel getCryptoAsymmetric(final CryptoAsymmetricType cryptoAsymmetricType){
|
|
|
|
public static OtherCryptoAsymmetricModel getCryptoAsymmetric(final CryptoAsymmetricType cryptoAsymmetricType){
|
|
|
|
if(cryptoAsymmetricType == null){
|
|
|
|
if(cryptoAsymmetricType == null){
|
|
|
@ -106,12 +108,36 @@ public class CryptoAsymmetricUtil {
|
|
|
|
return model;
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean noData = true;
|
|
|
|
// 查询数据库
|
|
|
|
// 查询数据库
|
|
|
|
ResultVo<OtherCryptoAsymmetricModel> resultVo = otherCryptoAsymmetricRestApi.getByCryptoType(typeCode);
|
|
|
|
ResultVo<OtherCryptoAsymmetricModel> resultVo = otherCryptoAsymmetricRestApi.getByCryptoType(typeCode);
|
|
|
|
if(resultVo.isSuccess()){
|
|
|
|
if(resultVo.isSuccess()){
|
|
|
|
model = resultVo.getData();
|
|
|
|
model = resultVo.getData();
|
|
|
|
// 存入缓存
|
|
|
|
if(model != null){
|
|
|
|
CacheUtil.put(cacheKey, model);
|
|
|
|
noData = false;
|
|
|
|
|
|
|
|
// 存入缓存
|
|
|
|
|
|
|
|
CacheUtil.put(cacheKey, model);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果没取到数值 则自动创建
|
|
|
|
|
|
|
|
if(noData){
|
|
|
|
|
|
|
|
// 获得系统配置参数 非对称加密枚举
|
|
|
|
|
|
|
|
CryptoAsymmetricType asymmetricType = null;
|
|
|
|
|
|
|
|
OptionsModel optionsModel = OptionsUtil.getOptionByCode(OptionsType.CRYPTO_ASYMMETRIC);
|
|
|
|
|
|
|
|
if(optionsModel != null){
|
|
|
|
|
|
|
|
// 获得加密类型
|
|
|
|
|
|
|
|
asymmetricType = CryptoAsymmetricType.getCryptoType(
|
|
|
|
|
|
|
|
optionsModel.getOptionValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 默认 RSA 算法
|
|
|
|
|
|
|
|
model = create(asymmetricType!=null?asymmetricType:CryptoAsymmetricType.RSA);
|
|
|
|
|
|
|
|
ResultVo<?> insertModel = otherCryptoAsymmetricRestApi.insertInner(model);
|
|
|
|
|
|
|
|
if(insertModel.isSuccess()){
|
|
|
|
|
|
|
|
// 存入缓存
|
|
|
|
|
|
|
|
CacheUtil.put(cacheKey, model);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
}catch (Exception e){
|
|
|
|
log.error(e.getMessage(),e);
|
|
|
|
log.error(e.getMessage(),e);
|
|
|
@ -134,8 +160,8 @@ public class CryptoAsymmetricUtil {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 刷新参数 - 删就完了
|
|
|
|
* 刷新参数 - 删就完了
|
|
|
|
* @param model
|
|
|
|
* @param model m
|
|
|
|
* @return
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean refresh(final OtherCryptoAsymmetricModel model){
|
|
|
|
public static boolean refresh(final OtherCryptoAsymmetricModel model){
|
|
|
|
if(model == null || StringUtils.isEmpty(model.getCryptoType())){
|
|
|
|
if(model == null || StringUtils.isEmpty(model.getCryptoType())){
|
|
|
@ -174,10 +200,11 @@ public class CryptoAsymmetricUtil {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 创建公私钥
|
|
|
|
* 创建公私钥
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
* @return
|
|
|
|
* @return Model
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static OtherCryptoAsymmetricModel create(final CryptoAsymmetricType cryptoAsymmetricType){
|
|
|
|
public static OtherCryptoAsymmetricModel create(final CryptoAsymmetricType cryptoAsymmetricType){
|
|
|
|
OtherCryptoAsymmetricModel model = new OtherCryptoAsymmetricModel();
|
|
|
|
OtherCryptoAsymmetricModel model = new OtherCryptoAsymmetricModel();
|
|
|
|
|
|
|
|
model.setCryptoType(cryptoAsymmetricType.getCode());
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
case RSA:
|
|
|
|
case RSA:
|
|
|
|
RSA rsa = SecureUtil.rsa();
|
|
|
|
RSA rsa = SecureUtil.rsa();
|
|
|
@ -204,7 +231,8 @@ public class CryptoAsymmetricUtil {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 加密数据
|
|
|
|
* 加密数据
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
* @return
|
|
|
|
* @param data 数据
|
|
|
|
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String encrypt(final CryptoAsymmetricType cryptoAsymmetricType, final Object data){
|
|
|
|
public static String encrypt(final CryptoAsymmetricType cryptoAsymmetricType, final Object data){
|
|
|
|
|
|
|
|
|
|
|
@ -214,14 +242,15 @@ public class CryptoAsymmetricUtil {
|
|
|
|
// 原始/加密 数据
|
|
|
|
// 原始/加密 数据
|
|
|
|
String encryptedStr = jsonObject.toString();
|
|
|
|
String encryptedStr = jsonObject.toString();
|
|
|
|
|
|
|
|
|
|
|
|
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果找不到 公私钥 直接返回原始数据
|
|
|
|
|
|
|
|
if(cryptoAsymmetric == null){
|
|
|
|
|
|
|
|
return encryptedStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果找不到 公私钥 直接返回原始数据
|
|
|
|
|
|
|
|
if(cryptoAsymmetric == null){
|
|
|
|
|
|
|
|
throw new RuntimeException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
case RSA:
|
|
|
|
case RSA:
|
|
|
|
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
@ -239,120 +268,93 @@ public class CryptoAsymmetricUtil {
|
|
|
|
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
|
|
|
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
throw new RuntimeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
}catch (Exception e){
|
|
|
|
// 加密失败
|
|
|
|
// 加密失败
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_CRYPTO_EN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return encryptedStr;
|
|
|
|
return encryptedStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 加密数据
|
|
|
|
* RSA 解密数据
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
* @return
|
|
|
|
* @param data 数据
|
|
|
|
|
|
|
|
* @return Object
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String decrypt(final CryptoAsymmetricType cryptoAsymmetricType, final String data){
|
|
|
|
public static Object decryptToObj(final CryptoAsymmetricType cryptoAsymmetricType, final String data){
|
|
|
|
if(StringUtils.isEmpty(data)){
|
|
|
|
Object obj;
|
|
|
|
return null;
|
|
|
|
String decryptedData = decrypt(cryptoAsymmetricType, data);
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
|
|
|
|
obj = JSONObject.parse(decryptedData);
|
|
|
|
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
|
|
|
|
}catch (Exception e){
|
|
|
|
// 如果找不到 公私钥 直接返回原始数据
|
|
|
|
// 非对称解密反射失败
|
|
|
|
if(cryptoAsymmetric == null){
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_CRYPTO_REFLEX);
|
|
|
|
return data;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 解密数据
|
|
|
|
|
|
|
|
* @param cryptoAsymmetricType 枚举
|
|
|
|
|
|
|
|
* @param data 数据
|
|
|
|
|
|
|
|
* @return String
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String decrypt(final CryptoAsymmetricType cryptoAsymmetricType, final String data){
|
|
|
|
String decryptStr;
|
|
|
|
String decryptStr;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(StringUtils.isEmpty(data)){
|
|
|
|
|
|
|
|
throw new RuntimeException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
|
|
|
|
|
|
|
|
// 如果找不到 公私钥 直接返回原始数据
|
|
|
|
|
|
|
|
if(cryptoAsymmetric == null){
|
|
|
|
|
|
|
|
throw new RuntimeException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String tmp;
|
|
|
|
|
|
|
|
String currData = data.replaceAll(" ", "+");
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
case RSA:
|
|
|
|
case RSA:
|
|
|
|
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
decryptStr = rsa.decryptStr(data, KeyType.PrivateKey);
|
|
|
|
tmp = rsa.decryptStr(currData, KeyType.PrivateKey);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case SM2:
|
|
|
|
case SM2:
|
|
|
|
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
decryptStr = sm2.decryptStr(data, KeyType.PrivateKey);
|
|
|
|
tmp = sm2.decryptStr(currData, KeyType.PrivateKey);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case ECIES:
|
|
|
|
case ECIES:
|
|
|
|
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
decryptStr = ecies.decryptStr(data, KeyType.PrivateKey);
|
|
|
|
tmp = ecies.decryptStr(currData, KeyType.PrivateKey);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
throw new RuntimeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
|
|
// 加密失败
|
|
|
|
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//解密,因为编码传值时有空格出现
|
|
|
|
// 转换对象
|
|
|
|
String decryptStr;
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
String tmp = rsa.decryptStr(data, KeyType.PrivateKey);
|
|
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(tmp);
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(tmp);
|
|
|
|
Object obj = jsonObject.get(rsaKey);
|
|
|
|
Object obj = jsonObject.get(CRYPTO_KEY);
|
|
|
|
if(obj instanceof Collection){
|
|
|
|
if(obj instanceof Collection){
|
|
|
|
decryptStr = jsonObject.getJSONArray(rsaKey).toJSONString();
|
|
|
|
decryptStr = jsonObject.getJSONArray(CRYPTO_KEY).toJSONString();
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
decryptStr = jsonObject.getJSONObject(rsaKey).toJSONString();
|
|
|
|
decryptStr = jsonObject.getJSONObject(CRYPTO_KEY).toJSONString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
}catch (Exception e){
|
|
|
|
// 解密失败
|
|
|
|
// 解密失败
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_DE);
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_CRYPTO_DE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return decryptStr;
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
|
|
|
jsonObject.put(CRYPTO_KEY, data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 原始/加密 数据
|
|
|
|
|
|
|
|
String encryptedStr = jsonObject.toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OtherCryptoAsymmetricModel cryptoAsymmetric = getCryptoAsymmetric(cryptoAsymmetricType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果找不到 公私钥 直接返回原始数据
|
|
|
|
|
|
|
|
if(cryptoAsymmetric == null){
|
|
|
|
|
|
|
|
return encryptedStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
switch (cryptoAsymmetricType){
|
|
|
|
|
|
|
|
case RSA:
|
|
|
|
|
|
|
|
RSA rsa = SecureUtil.rsa(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
|
|
|
|
encryptedStr = rsa.encryptBase64(
|
|
|
|
|
|
|
|
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SM2:
|
|
|
|
|
|
|
|
SM2 sm2 = SmUtil.sm2(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
|
|
|
|
encryptedStr = sm2.encryptBase64(
|
|
|
|
|
|
|
|
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ECIES:
|
|
|
|
|
|
|
|
ECIES ecies = new ECIES(cryptoAsymmetric.getPrivateKey(), cryptoAsymmetric.getPublicKey());
|
|
|
|
|
|
|
|
encryptedStr = ecies.encryptBase64(
|
|
|
|
|
|
|
|
StrUtil.bytes(encryptedStr, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
|
|
// 加密失败
|
|
|
|
|
|
|
|
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return encryptedStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// =====================================
|
|
|
|
// =====================================
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
public static void setOtherCryptoAsymmetricRestApi(OtherCryptoAsymmetricRestApi otherCryptoAsymmetricRestApi) {
|
|
|
|
public void setOtherCryptoAsymmetricRestApi(OtherCryptoAsymmetricRestApi otherCryptoAsymmetricRestApi) {
|
|
|
|
CryptoAsymmetricUtil.otherCryptoAsymmetricRestApi = otherCryptoAsymmetricRestApi;
|
|
|
|
CryptoAsymmetricUtil.otherCryptoAsymmetricRestApi = otherCryptoAsymmetricRestApi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|