Search code examples
pythoncryptographyfernet

How to generate RSA private key using Fernet


This should be simple (famous last words)

In the terminal, i can run this command:

winpty openssl genrsa -des3 -out my_rsa_key_pair 2048

How can I do the exact same thing using pyca/cryptography ?


Solution

  • The Fernet method is not used as a method for generating RSA keys. Therefore, Fernet-based RSA key generation is not supported by pyca/cryptography. However, you can generate RSA keys in the pyca/cryptography package like this:

    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography.hazmat.backends import default_backend
    
    private_key = rsa.generate_private_key(
        public_exponent=65537, # Commonly used public exponent
        key_size=2048, # Key size in bits
        backend=default_backend()
    )
    
    public_key = private_key.public_key()
    

    Hope this helps.