Generate RSA Key Pair with Cryptography Library

  • Share this:

Code introduction


This function uses the cryptography library to generate an RSA key pair and returns the private key.


Technology Stack : cryptography

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives.serialization import load_pem_private_key

def generate_rsa_key_pair():
    """
    This function generates a RSA key pair and returns the private key.
    """
    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )
    return private_key                
              
Tags: