I have many private keys in a format looking like this:
-----BEGIN ENCRYPTED PRIVATE KEY-----
...
-----END ENCRYPTED PRIVATE KEY-----
I would like to remove the passphrase from these keys using the python PyCryptodome
lib. I do know the passphrase.
However, I do not know the key type of each key (They are a mix of RSA/DSA/ECC keys).
Is there any way to detect the key type, so that I can use the correct procedure to import/export the key?
(e.g. Crypto.PublicKey.RSA.import_key
, Crypto.PublicKey.ECC.import_key
)
Currently, I'm doing this, which is rather ugly:
try:
rsa_key_decrypted = RSA.import_key(key, key_passphrase)
decrypted_key = rsa_key_decrypted.export_key(pkcs=8).decode('ascii')
except:
try:
dsa_key_decrypted = DSA.import_key(key, key_passphrase)
decrypted_key = dsa_key_decrypted.export_key().decode('ascii')
except:
ecc_key_decrypted = ECC.import_key(key, key_passphrase)
decrypted_key = ecc_key_decrypted.export_key(format='PEM')