0

I received an RSA public key from a colleague by way of a pem file:

-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAwGbtPQW6k1S1N8GM4UDxwexV4M+5BHcjs/l397/Mjt/J9+yL4PMw
cdLTnO/PHwCne3DdWsxHoZA/MIHRF+K/d2yDJ8DWHIKtbbUx6/1NOoogauwx+Ush
hNsaT3BCCw+ruFiyPCNKE6JN98hdbiP/61Y9OPZw/9m3c0mMtDv0gtjLJ2Q1z308
DGDZOA3+fTo/PEAgoCycVl0WwwN+AFYsj4DRqIlnxbd0PTSqxVBV+T1oaNmN/8jl
E0zOyQNzOHhDFhJJMucodrUOIFmpWSX6Ff2sVaXY81z4oRq3KrVOK4LZkP6cpPdh
s4Qa6ztWuwJ1ZKK6jOZxF0U2015/4njvgQIDAQAB
-----END RSA PUBLIC KEY-----

But when I attempt to encrypt some simple text with it on my Mac

openssl rsautl -encrypt -pubin -inkey "/path/to/file/public.pem" -ssl -in "Some text to encrypt with public key" -out /path/to/EncryptedMessage.txt

OpenSSL throws exception

unable to load Public Key

Furthermore, key validation

openssl rsa -text -pubin < "/path/to/file/public.pem"

also throws exception

unable to load Public Key
3106:error:0906D06C:PEM routines:PEM_read_bio:no start line:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.7/src/crypto/pem/pem_lib.c:648:Expecting: PUBLIC KEY

The public key was generated on a Mac and converted to pem

ssh-keygen -f ~/.ssh/id_rsa.pub -m 'PEM' -e > public.pem

The error message suggests that the public key is not in a valid format. So what am I not understanding and how do I resolve errors?

Edit

hexdump -C -n64 public.pem
00000000  2d 2d 2d 2d 2d 42 45 47  49 4e 20 52 53 41 20 50  |-----BEGIN RSA P|
00000010  55 42 4c 49 43 20 4b 45  59 2d 2d 2d 2d 2d 0a 4d  |UBLIC KEY-----.M|
00000020  49 49 42 43 67 4b 43 41  51 45 41 77 47 62 74 50  |IIBCgKCAQEAwGbtP|
00000030  51 57 36 6b 31 53 31 4e  38 47 4d 34 55 44 78 77  |QW6k1S1N8GM4UDxw|
00000040

Appears to be ASCII with no BOM.

migrated from crypto.stackexchange.com Feb 13 at 17:37

This question came from our site for software developers, mathematicians and others interested in cryptography.

3

The -----BEGIN RSA PUBLIC KEY----- header shows that this is a PKCS#1 file. OpenSSL expects a PKCS#8 file which begins with -----BEGIN PUBLIC KEY----- (note the lack of RSA).

You need to use the -RSAPublicKey_in option to openssl rsa to convert the file from PKCS#1 to PKCS#8 file:

openssl rsa -RSAPublicKey_in -pubin -in /path/to/file/public.pem -out /path/to/file/pkcs8.pem

Once you've done that, you can use your first command using the converted file.

A bit of Googling suggests that the version of OpenSSL on a Mac may not have the -RSAPublicKey_in option, in which case you may need to find a newer version, maybe on another OS.

  • If I remember correctly, before gaining that option, "openssl rsa" dealt only in PKCS#1 format keys (with PKCS#8 only available via "openssl pkey"), so I'm not sure why those versions would have an issue. – grawity Feb 13 at 20:00
  • 1
    @grawity+ in old OpenSSL most commandline operations used algorithm-specific 'traditional' formats for privatekey files (PKCS1 for RSA, adhoc for DSA, SEC1 for EC) with only explicit pkcs8 (and pkcs12 -export) commands using PKCS8; since 1.0.0 in 2010 generic operations like req and pkey use PKCS8 but rsa uses traditional. For publickey files however OpenSSL always used X.509 SPKI format for all algorithms until 1.0.0 when (only) rsa added the -RSAPublicKey_{in,out} options for PKCS1. – dave_thompson_085 Feb 14 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.