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