rsa加密解密

package security

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

func RsaEncrypt(origData, publicKey []byte) ([]byte, error) {
    // 將密鑰解析成公鑰實(shí)例
    block, _ := pem.Decode(publicKey)
    if block == nil {
        auxiliary.ErrorHanding("非對(duì)稱加密解析公鑰失敗", errors.New("publick key error"), false, auxiliary.DefaultErrorCallBack)
        return nil, errors.New("publick key error")
    }

    // 解析pem.Decode()返回的Block指針實(shí)例
    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) {

    // 將密鑰解析成私鑰實(shí)例
    block, _ := pem.Decode(privatekey)

    if block == nil {
        auxiliary.ErrorHanding("非對(duì)稱加密解析私鑰失敗", errors.New("private key error"), false, auxiliary.DefaultErrorCallBack)
        return nil, errors.New("private key error")
    }

    // 返回的Block指針實(shí)例
    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)
}

測(cè)試用例

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("讀取私鑰錯(cuò)誤")
    }
    defer privateFile.Close()

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

    if err != nil {
        panic("讀取公鑰錯(cuò)誤")
    }
    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))

}

我之前遇到的錯(cuò)誤:

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:<nil> tag:<nil> 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:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} @5

原因是我的代碼有錯(cuò)誤,在私鑰解密那塊:

    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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容