Skip to content

Instantly share code, notes, and snippets.

@xieyuschen
Created December 10, 2020 17:32
Show Gist options
  • Save xieyuschen/013df54569060a30db4798e79763c700 to your computer and use it in GitHub Desktop.
Save xieyuschen/013df54569060a30db4798e79763c700 to your computer and use it in GitHub Desktop.
Unknown type of public key

How to specify which way the key pair specified?

Here I record how to generate a key-pair to encrypt and decrypt. So I'm sure it use rsa pkcs1v15 to encrypt,and I can do encrypt and decrypt directly as the post above.
But when it comes to a given public-private key pair,thing seems unnormal:

Problem I encoutered:

When I encountered key pair in thoes files(pay attention that this is base64 encoding):

  • public.pem:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNI1fUIU+VxpdWBRCSazCD7rfZ
2Hofpm/oN50z/9NTFkr1QrIGNSg+Wz7VdJ692gNDYErH1xMxMUm/LwAFo/u4trzp
jDZBaJDgzxdD7qryrPkwOuQAJZObxz1CXsD9VE5qvYud67M+OCbuYG2gRpixfmso
qV3Z1WsAOWrKUlVWfQIDAQAB
-----END PUBLIC KEY-----
  • private.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXwIBAAKBgQDNI1fUIU+VxpdWBRCSazCD7rfZ2Hofpm/oN50z/9NTFkr1QrIG
NSg+Wz7VdJ692gNDYErH1xMxMUm/LwAFo/u4trzpjDZBaJDgzxdD7qryrPkwOuQA
JZObxz1CXsD9VE5qvYud67M+OCbuYG2gRpixfmsoqV3Z1WsAOWrKUlVWfQIDAQAB
AoGBAKvUnw0hmfhrkPiM5YTAJLNeksH3gtRgJfzuGNAlym4ejktCwrxQapVQ+SDz
vsRoibM2RW1/yqO6FXv/cpVG9uIQmq3jbZ9uO2lH+j/DGMaqMU7n3g+QJjzqyt4h
fRFq2HH6sldolHkGNiy3/uXm35mcJEFTjYUinVMZHZK07t5BAkEA/tkvPFPI279h
mOBTuJb1L8ln8d4OvHzvHa0zKgI7bH6L/a4j3VGskQeqdChA7hxllPcX5o/jlSdO
zp4fQ3n54wJBAM4Qpv7zGLDOZxiAeLE5xhyZZKINOHOB6H9xuX3jmmXdBFcWUpvx
V6DIoAFCBk3wd84BZVYsFYN64XA+P/m73B8CQQDFIywu9oEBu+G7aIxa1iuEc8A8
kRHFLL+rrTdO37soChwweGy132AmME/i2uCfUMgjm6CZceUSk/2aRybYA5sRAkEA
u4bKArBGIvjjN5gKvfX+xZ4Ox67SVcO+SuS60sbjhpnZTkuhSAsdHNQXUK2+QQV6
cb4xc3dQ6MNWD6iG3gVJLwJBAIAmRr8LqxzBSltLUAorQzEp2b98PCvDX4PHAQWy
mz8GjqvXsmNO1Tp5ixtvW6NOqPddljrDx4uS20hgezqwY0k=
-----END RSA PRIVATE KEY-----

So I used codes like this:

func main() {

	publicKeyStr,_:=ioutil.ReadFile("public.pem")
	block, _ := pem.Decode([]byte(publicKeyStr))
	key,_:=x509.ParsePKCS1PublicKey(block.Bytes)
	chiper,_:=rsa.EncryptPKCS1v15(rand.Reader,key,[]byte("helloworld"))
	fmt.Println("Chiper is: ")
	fmt.Println(string(chiper))

	privateKeyStr,_:=ioutil.ReadFile("private.pem")
	priblock,_:=pem.Decode([]byte(privateKeyStr))
	priKey,_:=x509.ParsePKCS1PrivateKey(priblock.Bytes)
	plaintext,_:=rsa.DecryptPKCS1v15(rand.Reader,priKey,chiper)
	fmt.Println("PlainText is: ",string(plaintext))
}

But unluckily Ithe key in key,_:=x509.ParsePKCS1PublicKey(block.Bytes) is nil, and all process below cannot work properly:( when I get err and output it,it tells me:

2020/12/11 01:10:06 x509: failed to parse public key (use ParsePKIXPublicKey instead for this key format)
panic: x509: failed to parse public key (use ParsePKIXPublicKey instead for this key format)

How to solve it?

According the error infomation, I replace ParsePKCS1PublicKey(...) by ParsePKIXPublicKey(...). After reading the intelligent prompt, I use those code to get key info:

	publicKeyStr,_:=ioutil.ReadFile("public.pem")
	block, _ := pem.Decode(publicKeyStr)

	pub,_:=x509.ParsePKIXPublicKey(block.Bytes)
	switch pub := pub.(type) {
	case *rsa.PublicKey:
		fmt.Println("pub is of type RSA:", pub)
	case *dsa.PublicKey:
		fmt.Println("pub is of type DSA:", pub)
	case *ecdsa.PublicKey:
		fmt.Println("pub is of type ECDSA:", pub)
	case ed25519.PublicKey:
		fmt.Println("pub is of type Ed25519:", pub)
	default:
		panic("unknown type of public key")
	}

So from here I can know pub is of type RSA: which means public pem is a type of Rsa key. So I can use assets to get rsa public key like this:

pub,_:=x509.ParsePKIXPublicKey(block.Bytes)
key:=pub.(*rsa.PublicKey)
chiper,_:=rsa.EncryptPKCS1v15(rand.Reader,key,[]byte("helloworld"))

This works quite well:)

The whole code:

func main() {
	publicKeyStr,_:=ioutil.ReadFile("public.pem")
	block, _ := pem.Decode(publicKeyStr)

	pub,_:=x509.ParsePKIXPublicKey(block.Bytes)

	key:=pub.(*rsa.PublicKey)

	chiper,_:=rsa.EncryptPKCS1v15(rand.Reader,key,[]byte("helloworld"))
	fmt.Println("Chiper is: ")
	fmt.Println(string(chiper))

	privateKeyStr,_:=ioutil.ReadFile("private.pem")
	priblock,_:=pem.Decode([]byte(privateKeyStr))
	priKey,_:=x509.ParsePKCS1PrivateKey(priblock.Bytes)
	plaintext,_:=rsa.DecryptPKCS1v15(rand.Reader,priKey,chiper)
	fmt.Println("PlainText is: ",string(plaintext))
}

And it output like this:

Chiper is: 
���cI�l�M���Im�|z��"ݣbY�[x��Z��ǀ��@��~��`
PlainText is:  helloworld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment