// RSA加密数据
func rsaEncrypt(publicKey, origData string) (cipherByte []byte, err error) {
// 读取公钥证书
certSlc, errCert := base64.StdEncoding.DecodeString(publicKey)
if errCert != nil {
lxlog.I("RsaEncrypt DecodeString error!---", errCert)
return
}
//解析成公钥
pubInterface, errCertBody := x509.ParsePKIXPublicKey(certSlc)
if errCertBody != nil {
lxlog.I("RsaEncrypt ParseCertificate error! ---", errCertBody)
return
}
// 提取公钥
rsaPublicKey := pubInterface.(*rsa.PublicKey)
// 对明文进行加密,PKCS(公钥密码标准),#1就是RSA的标准
cipherByte, err = rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, []byte(origData))
return
}
网友评论