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