I'm getting some strange situation with parsing just-created rsa keys.
In this part I generate new pair and write into file:
rsaKey,err:= rsa.GenerateKey(rand.Reader,2048)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
publicKey := rsaKey.PublicKey
outFile, err := os.Create("./private.pem")
defer outFile.Close()
var privateKey = &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
}
err = pem.Encode(outFile, privateKey)
asn1Bytes, err := asn1.Marshal(publicKey)
var pemkey = &pem.Block{
Type: "PUBLIC KEY",
Bytes: asn1Bytes,
}
pemfile, err := os.Create("./public.pem")
defer pemfile.Close()
err = pem.Encode(pemfile, pemkey)
Next step is reading key from file and parse it:
data, err := ioutil.ReadFile("./public.pem")
block, _ :=pem.Decode(data)
res,err := x509.ParsePKIXPublicKey(block.Bytes)
fmt.Print(err)
But x509.ParsePKIXPublicKey(block.Bytes) return error:
asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:257 isCompound:false}) {optional:false explicit:false application:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} AlgorithmIdentifier @4
playground example
https://play.golang.org/p/djlK8lO5_E2
Please help me solve this issue.