You can use the cryptography
library to generate a pair of RSA keys and save the public and private keys as .pem files respectively. Here is a sample code:
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa # Generate RSA key pair private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=default_backend() ) # Get the public key public_key = private_key.public_key() # Save the private key as a .pem file pem_data_private = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) with open("private_key.pem", "wb") as pem_file_private: pem_file_private.write(pem_data_private) print("Private key saved as private_key.pem") # Save the public key as a .pem file pem_data_public = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) with open("public_key.pem", "wb") as pem_file_public: pem_file_public.write(pem_data_public) print("Public key saved as public_key.pem")
This code generates a 2048-bit RSA private key and obtains the corresponding public key through the private key. Then, it saves the private key and public key as private_key.pem
and public_key.pem
files respectively. The private key file does not use any encryption algorithm (serialization.NoEncryption()
), because in real applications, a password is usually used to protect the private key. If you need to password-protect the private key, you can provide corresponding values in the serialization.PrivateFormat.PKCS8
and encryption_algorithm
parameters.
You can use the public key from the generated RSA key pair to encrypt data, and then use the private key to decrypt it. The following is a sample code that demonstrates how to use the cryptography
library for encryption and decryption:
# pip install cryptography from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa # Generate RSA key pair private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=default_backend() ) # Get the public key public_key = private_key.public_key() # Save the private key as a .pem file pem_data_private = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) with open("rsa_private_key.pem", "wb") as pem_file_private: pem_file_private.write(pem_data_private) print("Private key saved as rsa_private_key.pem") # Save the public key as a .pem file pem_data_public = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) with open("rsa_public_key.pem", "wb") as pem_file_public: pem_file_public.write(pem_data_public) print("Public key saved as rsa_public_key.pem") from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.asymmetric import padding #Load public key with open("rsa_public_key.pem", "rb") as pem_file_public: public_key = serialization.load_pem_public_key( pem_file_public.read(), backend=default_backend() ) #Load private key with open("rsa_private_key.pem", "rb") as pem_file_private: private_key = serialization.load_pem_private_key( pem_file_private.read(), password=None, # If the private key uses a password, the password needs to be provided here. backend=default_backend() ) #Data to be encrypted data_to_encrypt = b"51C1F8F6B1C072A82F500811FF8220F6" # Encrypt data using public key encrypted_data = public_key.encrypt( data_to_encrypt, padding=padding.PKCS1v15() ) print("Encrypted data:", encrypted_data.hex()) # Decrypt data using private key decrypted_data = private_key.decrypt( encrypted_data, padding=padding.PKCS1v15() ) print("Decrypted data:", decrypted_data.decode("utf-8"))
In this example, the saved public and private keys are loaded first. The data is then encrypted using the public key and the encrypted data is decrypted using the private key. Please note that the same padding scheme needs to be used for encryption and decryption, and OAEP (Optimal Asymmetric Encryption Padding) is used here.
In real applications, you may need to better manage keys and handle exceptions while ensuring the security of private keys.
Verification OK
To convert the RSA public key exponent (E) and modulus (N) into .pem format, you can use the cryptography
library to generate an RSA key pair and save the public key as .pem files. First, you need to install the cryptography
library. You can install it using the following command:
pip install cryptography
You can then use the following Python code to generate an RSA key pair and save the public key as a .pem file:
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa # Public key exponent (E) and modulus (N) e = 0x00010001 n = 0x88953D31471F761D6F4EA662C5E041DABA36FEBD21F39C741F194869A5C510D70934DD98C308403726CA77604B25DCE8649F6911D4670A99362C5F3F64A0BA92 7AEB64E0F60E02CCE1D0BA4770A1FC0D5E46A30D7EC82F043C21F4D8D86B23298F914144ECD2B1E11D1D37016DBC57693412A2899DC15BEDD998156C605DCE009B6901BC1 497ED41F3DEF4EFD6709F205A046481442572780CEC0F55260E24E8DCA6FA294181B5D3B9B30F391E0BAEBE573FD85948DA9E7BD5ABDA7ECAA835CD8F5C787BF043107DF4A34F0 A875D3DDB22D58085881494BEDA413FAA5E7E7AF1E9B4B6B84F4DA8059C5F1DC0423E448FED7DEC9668E8105439CC9619EC7E99 # Convert integer to byte array e_bytes = e.to_bytes((e.bit_length() + 7) // 8, byteorder='big') n_bytes = n.to_bytes((n.bit_length() + 7) // 8, byteorder='big') # Generate RSA public key public_key = rsa.RSAPublicNumbers(e, n).public_key(backend=default_backend()) # Save the public key as a .pem file pem_format = serialization.PublicFormat.SubjectPublicKeyInfo pem_data = public_key.public_bytes(encoding=serialization.Encoding.PEM, format=pem_format) with open("public_key.pem", "wb") as pem_file: pem_file.write(pem_data) print("Public key saved as public_key.pem")
This code uses the cryptography
library to convert the given public key exponent (E) and modulus (N) into an RSA public key object, and then saves it as a .pem file. Finally, you can find a file named public_key.pem
in the directory where the script is located, which contains your RSA public key.