码迷,mamicode.com
首页 > 其他好文 > 详细

golang Rsa

时间:2016-08-11 22:43:33      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

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

  

golang Rsa

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5762879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!