Commit e074b984 authored by 胡建龙's avatar 胡建龙

20220415 项目搭建

parent a0761508
......@@ -20,6 +20,23 @@
<version>5.1</version>
</dependency>
<!-- cmb需要的base64 -->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.chinapay.secss</groupId>
<artifactId>chinapaysecure1_5</artifactId>
<version>1.5</version>
<scope>system</scope>
<systemPath>
${project.basedir}/src/main/webapp/WEB-INF/lib/chinapaysecure1_5.jar
</systemPath>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
......
......@@ -26,7 +26,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
/**
......
......@@ -12,6 +12,7 @@ package com.cmb.controllers;
import com.alibaba.fastjson.JSONObject;
import com.chinapay.service.IHclcChinaPayService;
import com.cmb.service.HclcCmbPolyService;
import com.hand.hap.core.IRequest;
import com.hand.hap.system.controllers.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -30,95 +31,26 @@ import javax.servlet.http.HttpServletRequest;
* @since 1.0.0
*/
@Controller
@RequestMapping(value = {"/r/api", "/"})
public class HclcCmbPolyController extends BaseController {
@Autowired
IHclcChinaPayService hclcChinaPayService;
HclcCmbPolyService hclcCmbPolyService;
/**
* 4.4签约短信接口
* 获取二维码
*
* @param request
* @param params 4.4签约短信接口
* @param params 还款信息
* @return Map
*/
@ResponseBody
@RequestMapping("/china/pay/send/sign/message")
@RequestMapping("/cmb/poly/get/qrcode")
public JSONObject sendSignMessage(HttpServletRequest request,
@RequestBody(required = true) JSONObject params) {
JSONObject jsonResult = new JSONObject();
IRequest iRequest = createRequestContext(request);
jsonResult = hclcChinaPayService.sendSignMessage(iRequest, params);
jsonResult = hclcCmbPolyService.getQrcode(iRequest, params);
return jsonResult;
}
/**
* 4.5签约接口
*
* @param request
* @param params 4.5签约接口
* @return Map
*/
@ResponseBody
@RequestMapping("/china/pay/send/sign")
public JSONObject sendSign(HttpServletRequest request,
@RequestBody(required = true) JSONObject params) {
JSONObject jsonResult = new JSONObject();
IRequest iRequest = createRequestContext(request);
jsonResult = hclcChinaPayService.sendSign(iRequest, params);
return jsonResult;
}
/**
* 4.9 批量交易文件上传接口
*
* @param request
* @param params 4.9 批量交易文件上传接口
* @return Map
*/
@ResponseBody
@RequestMapping("/china/pay/upload/batch")
public JSONObject uploadBatch(HttpServletRequest request,
@RequestBody(required = true) JSONObject params) {
JSONObject jsonResult = new JSONObject();
IRequest iRequest = createRequestContext(request);
jsonResult = hclcChinaPayService.uploadBatch(iRequest, params);
return jsonResult;
}
/**
* 4.11 批量交易回盘文件内容下载接口
*
* @param request
* @param params 4.11 批量交易回盘文件内容下载接口
* @return Map
*/
@ResponseBody
@RequestMapping("/china/pay/download/batch")
public JSONObject downloadBatch(HttpServletRequest request,
@RequestBody(required = true) JSONObject params) {
JSONObject jsonResult = new JSONObject();
IRequest iRequest = createRequestContext(request);
jsonResult = hclcChinaPayService.downloadBatch(iRequest, params);
return jsonResult;
}
/**
* 4.12 批量交易文件查询接口
*
* @param request
* @param params 4.12 批量交易文件查询接口
* @return Map
*/
@ResponseBody
@RequestMapping("/china/pay/query/batch")
public JSONObject queryBatch(HttpServletRequest request,
@RequestBody(required = true) JSONObject params) {
JSONObject jsonResult = new JSONObject();
IRequest iRequest = createRequestContext(request);
jsonResult = hclcChinaPayService.queryBatch(iRequest, params);
return jsonResult;
}
}
\ No newline at end of file
package com.cmb.util;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.gm.GMNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jcajce.spec.SM2ParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
import org.bouncycastle.jce.spec.ECPublicKeySpec;
import org.bouncycastle.util.encoders.Hex;
import java.io.IOException;
import java.math.BigInteger;
import java.security.*;
import java.util.Arrays;
public class BCUtil {
private final static int RS_LEN = 32;
private static X9ECParameters x9ECParameters = GMNamedCurves.getByName("sm2p256v1");
private static ECDomainParameters ecDomainParameters = new ECDomainParameters(x9ECParameters.getCurve(),x9ECParameters.getG(),x9ECParameters.getN());
private static ECParameterSpec ecParameterSpec = new ECParameterSpec(x9ECParameters.getCurve(),x9ECParameters.getG(),x9ECParameters.getN());
static {
if (Security.getProvider("BC") == null) {
Security.addProvider(new BouncyCastleProvider());
}
}
/**
*
* @param msg
* @param userId
* @param privateKey
* @return r||s,直接拼接byte数组的rs
*/
public static byte[] signSm3WithSm2(byte[] msg, byte[] userId, PrivateKey privateKey){
return rsAsn1ToPlainByteArray(signSm3WithSm2Asn1Rs(msg, userId, privateKey));
}
/**
*
* @param msg
* @param userId
* @param privateKey
* @return rs in <b>asn1 format</b>
*/
public static byte[] signSm3WithSm2Asn1Rs(byte[] msg, byte[] userId, PrivateKey privateKey){
try {
SM2ParameterSpec parameterSpec = new SM2ParameterSpec(userId);
Signature signer = Signature.getInstance("SM3withSM2", "BC");
signer.setParameter(parameterSpec);
signer.initSign(privateKey, new SecureRandom());
signer.update(msg, 0, msg.length);
byte[] sig = signer.sign();
return sig;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
* @param msg
* @param userId
* @param rs r||s,直接拼接byte数组的rs
* @param publicKey
* @return
*/
public static boolean verifySm3WithSm2(byte[] msg, byte[] userId, byte[] rs, PublicKey publicKey){
return verifySm3WithSm2Asn1Rs(msg, userId, rsPlainByteArrayToAsn1(rs), publicKey);
}
/**
*
* @param msg
* @param userId
* @param rs in <b>asn1 format</b>
* @param publicKey
* @return
*/
public static boolean verifySm3WithSm2Asn1Rs(byte[] msg, byte[] userId, byte[] rs, PublicKey publicKey){
try {
SM2ParameterSpec parameterSpec = new SM2ParameterSpec(userId);
Signature verifier = Signature.getInstance("SM3withSM2", "BC");
verifier.setParameter(parameterSpec);
verifier.initVerify(publicKey);
verifier.update(msg, 0, msg.length);
return verifier.verify(rs);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* BC的SM3withSM2签名得到的结果的rs是asn1格式的,这个方法转化成直接拼接r||s
* @param rsDer rs in asn1 format
* @return sign result in plain byte array
*/
private static byte[] rsAsn1ToPlainByteArray(byte[] rsDer){
ASN1Sequence seq = ASN1Sequence.getInstance(rsDer);
byte[] r = bigIntToFixexLengthBytes(ASN1Integer.getInstance(seq.getObjectAt(0)).getValue());
byte[] s = bigIntToFixexLengthBytes(ASN1Integer.getInstance(seq.getObjectAt(1)).getValue());
byte[] result = new byte[RS_LEN * 2];
System.arraycopy(r, 0, result, 0, r.length);
System.arraycopy(s, 0, result, RS_LEN, s.length);
return result;
}
/**
* BC的SM3withSM2验签需要的rs是asn1格式的,这个方法将直接拼接r||s的字节数组转化成asn1格式
* @param sign in plain byte array
* @return rs result in asn1 format
*/
private static byte[] rsPlainByteArrayToAsn1(byte[] sign){
if(sign.length != RS_LEN * 2) throw new RuntimeException("err rs. ");
BigInteger r = new BigInteger(1, Arrays.copyOfRange(sign, 0, RS_LEN));
BigInteger s = new BigInteger(1, Arrays.copyOfRange(sign, RS_LEN, RS_LEN * 2));
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(new ASN1Integer(r));
v.add(new ASN1Integer(s));
try {
return new DERSequence(v).getEncoded("DER");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static byte[] bigIntToFixexLengthBytes(BigInteger rOrS){
// for sm2p256v1, n is 00fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123,
// r and s are the result of mod n, so they should be less than n and have length<=32
byte[] rs = rOrS.toByteArray();
if(rs.length == RS_LEN) return rs;
else if(rs.length == RS_LEN + 1 && rs[0] == 0) return Arrays.copyOfRange(rs, 1, RS_LEN + 1);
else if(rs.length < RS_LEN) {
byte[] result = new byte[RS_LEN];
Arrays.fill(result, (byte)0);
System.arraycopy(rs, 0, result, RS_LEN - rs.length, rs.length);
return result;
} else {
throw new RuntimeException("err rs: " + Hex.toHexString(rs));
}
}
public static BCECPrivateKey getPrivatekeyFromD(BigInteger d){
ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(d, ecParameterSpec);
return new BCECPrivateKey("EC", ecPrivateKeySpec, BouncyCastleProvider.CONFIGURATION);
}
public static BCECPublicKey getPublickeyFromXY(BigInteger x, BigInteger y){
ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(x9ECParameters.getCurve().createPoint(x, y), ecParameterSpec);
return new BCECPublicKey("EC", ecPublicKeySpec, BouncyCastleProvider.CONFIGURATION);
}
}
......@@ -145,3 +145,9 @@ cup.mer.id=000092112092486
cup.upload.file.path=D:\\HCLC_FTP\\CUP_OUT\\
cup.download.file.path=D:\\HCLC_FTP\\CUP_IN\\
cup.init.file.path=F:\\work\\workspace\\IdeaProjects\\hls-support-rlwx\\key\\
cmb.version = 0.0.1
cmb.encoding = UTF-8
cmb.signMethod = 02
cmb.currencyCode = 156
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment