您的位置:首页 > 编程语言 > Go语言

golang Rsa

2016-08-11 22:01 113 查看
package models

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)

func RsaDecrypt(ciphertext []byte, pri_key []byte) ([]byte, error) {

block, _ := pem.Decode(pri_key)
if block == nil {
return nil, errors.New("invalid rsa private key")
}

priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}

return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}

func RsaEncrypt(plaintext []byte, pub_key []byte) ([]byte, error) {

block, _ := pem.Decode(pub_key)
if block == nil {
return nil, errors.New("invalid rsa public key")
}

pubInf, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pub := pubInf.(*rsa.PublicKey)
return rsa.EncryptPKCS1v15(rand.Reader, pub, plaintext)
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: