|
|
@@ -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) |
|
|
|
|
|
|
|
} |