diff --git a/card_pay/api.go b/card_pay/api.go index c342e63..a17565f 100644 --- a/card_pay/api.go +++ b/card_pay/api.go @@ -5,6 +5,7 @@ import ( "crypto" "crypto/rand" "crypto/rsa" + "crypto/x509" "encoding/base64" "fmt" "golang.org/x/crypto/ssh" @@ -140,3 +141,25 @@ func CardPayGetSign(privateKeyStr string, data string) (string, error) { } return base64.StdEncoding.EncodeToString(sign), nil } +func RsaEncrypts(pubStr, data string) string { + // 公钥加密 + // 读取公钥证书 + certSlc, errCert := base64.StdEncoding.DecodeString(pubStr) + if errCert != nil { + return "" + } + //证书解析 + certBody, errCertBody := x509.ParseCertificate(certSlc) + if errCertBody != nil { + return "" + } + // 提取公钥 + rsaPublicKey := certBody.PublicKey.(*rsa.PublicKey) + // 对明文进行加密,PKCS(公钥密码标准),#1就是RSA的标准 + cipherByte, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, []byte(data)) + if err != nil { + return "" + } + return base64.StdEncoding.EncodeToString(cipherByte) + +} diff --git a/card_pay/card_pay.go b/card_pay/card_pay.go index 56de304..6b457bc 100644 --- a/card_pay/card_pay.go +++ b/card_pay/card_pay.go @@ -4,7 +4,7 @@ package card_pay func BindCard(keyArr, param map[string]string) (string, error) { //param = map[string]string{ // "version": "2.0", - // "mchtOrderNo": "123", //流水号不能为空 + // "mchtOrderNo": "123456", //流水号不能为空 // "memberId": "562902003186055", //分账模式(splitModel)为“1-普通分账”的,该值一律传商户自编的编号。 // "transType": "01", //值可填“01 快捷支付、02 代收”。 不填时默认 01 快捷支付 // "userName": RsaEncrypts("黄加价"), //分账主体商户姓名