Implementing RSA Algorithm using Python

RSA (Rivest-Shamir-Adleman) is a widely used public-key cryptosystem for secure communication. It involves the generation of a public key and a private key. The public key is used for encryption, while the private key is used for decryption.

Implementation of the RSA Algorithm

Here's a step-by-step guide to implementing RSA encryption in Python using the cryptography library:

Install the cryptography library
pip install cryptography
Generate RSA key pair
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization def generate_key_pair(): private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=default_backend() ) public_key = private_key.public_key() private_key_bytes = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) public_key_bytes = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) return private_key_bytes, public_key_bytes private_key, public_key = generate_key_pair() Save the keys to files (optional) with open('private_key.pem', 'wb') as private_key_file: private_key_file.write(private_key) with open('public_key.pem', 'wb') as public_key_file: public_key_file.write(public_key) Load keys from files (optional) def load_private_key(file_path): with open(file_path, 'rb') as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None, backend=default_backend() ) return private_key def load_public_key(file_path): with open(file_path, 'rb') as key_file: public_key = serialization.load_pem_public_key( key_file.read(), backend=default_backend() ) return public_key loaded_private_key = load_private_key('private_key.pem') loaded_public_key = load_public_key('public_key.pem')
Encrypt and Decrypt
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding def encrypt(plaintext, public_key): ciphertext = public_key.encrypt( plaintext.encode('utf-8'), padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) return ciphertext def decrypt(ciphertext, private_key): plaintext = private_key.decrypt( ciphertext, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) return plaintext.decode('utf-8') message = "Hello, RSA!" # Encrypt using the public key encrypted_message = encrypt(message, loaded_public_key) print(f"Encrypted Message: {encrypted_message}") # Decrypt using the private key decrypted_message = decrypt(encrypted_message, loaded_private_key) print(f"Decrypted Message: {decrypted_message}")

Make sure to handle key storage and distribution securely in a real-world scenario. This example demonstrates the basics of RSA encryption using the cryptography library in Python.

Points to Remember:
  1. This is a simplified implementation for demonstration purposes.
  2. In real-world applications, you'd use more robust libraries for prime number generation, key management, and padding schemes.
  3. RSA is computationally expensive, so consider using it for key exchange and symmetric encryption for bulk data.
Full Source | Python
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.asymmetric import rsa, padding import sys def generate_key_pair(): private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=default_backend() ) public_key = private_key.public_key() private_key_bytes = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) public_key_bytes = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) return private_key_bytes, public_key_bytes def encrypt(plaintext, public_key): ciphertext = public_key.encrypt( plaintext.encode('utf-8'), padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) return ciphertext def decrypt(ciphertext, private_key): plaintext = private_key.decrypt( ciphertext, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) return plaintext.decode('utf-8') if len(sys.argv) != 2: print("Usage: python script.py <message>") sys.exit(1) message = sys.argv[1] # Step 1: Generate RSA key pair private_key, public_key = generate_key_pair() # Step 2: Encrypt using the public key encrypted_message = encrypt(message, serialization.load_pem_public_key(public_key, backend=default_backend())) print(f"\nEncrypted Message: {encrypted_message.decode('latin-1')}") # Step 3: Decrypt using the private key decrypted_message = decrypt(encrypted_message, serialization.load_pem_private_key(private_key, password=None, backend=default_backend())) print(f"\nDecrypted Message: {decrypted_message}")

Now you can run the script from the command line, providing the message as a command-line argument:

python script.py "Hello, RSA!"

Conclusion

RSA (Rivest-Shamir-Adleman) is a widely used public-key cryptosystem that involves the generation of a pair of keys—a public key for encryption and a private key for decryption. It relies on the mathematical difficulty of factoring the product of two large prime numbers to secure the communication and is commonly used in secure data transmission and digital signatures.