The question/task: Create a Python tool to encrypt messages with RSA.
My reasoning: Here is a comprehensive Python tool to encrypt messages using RSA. Prerequisites
You will need to install the cryptography library:
pip install cryptography
The Python RSA Tool
Save the following code into a file named rsa_tool.py. This tool allows you to generate keys, encrypt strings, and decrypt them to verify the process. ```python
import os
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.backends import default_backend
class RSACipher:
def init(self, public_key_path='public_key.pem', private_key_path='private_key.pem'):
self.public_key_path = public_key_path
self.private_key_path = private_key_path
def generate_keys(self):
"""Generates a new RSA private and public key pair."""
print("Generating RSA key pair (2048 bit)...")
Generate Private Key
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
Serialize Private Key to PEM format
pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
Generate Public Key from Private Key
public_key = private_key.public_key()
Serialize Public Key to PEM format
pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
Save keys to files
Here is a comprehensive Python tool to encrypt messages using RSA.
I have designed this solution using the cryptography library, which is the standard, secure choice for cryptographic operations in Python. It handles the complex mathematics and secure padding (OAEP) required to make RSA safe against common attacks.
Prerequisites
You will need to install the cryptography library:
pip install cryptography
The Python RSA Tool
Save the following code into a file named rsa_tool.py.
This tool allows you to generate keys, encrypt strings, and decrypt them to verify the process.
import os
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.backends import default_backend
class RSACipher:
def __init__(self, public_key_path='public_key.pem', private_key_path='private_key.pem'):
self.public_key_path = public_key_path
self.private_key_path = private_key_path
def generate_keys(self):
"""Generates a new RSA private and public key pair."""
print("Generating RSA key pair (2048 bit)...")
# Generate Private Key
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
# Serialize Private Key to PEM format
pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
# Generate Public Key from Private Key
public_key = private_key.public_key()
# Serialize Public Key to PEM format
pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# Save keys to files