2
from Crypto.PublicKey import RSA

pub_key = """
-----BEGIN PGP PUBLIC KEY BLOCK-----
mykeyhere
-----END PGP PUBLIC KEY BLOCK-----
"""

public_key_object = RSA.importKey(pub_key)

This throws the following error - RSA key format is not supported

This is a public PGP key generated with RSA-4096 encryption. I am curious why this library could be throwing errors. Could there be invalid characters in the key? Doesn't look like there are any , no forward slashes or whatnot.

5
  • Which Crypto library is this? Commented Sep 14, 2020 at 14:29
  • @AKX pycryptodome.readthedocs.io/en/latest/src/public_key/rsa.html Commented Sep 14, 2020 at 14:30
  • Well, have you tried .strip() on the key data? As it is, it will now contain a leading and trailing newline. Commented Sep 14, 2020 at 14:37
  • @AKX Same issue. Commented Sep 14, 2020 at 14:39
  • 1
    Well, if it helps, you can see the heuristics over here: github.com/Legrandin/pycryptodome/blob/master/lib/Crypto/… It doesn't look like PGP PUBLIC KEYs are explicitly supported; the key.export_key('PEM') call linked in the docs you paste generates a -----BEGIN RSA PRIVATE KEY----- key. Commented Sep 14, 2020 at 14:43

2 Answers 2

1

For PGP you need a PGP specific library. PGP, at least for the public keys, uses it's own defined format. If you look at your cryptographic library then you will find this nugget:

The following formats are supported for an RSA public key:

  • X.509 certificate (binary or PEM format)
  • X.509 subjectPublicKeyInfo DER SEQUENCE (binary or PEM encoding)
  • PKCS#1 RSAPublicKey DER SEQUENCE (binary or PEM encoding)
  • An OpenSSH line (e.g. the content of ~/.ssh/id_ecdsa, ASCII)

So the PGP key format is not supported, only X.509 keys (the certificate contains the subject public key which contains a PKCS#1 public key if RSA is used, like a Matrushka doll) and OpenSSH keys.


Here is the first library that I found: py-pgp, which includes:

from pgp import read_key
key = read_key(data)

for "transferable" keys, which I presume are public keys.

Sign up to request clarification or add additional context in comments.

1 Comment

Also a PGP public key is not necessarily an RSA key at all.
0

try python-gnupg. it takes care of everything for you

pycryptodome is a low level library. we are free to pick and choose the type of key and cipher over there but we use it only if we know what we are doing ^_^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.