0

My intention is to encrypt a text using a PEM formatted public key. (src)

> echo "encrypt this." | openssl rsautl -encrypt -pubin -inkey pub.pem
unable to load Public Key

The same happens if I put the text into a file named txt and run:

> openssl rsautl -encrypt -pubin -inkey pub.pem -ssl -in txt -out txt.enc
unable to load Public Key

(This was suggested in an answer of a similar question: How to use ssh-rsa public key to encrypt a text?)

Why is OpenSSL not able to load the public key?


pub.pem: (src)

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>
mQGiBDp1yy0RBADVlyDewVwltBs7HnHCG3bXlVUODFkn/00TdbM2SPnOAIkj4giB
ylOP7Mg+Hr5y7FIBvmPWx06In6JjNQiSbpshP5YHv57UfE79nEJdWuSTQt/7j7IJ
GkHYtBRHQMIAHMgT8IB5d3gFq52jSa8hw/ixMP09a0Rw8RP9+kOE4s9UrQCg/zVH
IHswdc/mb50PjdeXwnjxQbkD/3lJYEzz8eUlFHB4rVaC1yRi21Lypf0DIMfQg5j9
xBxY4odFJKyf22PeuAjp9roURRIbGIkIGH8eXF+Mav9OqEdD80JbEn1hZuaLk1RF
k1XJjmFRdKXz+Q7JmRdbs3zXXav2cYwalgzEXT5kuXuNlThLTnLoEFop8Hl3xM4/
PdqMBACkkHb07vPY5l429tdXqL00lE6LedlBW4FLjI534QgselsrUxq5U5y0Wg1Z
//a66l5QkyaMrpsHKfkLHdaPOVCs/WeG6eLwD/cUBEM1Y9Yb5DaB0njdZB3Yxcm8
W23hpKjDanb7SbaSA16gBIWRlvrB/qU+MZAj+EXRDJmwMJq2y7QjbmV0aXZhIGNh
ZnRvcmkgPG5ldGl2YWNAb25lYm94LmNvbT6JAE4EEBECAA4FAjp1yy0ECwMCAQIZ
AQAKCRDFpFclYzXzSwiRAJ0S3djCkJJPUalRyE+vWnfnhvJmDgCfTEBN2N6GlGWO
mrOg1tQlZoWbd5q5Ag0EOnXLLRAIAPZCV7cIfwgXcqK61qlC8wXo+VMROU+28W65
Szgg2gGnVqMU6Y9AVfPQB8bLQ6mUrfdMZIZJ+AyDvWXpF9Sh01D49Vlf3HZSTz09
jdvOmeFXklnN/biudE/F/Ha8g8VHMGHOfMlm/xX5u/2RXscBqtNbno2gpXI61Brw
v0YAWCvl9Ij9WE5J280gtJ3kkQc2azNsOA1FHQ98iLMcfFstjvbzySPAQ/ClWxiN
jrtVjLhdONM0/XwXV0OjHRhs3jMhLLUq/zzhsSlAGBGNfISnCnLWhsQDGcgHKXrK
lQzZlp+r0ApQmwJG0wg9ZqRdQZ+cfL2JSyIZJrqrol7DVekyCzsAAgIH+wVFKD3A
FEdEBHqDZuKjLdLJIKHk4gloKeQ60R9NLLFynfIgSvgsii5uWLY9+gZ2FIGnP3Yc
GxZH1HASv+pG1sw0MnhutxZui3E3Mt69Uv1KTlTGYkfS+mXBw4Qr7hXavCkF45we
f/9Qlj6hSKVjy4YcewdvpopM9S4gVcBq+EdTp1negsCyj3YhFiEo0JEL40mnoHX7
HudJBbiBmknmBZOjxzBBeDPcu7fWV/LDCWiFoGg9uWy2KOcIt7sNXVJbukbSGYg2
hzOB2JPaqCqI5+4YfUCumNLd0lktT7S1V3/6xszEnybQL7tMtmrZZFAFHFAwLNPA
bLxdF/b26GbrTT+JAEYEGBECAAYFAjp1yy0ACgkQxaRXJWM180ttbQCg98c40J41
iXkP9CuqGR0LBJ46VNAAnj+5dH9N226fBp5TN0rAyxwBveTK
=0VvA
-----END PGP PUBLIC KEY BLOCK-----

(Ubuntu 14.04.3 LTS)

> openssl version
OpenSSL 1.0.1f 6 Jan 2014
Raffael
  • 1,251

1 Answers1

1

That's not a "PEM-formatted public key". Although the outer armor is similar to PEM, you actually have a PGP keyblock (similar in purpose to SSL X.509 certificates), not a bare RSA key.

(Notice how the first line says it's a "PGP public key". The expected headers would be either BEGIN PUBLIC KEY for PKCS#7 or BEGIN RSA PUBLIC KEY for PEM / PKCS#1.)

In fact, the PGP keyblock doesn't even have a RSA key inside – this particular one contains DSA and ElGamal keys. (DSA is used for making digital signatures; ElGamal is a DH-based encryption system.)

It is technically possible to extract the raw keypairs out of the keyblock, but that doesn't make sense here – if the recipient uses PGP and expects to decrypt messages via PGP, then you should also encrypt them PGP. (Besides, the openssl tool doesn't even support working with ElGamal anyway.)

$ gpg < pub.txt
gpg: armor header: Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>
pub  dsa1024/C5A457256335F34B 2001-01-29
uid                           netiva caftori 
sig        C5A457256335F34B 2001-01-29   [selfsig]
sub  elg2048/9AF9DF1397F431A1 2001-01-29
sig        C5A457256335F34B 2001-01-29   [keybind]

The proper way to use this file is to import it into PGP, GnuPG, or a compatible tool:

$ gpg --import < pub.txt
$ echo "encrypt this" | gpg --armor --recipient C5A457256335F34B --encrypt

(Note however that the keys are slighty corrupted – there must be an empty line after the "Version:" line. You must add it by hand before trying to import the keyblock.)

grawity
  • 501,077