rsa加密解密

package security

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

func RsaEncrypt(origData, publicKey []byte) ([]byte, error) {
    // 将密钥解析成公钥实例
    block, _ := pem.Decode(publicKey)
    if block == nil {
        auxiliary.ErrorHanding("非对称加密解析公钥失败", errors.New("publick key error"), false, auxiliary.DefaultErrorCallBack)
        return nil, errors.New("publick key error")
    }

    // 解析pem.Decode()返回的Block指针实例
    pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err != nil {
        auxiliary.ErrorHanding("公钥解析Pem失败", err, false, auxiliary.DefaultErrorCallBack)
        return nil, err
    }

    pub := pubInterface.(*rsa.PublicKey)

    // RSA算法加密
    return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}

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

    // 将密钥解析成私钥实例
    block, _ := pem.Decode(privatekey)

    if block == nil {
        auxiliary.ErrorHanding("非对称加密解析私钥失败", errors.New("private key error"), false, auxiliary.DefaultErrorCallBack)
        return nil, errors.New("private key error")
    }

    // 返回的Block指针实例
    priv, err := x509.ParsePKCS8PrivateKey(block.Bytes)

    if err != nil {
        auxiliary.ErrorHanding("私钥解析Pem失败", err, false, auxiliary.DefaultErrorCallBack)
        return nil, err
    }

    // RSA算法解密
    return rsa.DecryptPKCS1v15(rand.Reader, priv.(*rsa.PrivateKey), ciphertext)
}

测试用例

func Test_Rsa(t *testing.T) {
    var privateKey []byte
    var publicKey []byte

    privateFile, err := os.Open("/Users/fangqi/go/src/strategy/private.key")
    if err != nil {
        panic("读取私钥错误")
    }
    defer privateFile.Close()

    publicFile, err := os.Open("/Users/fangqi/go/src/strategy/publickey.pem")

    if err != nil {
        panic("读取公钥错误")
    }
    defer publicFile.Close()

    privateKey, _ = ioutil.ReadAll(privateFile)
    publicKey, _ = ioutil.ReadAll(publicFile)

    data, err := RsaEncrypt([]byte("fonzieb"), publicKey)
    if err != nil {
        panic(err)
    }

    fmt.Println("RSA加密", base64.StdEncoding.EncodeToString(data))

    origData, err := RsaDecrypt(data, privateKey)
    if err != nil {
        panic(err)
    }

    fmt.Println("RSA 解密", string(origData))

}

我之前遇到的错误:

asn1: structure error: tags don't match (2 vs {class:0 tag:16 length:13 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} @5 [recovered]
panic: asn1: structure error: tags don't match (2 vs {class:0 tag:16 length:13 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} @5

原因是我的代码有错误,在私钥解密那块:

    priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)


    if err != nil {
        auxiliary.ErrorHanding("私钥解析Pem失败", err, false, auxiliary.DefaultErrorCallBack)
        return nil, err
    }

    // RSA算法解密
    return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)

你可能感兴趣的:(rsa加密解密)