douzi7890
2018-07-24 04:10
浏览 589
已采纳

Golang RSA加密,错误为“无法解析公钥”

I have a trouble when I just want to simply encrypt a short string.

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "log"
)

func main() {

    var pwd = "1234"
    var pwdB = []byte(pwd)

    fmt.Println(pwd)
    fmt.Println(pwdB)

    const pemPublicKey = `-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALE0I2XX+IzlhIfBx2KoYqcxlEU23oje
PTJzJ7GoYyK4R5gCkWV6ltyLN5G+rNkfsAnTObqIJK+sQzcqmm9up88CAwEAAQ==
-----END PUBLIC KEY-----`

    fmt.Println(pemPublicKey)

    block, _ := pem.Decode([]byte(pemPublicKey))
    if block == nil {
        panic("failed to parse PEM block containing the public key")
    }
    fmt.Println(block)

    pkey, _ := x509.ParsePKCS1PublicKey(block.Bytes)
    if pkey == nil {
        panic("failed to parse public key")
    }

    fmt.Println(pkey)

    in, err := rsa.EncryptPKCS1v15(rand.Reader, pkey, pwdB)
    if err != nil {
        log.Fatalf("encrypt: %s", err)
    }

    fmt.Println(in)
}

playground link

The error is :

panic: failed to parse public key
goroutine 1 [running]:
main.main()
    /tmp/sandbox736400947/main.go:36 +0x4a0

It seems that is the process in Parsing the public key, but I don't know what have I done wrong... What should I do next? Thanks :(

图片转代码服务由CSDN问答提供 功能建议

我只想简单加密短字符串时遇到麻烦。 < pre> package main import( “ crypto / rand” “ crypto / rsa” “ crypto / x509” “ encoding / pem” “ fmt” “ log” ) func main(){ var pwd =“ 1234” var pwdB = [] byte(pwd) fmt.Println(pwd) fmt.Println(pwdB) const pemPublicKey ='----- BEGIN PUBLIC KEY ----- \ MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALE0I2XX + IzlhIfBx2KoYqcxlEU23oje PTJzJ7GoYyK4RQn = C-n \ QW = N \ QW = N \ QW \ QW \ QW \ QW \ QW \ QW \ QW \ QW \ QW n fmt.Println(pemPublicKey) 块,_:= pem.Decode([] byte(pemPublicKey)) if block == nil { panic(“无法解析包含公共元素的PEM块 key“) } fmt.Println(block) pkey,_:= x509.ParsePKCS1PublicKey(block.Bytes) ,如果pkey == nil { panic(“无法解析公钥” ) } fmt.Println(pkey) in err:= rsa.EncryptPKCS1v15(rand。 读者,pkey,pwdB) ,如果错误!= nil { log.Fatalf(“ encrypt:%s”,err) } fmt.Println(in) }

游乐场链接

错误是:

 恐慌:解析公钥失败
goroutine 1 [正在运行]:
main.main()
 / tmp / sandbox736400947 /  main.go:36 + 0x4a0 
   
 
 

似乎是解析公钥的过程, 但是我不知道我做错了什么。 。 接下来我该怎么办? 谢谢:(

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douba6361 2018-07-24 06:19
    已采纳

    You need to use x509.ParsePKIXPublicKey, check the error returned by the Parse... function, and perform a type check to make sure it is a RSA key before using it.

    Your code would become:

    pkey, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err != nil {
        panic(err)
    }
    
    rsaKey, ok := pkey.(*rsa.PublicKey)
    if !ok {
        log.Fatalf("got unexpected key type: %T", pkey)
    }
    

    Please don't skip errors. In this case, ParsePKCS1PublicKey would have returned something along the lines of asn1: structure error: tags don't match ... which indicates a bad format.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题