I am trying to load a passphrase-protected private SSH key from a file using the cryptography Python module. However, I'm not exactly sure how to proceed. The following yields a
Crypto.Util.Padding.PaddingError: Padding is incorrect. error.
def importPrivateKey(private_key,passphrase): """ Imports the private key in an RSA object and generates the hash. """ # -- Test with pgcrypto (now deprecated) -- WORKS with open(private_key, 'r') as private_key_file: key = RSA.importKey(private_key_file.read(),passphrase=passphrase) print(key) # -- Test with cryptography (active) -- FAILS with open(private_key,'r') as private_key_file: key = load_pem_private_key(private_key_file.read(),password=passphrase,backend=RSABackend()) print(key) SecureString.clearmem(passphrase) return key
You can see from the snippet that I have working solution using the pycrypto module, but this module has not been updated in 5 years now. The code that uses cryptography fails with the aforementioned error.
I know there is
load_public_ssh_key deserialization function, but this seems to operate on decrypted keys, hence my choice of
Is it possible to load both private/public keys with the cryptography module?