0% found this document useful (0 votes)
24 views1 page

RSA CryptoSystem - Final

The document shows how to generate an RSA key pair and use it to encrypt and decrypt data in Python. It defines functions to generate an RSA key pair, encrypt data with a public key, and decrypt ciphertext with a private key.

Uploaded by

prafulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views1 page

RSA CryptoSystem - Final

The document shows how to generate an RSA key pair and use it to encrypt and decrypt data in Python. It defines functions to generate an RSA key pair, encrypt data with a public key, and decrypt ciphertext with a private key.

Uploaded by

prafulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

from cryptography.hazmat.primitives.

asymmetric import rsa


from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

def generate_rsa_keypair():
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
public_key = private_key.public_key()
return private_key, public_key

def rsa_encrypt(public_key, plaintext):


ciphertext = public_key.encrypt(
plaintext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return ciphertext

def rsa_decrypt(private_key, ciphertext):


plaintext = private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return plaintext

# Example usage:
private_key, public_key = generate_rsa_keypair()

plaintext = b'Hello, RSA Cryptosystem!'

# Encryption
ciphertext = rsa_encrypt(public_key, plaintext)
print("Encrypted:", ciphertext)

# Decryption
decrypted_text = rsa_decrypt(private_key, ciphertext)
print("Decrypted:", decrypted_text.decode())

You might also like