**RSA加密与解密**
**[生成RSA密钥](https://studygolang.com/articles/12219)**
````golang
package crypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
)
func RsaEncrypt(origData []byte) ([]byte, error) {
publicKey, err := ioutil.ReadFile("public.pem")
if err != nil {
return nil, err
}
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pub := pubInterface.(*rsa.PublicKey)
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
func RsaDecrypt(ciphertext []byte) ([]byte, error) {
privateKey, err := ioutil.ReadFile("private.pem")
if err != nil {
return nil, err
}
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error!")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
````
有疑问加站长微信联系(非本文作者))