|
|
@@ -1,280 +0,0 @@
|
|
|
-package com.jzg.quotation.huatai.test;
|
|
|
-
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import org.apache.commons.collections.map.LinkedMap;
|
|
|
-
|
|
|
-import javax.crypto.Cipher;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.security.*;
|
|
|
-import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
-import java.security.spec.X509EncodedKeySpec;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * 签名工具
|
|
|
- */
|
|
|
-public class SignatureUtils {
|
|
|
- /**
|
|
|
- * 加密算法RSA
|
|
|
- */
|
|
|
- public static final String KEY_ALGORITHM = "RSA";
|
|
|
-
|
|
|
- /**
|
|
|
- * 签名算法
|
|
|
- */
|
|
|
- public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
|
|
|
-
|
|
|
- /**
|
|
|
- * RSA最大加密明文大小
|
|
|
- */
|
|
|
- private static final int MAX_ENCRYPT_BLOCK = 117;
|
|
|
-
|
|
|
- /**
|
|
|
- * RSA最大解密密文大小
|
|
|
- */
|
|
|
- private static final int MAX_DECRYPT_BLOCK = 128;
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 加签
|
|
|
- * <p>
|
|
|
- * 对于<b>银保信</b>,publicKey是指合作方的公钥,privateKey是指银保信的私钥<br>
|
|
|
- * 对于<b>合作方</b>,publicKey是指银保信的公钥,privateKey是指合作方的私钥<br>
|
|
|
- *
|
|
|
- * @param requestContent 报文原文
|
|
|
- * @return 加密加签后的返回报文
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public String sign(String requestContent, String privateKey) {
|
|
|
- JSONObject requestJSONObject = JSON.parseObject(requestContent);
|
|
|
- //加签
|
|
|
- String signContent = getSignContent(requestJSONObject);// 所有key排序后放入LingkedMap后map.toString()
|
|
|
- return sign(signContent.getBytes(), privateKey);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 验签
|
|
|
- *
|
|
|
- * @param request
|
|
|
- * @param sign
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public boolean checkSign(String request, String sign, String publicKey) {
|
|
|
- try {
|
|
|
- JSONObject requestJSONObject = JSON.parseObject(request);
|
|
|
- String signContent = getSignContent(requestJSONObject);// 所有key排序后放入LingkedMap后map.toString()
|
|
|
- return verify(signContent.getBytes(), publicKey, sign);
|
|
|
- } catch (Exception e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 加密
|
|
|
- * <p>
|
|
|
- * 对于<b>请求者</b>,publicKey是指华泰的公钥,privateKey是指请求者的私钥<br>
|
|
|
- * 对于<b>华泰</b>,publicKey是指请求者的公钥,privateKey是指华泰的私钥<br>
|
|
|
- *
|
|
|
- * @param bizContent 报文原文
|
|
|
- * @return 加密加签后的返回报文
|
|
|
- * @throws UnsupportedEncodingException
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public String encrypt(String bizContent, String publicKey) throws UnsupportedEncodingException{
|
|
|
- //加密
|
|
|
- return Base64Utils.encode(SignatureUtils.encryptByPublicKey(bizContent.getBytes("utf-8"), publicKey));//对业务数据进行加密
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 解密
|
|
|
- *
|
|
|
- * @param requestContent
|
|
|
- * @param privateKey
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public String decrypt(String requestContent, String privateKey){
|
|
|
- try {
|
|
|
- return new String(SignatureUtils.decryptByPrivateKey(Base64Utils.decode(requestContent), privateKey));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return "";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 封装待验签的内容
|
|
|
- *
|
|
|
- * @param sortedParam
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getSignContent(Map<String, Object> sortedParam) {
|
|
|
- LinkedMap map = new LinkedMap();
|
|
|
- List<String> keys = new ArrayList<>(sortedParam.keySet());
|
|
|
- Collections.sort(keys);
|
|
|
- for (int i = 0; i < keys.size(); i++) {
|
|
|
- String key = keys.get(i);
|
|
|
- String value = sortedParam.get(key).toString();
|
|
|
- map.put(key, value);
|
|
|
- }
|
|
|
- return map.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 校验数字签名
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param data 已加密数据
|
|
|
- * @param publicKey 公钥(BASE64编码)
|
|
|
- * @param sign 数字签名
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static boolean verify(byte[] data, String publicKey, String sign) {
|
|
|
- try {
|
|
|
- byte[] keyBytes = Base64Utils.decode(publicKey);
|
|
|
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
|
|
- PublicKey publicK = keyFactory.generatePublic(keySpec);
|
|
|
- Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
|
|
|
- signature.initVerify(publicK);
|
|
|
- signature.update(data);
|
|
|
- return signature.verify(Base64Utils.decode(sign));
|
|
|
- } catch (Exception e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * <P>
|
|
|
- * 私钥解密
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param encryptedData 已加密数据
|
|
|
- * @param privateKey 私钥(BASE64编码)
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey){
|
|
|
- ByteArrayOutputStream out = null;
|
|
|
- byte[] decryptedData = null;
|
|
|
- try {
|
|
|
- out = new ByteArrayOutputStream();
|
|
|
- byte[] keyBytes = Base64Utils.decode(privateKey);
|
|
|
- PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
|
|
- Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
- Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
|
|
- cipher.init(Cipher.DECRYPT_MODE, privateK);
|
|
|
- int inputLen = encryptedData.length;
|
|
|
- int offSet = 0;
|
|
|
- byte[] cache = null;
|
|
|
- int i = 0;
|
|
|
- // 对数据分段解密
|
|
|
- while (inputLen - offSet > 0) {
|
|
|
- if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
|
|
|
- cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
|
|
|
- } else {
|
|
|
- cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
|
|
|
- }
|
|
|
- out.write(cache, 0, cache.length);
|
|
|
- i++;
|
|
|
- offSet = i * MAX_DECRYPT_BLOCK;
|
|
|
- }
|
|
|
- decryptedData = out.toByteArray();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (null != out) {
|
|
|
- try {
|
|
|
- out.close();
|
|
|
- } catch (IOException e) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return decryptedData;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 公钥加密
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param data 源数据
|
|
|
- * @param publicKey 公钥(BASE64编码)
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static byte[] encryptByPublicKey(byte[] data, String publicKey){
|
|
|
- ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
- byte[] encryptedData = null;
|
|
|
- try {
|
|
|
- byte[] keyBytes = Base64Utils.decode(publicKey);
|
|
|
- X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
|
|
- Key publicK = keyFactory.generatePublic(x509KeySpec);
|
|
|
- // 对数据加密
|
|
|
- Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
|
|
- int inputLen = data.length;
|
|
|
- int offSet = 0;
|
|
|
- byte[] cache;
|
|
|
- int i = 0;
|
|
|
- // 对数据分段加密
|
|
|
- while (inputLen - offSet > 0) {
|
|
|
- if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
|
|
|
- cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
|
|
|
- } else {
|
|
|
- cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
|
|
- }
|
|
|
- out.write(cache, 0, cache.length);
|
|
|
- i++;
|
|
|
- offSet = i * MAX_ENCRYPT_BLOCK;
|
|
|
- }
|
|
|
- encryptedData = out.toByteArray();
|
|
|
- out.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- out.close();
|
|
|
- } catch (IOException e) {
|
|
|
- }
|
|
|
- }
|
|
|
- return encryptedData;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 用私钥对信息生成数字签名
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param data 已加密数据
|
|
|
- * @param privateKey 私钥(BASE64编码)
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static String sign(byte[] data, String privateKey) {
|
|
|
- try {
|
|
|
-
|
|
|
- byte[] keyBytes = Base64Utils.decode(privateKey);
|
|
|
- PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
|
|
- PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
- Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
|
|
|
- signature.initSign(privateK);
|
|
|
- signature.update(data);
|
|
|
- return Base64Utils.encode(signature.sign());
|
|
|
- } catch (Exception e) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- }
|
|
|
-}
|