asojidaiod
asojidaiod

Reputation: 93

RSA key format is not supported for python

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.

Upvotes: 2

Views: 2645

Answers (2)

Zach
Zach

Reputation: 958

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 ^_^

Upvotes: 0

Maarten Bodewes
Maarten Bodewes

Reputation: 93948

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.

Upvotes: 1

Related Questions