|
@@ -1,17 +1,24 @@
|
|
package com.hrsk.cloud.eg.domain.api.channel.customized.threedocking.impl;
|
|
package com.hrsk.cloud.eg.domain.api.channel.customized.threedocking.impl;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
+import com.google.common.base.Function;
|
|
import com.hrsk.cloud.eg.client.dto.plan.command.EgLoanApiConfigInfoCmd;
|
|
import com.hrsk.cloud.eg.client.dto.plan.command.EgLoanApiConfigInfoCmd;
|
|
import com.hrsk.cloud.eg.client.dto.plan.command.PlanCmd;
|
|
import com.hrsk.cloud.eg.client.dto.plan.command.PlanCmd;
|
|
import com.hrsk.cloud.eg.client.dto.user.command.UserBaseInfoCmd;
|
|
import com.hrsk.cloud.eg.client.dto.user.command.UserBaseInfoCmd;
|
|
-
|
|
|
|
|
|
+import com.hrsk.cloud.eg.client.vo.DataVo;
|
|
|
|
+import com.hrsk.cloud.eg.domain.api.channel.customized.common.ServerCodeEnums;
|
|
|
|
+import com.hrsk.cloud.eg.domain.api.channel.customized.threedocking.LoanDockingApi;
|
|
|
|
+import com.hrsk.cloud.eg.domain.api.channel.guide.http.HttpApiRequest;
|
|
|
|
+import com.hrsk.cloud.eg.domain.api.channel.guide.http.HttpApiRequestConfig;
|
|
|
|
+import com.hrsk.cloud.eg.domain.api.channel.guide.http.HttpApiResponse;
|
|
|
|
+import com.hrsk.cloud.eg.domain.common.HessianUtils;
|
|
|
|
+import com.hrsk.cloud.eg.domain.common.Md5Util;
|
|
import lombok.Data;
|
|
import lombok.Data;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.commons.codec.binary.Base64;
|
|
-import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Cipher;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.security.*;
|
|
import java.security.*;
|
|
@@ -44,10 +51,10 @@ public class YunHaiLianDongCreditService implements LoanDockingApi {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public DataVo qualityCheck(UserBaseInfoCmd userInfo, PlanCmd product, EgLoanApiConfigInfoCmd configInfo) {
|
|
|
|
|
|
+ public DataVo qualityCheck(UserBaseInfoCmd userInfo, PlanCmd product, EgLoanApiConfigInfoCmd configInfo, Function<HttpApiRequest, HttpApiResponse> httpClientFunc,Function<String,Object> redisGetFunction,Function<RedisSetDto,Boolean> redisSetFunction) {
|
|
try {
|
|
try {
|
|
// log.info("credit YunHaiLianDongCreditService qualityCheck begin...userId:{}", userInfo.getUserId());
|
|
// log.info("credit YunHaiLianDongCreditService qualityCheck begin...userId:{}", userInfo.getUserId());
|
|
-
|
|
|
|
|
|
+
|
|
byte[] requestConfig = configInfo.getRequestConfig();
|
|
byte[] requestConfig = configInfo.getRequestConfig();
|
|
Object deserialize = HessianUtils.deserialize(requestConfig);
|
|
Object deserialize = HessianUtils.deserialize(requestConfig);
|
|
JSONObject config = JSONObject.parseObject(deserialize.toString());
|
|
JSONObject config = JSONObject.parseObject(deserialize.toString());
|
|
@@ -57,9 +64,10 @@ public class YunHaiLianDongCreditService implements LoanDockingApi {
|
|
checkInfo.setChannel_id(config.getInteger("channel_id"));
|
|
checkInfo.setChannel_id(config.getInteger("channel_id"));
|
|
String url = config.getString("checkUrl");
|
|
String url = config.getString("checkUrl");
|
|
// log.info("credit qualityCheck YunHaiLianDongCreditService request userId:{}, url:{}, CheckInfo:{}", userInfo.getUserId(), url, checkInfo);
|
|
// log.info("credit qualityCheck YunHaiLianDongCreditService request userId:{}, url:{}, CheckInfo:{}", userInfo.getUserId(), url, checkInfo);
|
|
- String response = null;
|
|
|
|
- try {
|
|
|
|
- response =httpRestTemplate.restTemplate().postForObject(url,checkInfo, String.class);
|
|
|
|
|
|
+ HttpApiResponse response = null;
|
|
|
|
+ try{
|
|
|
|
+ HttpApiRequest request=new HttpApiRequest(HttpApiRequestConfig.postRequest(JSON.toJSONString(checkInfo),"post",url));
|
|
|
|
+ response =httpClientFunc.apply(request);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("云海联动准入异常,异常信息:{}", e.getMessage(), e);
|
|
log.error("云海联动准入异常,异常信息:{}", e.getMessage(), e);
|
|
}
|
|
}
|
|
@@ -82,188 +90,188 @@ public class YunHaiLianDongCreditService implements LoanDockingApi {
|
|
return DataVo.fail(1, "云海联动撞库失败,请联系系统管理员", product.getPlanId());
|
|
return DataVo.fail(1, "云海联动撞库失败,请联系系统管理员", product.getPlanId());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- public static class RSAUtils {
|
|
|
|
-
|
|
|
|
- public static final String CHARSET = "UTF-8";
|
|
|
|
- public static final String RSA_ALGORITHM = "RSA";
|
|
|
|
- public static final String RSA_ALGORITHM_SIGN = "SHA256WithRSA";
|
|
|
|
- public static final int ALGORITHM_RSA_PRIVATE_KEY_LENGTH = 2048;
|
|
|
|
-
|
|
|
|
- public Map<String, String> initRSAKey(int keysize) {
|
|
|
|
- if (keysize != ALGORITHM_RSA_PRIVATE_KEY_LENGTH) {
|
|
|
|
- throw new IllegalArgumentException(
|
|
|
|
- "RSA1024已经不安全了,请使用" + ALGORITHM_RSA_PRIVATE_KEY_LENGTH + "初始化RSA密钥对");
|
|
|
|
- }
|
|
|
|
- //为RSA算法创建一个KeyPairGenerator对象
|
|
|
|
- KeyPairGenerator kpg;
|
|
|
|
- try {
|
|
|
|
- kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
|
|
|
|
- } catch (NoSuchAlgorithmException e) {
|
|
|
|
- throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
|
|
|
|
- }
|
|
|
|
- //初始化KeyPairGenerator对象,不要被initialize()源码表面上欺骗,其实这里声明的size是生效的
|
|
|
|
- kpg.initialize(ALGORITHM_RSA_PRIVATE_KEY_LENGTH);
|
|
|
|
- //生成密匙对
|
|
|
|
- KeyPair keyPair = kpg.generateKeyPair();
|
|
|
|
- //得到公钥
|
|
|
|
- Key publicKey = keyPair.getPublic();
|
|
|
|
- String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
|
|
|
|
- //得到私钥
|
|
|
|
- Key privateKey = keyPair.getPrivate();
|
|
|
|
- String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
|
|
|
|
- Map<String, String> keyPairMap = new HashMap<String, String>();
|
|
|
|
- keyPairMap.put("publicKey", publicKeyStr);
|
|
|
|
- keyPairMap.put("privateKey", privateKeyStr);
|
|
|
|
- return keyPairMap;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public Map<String, String> createKeys(int keySize) {
|
|
|
|
- //为RSA算法创建一个KeyPairGenerator对象
|
|
|
|
- KeyPairGenerator kpg;
|
|
|
|
- try {
|
|
|
|
- kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
|
|
|
|
- } catch (NoSuchAlgorithmException e) {
|
|
|
|
- throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //初始化KeyPairGenerator对象,密钥长度
|
|
|
|
- kpg.initialize(keySize);
|
|
|
|
- //生成密匙对
|
|
|
|
- KeyPair keyPair = kpg.generateKeyPair();
|
|
|
|
- //得到公钥
|
|
|
|
- Key publicKey = keyPair.getPublic();
|
|
|
|
- String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
|
|
|
|
- //得到私钥
|
|
|
|
- Key privateKey = keyPair.getPrivate();
|
|
|
|
- String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
|
|
|
|
- Map<String, String> keyPairMap = new HashMap<String, String>();
|
|
|
|
- keyPairMap.put("publicKey", publicKeyStr);
|
|
|
|
- keyPairMap.put("privateKey", privateKeyStr);
|
|
|
|
-
|
|
|
|
- return keyPairMap;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 得到公钥
|
|
|
|
- *
|
|
|
|
- * @param publicKey 密钥字符串(经过base64编码)
|
|
|
|
- * @throws Exception
|
|
|
|
- */
|
|
|
|
- public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
|
- //通过X509编码的Key指令获得公钥对象
|
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
- X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
|
|
|
|
- RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
|
|
|
|
- return key;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 得到私钥
|
|
|
|
- *
|
|
|
|
- * @param privateKey 密钥字符串(经过base64编码)
|
|
|
|
- * @throws Exception
|
|
|
|
- */
|
|
|
|
- public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
|
- //通过PKCS#8编码的Key指令获得私钥对象
|
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
- PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
|
|
|
|
- RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
|
- return key;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 公钥加密
|
|
|
|
- *
|
|
|
|
- * @param data
|
|
|
|
- * @param publicKey
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String publicEncrypt(String data, RSAPublicKey publicKey) {
|
|
|
|
- try {
|
|
|
|
- Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
|
|
- return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * RSA算法使用私钥对数据生成数字签名
|
|
|
|
- *
|
|
|
|
- * @param data 待签名的明文字符串
|
|
|
|
- * @param key RSA私钥字符串
|
|
|
|
- * @return RSA私钥签名后的经过Base64编码的字符串
|
|
|
|
- */
|
|
|
|
- public static String buildRSASignByPrivateKey(String data, String key) {
|
|
|
|
- try {
|
|
|
|
- //通过PKCS#8编码的Key指令获得私钥对象
|
|
|
|
- PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key));
|
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
- PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
|
- //sign
|
|
|
|
- Signature signature = Signature.getInstance(RSA_ALGORITHM_SIGN);
|
|
|
|
- signature.initSign(privateKey);
|
|
|
|
- signature.update(data.getBytes(CHARSET));
|
|
|
|
- return Base64.encodeBase64URLSafeString(signature.sign());
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException("签名字符串[" + data + "]时遇到异常", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * RSA算法使用公钥校验数字签名
|
|
|
|
- *
|
|
|
|
- * @param data 参与签名的明文字符串
|
|
|
|
- * @param key RSA公钥字符串
|
|
|
|
- * @param sign RSA签名得到的经过Base64编码的字符串
|
|
|
|
- * @return true--验签通过,false--验签未通过
|
|
|
|
- */
|
|
|
|
- public static boolean buildRSAverifyByPublicKey(String data, String key, String sign) {
|
|
|
|
- try {
|
|
|
|
- //通过X509编码的Key指令获得公钥对象
|
|
|
|
- X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key));
|
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
- PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
|
|
|
|
- //verify
|
|
|
|
- Signature signature = Signature.getInstance(RSA_ALGORITHM_SIGN);
|
|
|
|
- signature.initVerify(publicKey);
|
|
|
|
- signature.update(data.getBytes(CHARSET));
|
|
|
|
- return signature.verify(Base64.decodeBase64(sign));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException("验签字符串[" + data + "]时遇到异常", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize) {
|
|
|
|
- int maxBlock = 0;
|
|
|
|
- if (opmode == Cipher.DECRYPT_MODE) {
|
|
|
|
- maxBlock = keySize / 8;
|
|
|
|
- } else {
|
|
|
|
- maxBlock = keySize / 8 - 11;
|
|
|
|
- }
|
|
|
|
- ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
- int offSet = 0;
|
|
|
|
- byte[] buff;
|
|
|
|
- int i = 0;
|
|
|
|
- try {
|
|
|
|
- while (datas.length > offSet) {
|
|
|
|
- if (datas.length - offSet > maxBlock) {
|
|
|
|
- buff = cipher.doFinal(datas, offSet, maxBlock);
|
|
|
|
- } else {
|
|
|
|
- buff = cipher.doFinal(datas, offSet, datas.length - offSet);
|
|
|
|
- }
|
|
|
|
- out.write(buff, 0, buff.length);
|
|
|
|
- i++;
|
|
|
|
- offSet = i * maxBlock;
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException("加解密阀值为[" + maxBlock + "]的数据时发生异常", e);
|
|
|
|
- }
|
|
|
|
- byte[] resultDatas = out.toByteArray();
|
|
|
|
- IOUtils.closeQuietly(out);
|
|
|
|
- return resultDatas;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+// public static class RSAUtils {
|
|
|
|
+//
|
|
|
|
+// public static final String CHARSET = "UTF-8";
|
|
|
|
+// public static final String RSA_ALGORITHM = "RSA";
|
|
|
|
+// public static final String RSA_ALGORITHM_SIGN = "SHA256WithRSA";
|
|
|
|
+// public static final int ALGORITHM_RSA_PRIVATE_KEY_LENGTH = 2048;
|
|
|
|
+//
|
|
|
|
+// public Map<String, String> initRSAKey(int keysize) {
|
|
|
|
+// if (keysize != ALGORITHM_RSA_PRIVATE_KEY_LENGTH) {
|
|
|
|
+// throw new IllegalArgumentException(
|
|
|
|
+// "RSA1024已经不安全了,请使用" + ALGORITHM_RSA_PRIVATE_KEY_LENGTH + "初始化RSA密钥对");
|
|
|
|
+// }
|
|
|
|
+// //为RSA算法创建一个KeyPairGenerator对象
|
|
|
|
+// KeyPairGenerator kpg;
|
|
|
|
+// try {
|
|
|
|
+// kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
|
|
|
|
+// } catch (NoSuchAlgorithmException e) {
|
|
|
|
+// throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
|
|
|
|
+// }
|
|
|
|
+// //初始化KeyPairGenerator对象,不要被initialize()源码表面上欺骗,其实这里声明的size是生效的
|
|
|
|
+// kpg.initialize(ALGORITHM_RSA_PRIVATE_KEY_LENGTH);
|
|
|
|
+// //生成密匙对
|
|
|
|
+// KeyPair keyPair = kpg.generateKeyPair();
|
|
|
|
+// //得到公钥
|
|
|
|
+// Key publicKey = keyPair.getPublic();
|
|
|
|
+// String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
|
|
|
|
+// //得到私钥
|
|
|
|
+// Key privateKey = keyPair.getPrivate();
|
|
|
|
+// String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
|
|
|
|
+// Map<String, String> keyPairMap = new HashMap<String, String>();
|
|
|
|
+// keyPairMap.put("publicKey", publicKeyStr);
|
|
|
|
+// keyPairMap.put("privateKey", privateKeyStr);
|
|
|
|
+// return keyPairMap;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// public Map<String, String> createKeys(int keySize) {
|
|
|
|
+// //为RSA算法创建一个KeyPairGenerator对象
|
|
|
|
+// KeyPairGenerator kpg;
|
|
|
|
+// try {
|
|
|
|
+// kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
|
|
|
|
+// } catch (NoSuchAlgorithmException e) {
|
|
|
|
+// throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// //初始化KeyPairGenerator对象,密钥长度
|
|
|
|
+// kpg.initialize(keySize);
|
|
|
|
+// //生成密匙对
|
|
|
|
+// KeyPair keyPair = kpg.generateKeyPair();
|
|
|
|
+// //得到公钥
|
|
|
|
+// Key publicKey = keyPair.getPublic();
|
|
|
|
+// String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
|
|
|
|
+// //得到私钥
|
|
|
|
+// Key privateKey = keyPair.getPrivate();
|
|
|
|
+// String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
|
|
|
|
+// Map<String, String> keyPairMap = new HashMap<String, String>();
|
|
|
|
+// keyPairMap.put("publicKey", publicKeyStr);
|
|
|
|
+// keyPairMap.put("privateKey", privateKeyStr);
|
|
|
|
+//
|
|
|
|
+// return keyPairMap;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * 得到公钥
|
|
|
|
+// *
|
|
|
|
+// * @param publicKey 密钥字符串(经过base64编码)
|
|
|
|
+// * @throws Exception
|
|
|
|
+// */
|
|
|
|
+// public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
|
+// //通过X509编码的Key指令获得公钥对象
|
|
|
|
+// KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
+// X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
|
|
|
|
+// RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
|
|
|
|
+// return key;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * 得到私钥
|
|
|
|
+// *
|
|
|
|
+// * @param privateKey 密钥字符串(经过base64编码)
|
|
|
|
+// * @throws Exception
|
|
|
|
+// */
|
|
|
|
+// public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
|
+// //通过PKCS#8编码的Key指令获得私钥对象
|
|
|
|
+// KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
+// PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
|
|
|
|
+// RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
|
+// return key;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * 公钥加密
|
|
|
|
+// *
|
|
|
|
+// * @param data
|
|
|
|
+// * @param publicKey
|
|
|
|
+// * @return
|
|
|
|
+// */
|
|
|
|
+// public static String publicEncrypt(String data, RSAPublicKey publicKey) {
|
|
|
|
+// try {
|
|
|
|
+// Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
|
|
|
+// cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
|
|
+// return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// /**
|
|
|
|
+// * RSA算法使用私钥对数据生成数字签名
|
|
|
|
+// *
|
|
|
|
+// * @param data 待签名的明文字符串
|
|
|
|
+// * @param key RSA私钥字符串
|
|
|
|
+// * @return RSA私钥签名后的经过Base64编码的字符串
|
|
|
|
+// */
|
|
|
|
+// public static String buildRSASignByPrivateKey(String data, String key) {
|
|
|
|
+// try {
|
|
|
|
+// //通过PKCS#8编码的Key指令获得私钥对象
|
|
|
|
+// PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key));
|
|
|
|
+// KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
+// PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
|
+// //sign
|
|
|
|
+// Signature signature = Signature.getInstance(RSA_ALGORITHM_SIGN);
|
|
|
|
+// signature.initSign(privateKey);
|
|
|
|
+// signature.update(data.getBytes(CHARSET));
|
|
|
|
+// return Base64.encodeBase64URLSafeString(signature.sign());
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// throw new RuntimeException("签名字符串[" + data + "]时遇到异常", e);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * RSA算法使用公钥校验数字签名
|
|
|
|
+// *
|
|
|
|
+// * @param data 参与签名的明文字符串
|
|
|
|
+// * @param key RSA公钥字符串
|
|
|
|
+// * @param sign RSA签名得到的经过Base64编码的字符串
|
|
|
|
+// * @return true--验签通过,false--验签未通过
|
|
|
|
+// */
|
|
|
|
+// public static boolean buildRSAverifyByPublicKey(String data, String key, String sign) {
|
|
|
|
+// try {
|
|
|
|
+// //通过X509编码的Key指令获得公钥对象
|
|
|
|
+// X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key));
|
|
|
|
+// KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
|
|
|
+// PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
|
|
|
|
+// //verify
|
|
|
|
+// Signature signature = Signature.getInstance(RSA_ALGORITHM_SIGN);
|
|
|
|
+// signature.initVerify(publicKey);
|
|
|
|
+// signature.update(data.getBytes(CHARSET));
|
|
|
|
+// return signature.verify(Base64.decodeBase64(sign));
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// throw new RuntimeException("验签字符串[" + data + "]时遇到异常", e);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize) {
|
|
|
|
+// int maxBlock = 0;
|
|
|
|
+// if (opmode == Cipher.DECRYPT_MODE) {
|
|
|
|
+// maxBlock = keySize / 8;
|
|
|
|
+// } else {
|
|
|
|
+// maxBlock = keySize / 8 - 11;
|
|
|
|
+// }
|
|
|
|
+// ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
+// int offSet = 0;
|
|
|
|
+// byte[] buff;
|
|
|
|
+// int i = 0;
|
|
|
|
+// try {
|
|
|
|
+// while (datas.length > offSet) {
|
|
|
|
+// if (datas.length - offSet > maxBlock) {
|
|
|
|
+// buff = cipher.doFinal(datas, offSet, maxBlock);
|
|
|
|
+// } else {
|
|
|
|
+// buff = cipher.doFinal(datas, offSet, datas.length - offSet);
|
|
|
|
+// }
|
|
|
|
+// out.write(buff, 0, buff.length);
|
|
|
|
+// i++;
|
|
|
|
+// offSet = i * maxBlock;
|
|
|
|
+// }
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// throw new RuntimeException("加解密阀值为[" + maxBlock + "]的数据时发生异常", e);
|
|
|
|
+// }
|
|
|
|
+// byte[] resultDatas = out.toByteArray();
|
|
|
|
+// IOUtils.closeQuietly(out);
|
|
|
|
+// return resultDatas;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
|
|
}
|
|
}
|