首页 > 其他 > 详细

golang Rsa

时间:2016-08-11 22:42:14      阅读:249      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!