Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSSL unable to load Public Key

Trying to encrypt a text message via command line on OSX Yosomite 10.10.2

Created public .pem key like this:

ssh-keygen -f ~/.ssh/id_rsa.pub -e -t PKCS8 > id_rsa.pem

If I try to encrypt myMessage.txt

openssl rsautl -encrypt -inkey ~/.ssh/id_rsa.pem -pubin -in ~/Desktop/myMessage.txt -out ~/Desktop/encrypted.txt

I get unable to load Public key

If I then type:

openssl asn1parse -in id_rsa.pem

Returns: Error: offset too large

But I have no idea how to fix it. What should I change to make it work?

like image 762
caramba Avatar asked Mar 12 '15 13:03

caramba


2 Answers

Still don't know what went wrong in my question but found a solution:

1) Generate RSA key:

$ openssl genrsa -out key.pem 1024 
$ openssl rsa -in key.pem -text -noout 

2) Save public key in pub.pem file:

$ openssl rsa -in key.pem -pubout -out pub.pem 
$ openssl rsa -in pub.pem -pubin -text -noout 

3) Encrypt some data:

$ echo test test test > file.txt 
$ openssl rsautl -encrypt -inkey pub.pem -pubin -in file.txt -out file.bin 

4) Decrypt encrypted data:

$ openssl rsautl -decrypt -inkey key.pem -in file.bin 

It works like a charm

Thanks to Marek Marcola for providing the information http://openssl.6102.n7.nabble.com/Re-Can-I-use-my-own-keys-with-openssl-for-RSA-enc-dec-td12506.html

like image 144
caramba Avatar answered Sep 19 '22 19:09

caramba


I had same problem when I was extracting public key from certificate.

openssl x509 -pubkey -noout -in cert.crt > pubKey.pem

Afterwards, I wanted to print information about key with command below.

openssl rsa -text -pubin -in pubKey.pem

And gets an error: unable to load Public Key

Solution

I opened pubKey.pem in notepad++ and in the Encoding menu was UCS-2 LE BOM selected. So I changed it to UTF-8 encoding. Size of pubKey.pem was half of the original one after changing encoding. Then it works like charm.

Tested in Windows and powershell

like image 25
Štefan Bartoš Avatar answered Sep 17 '22 19:09

Štefan Bartoš