Program 1: Simple RSA Encryption & Decryption
from [Link] import RSA
from [Link] import PKCS1_OAEP
# Generate RSA keys
key = [Link](2048)
# Get public and private keys
public_key = [Link]()
private_key = key
# Original message
message = "Hello"
# Encrypt
cipher = PKCS1_OAEP.new(public_key)
encrypted = [Link]([Link]())
print("Original Message:", message)
print("Encrypted Message:", encrypted)
# Decrypt
cipher = PKCS1_OAEP.new(private_key)
decrypted = [Link](encrypted)
print("Decrypted Message:", [Link]())
Output
Original Message: Hello
Encrypted Message:
b'...'
Decrypted Message:
Hello
---
Program 2: Encrypt User Input
from [Link] import RSA
from [Link] import PKCS1_OAEP
key = [Link](2048)
public_key = [Link]()
private_key = key
message = input("Enter a message: ")
cipher = PKCS1_OAEP.new(public_key)
encrypted = [Link]([Link]())
print("\nEncrypted Message:")
print(encrypted)
cipher = PKCS1_OAEP.new(private_key)
decrypted = [Link](encrypted)
print("\nDecrypted Message:")
print([Link]())
Sample Output
Enter a message: Good Morning
Encrypted Message:
b'...'
Decrypted Message:
Good Morning
---
Program 3: Display Public and Private Keys
from [Link] import RSA
key = [Link](2048)
print("Public Key:\n")
print([Link]().export_key().decode())
print("\nPrivate Key:\n")
print(key.export_key().decode())
Output
Public Key:
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
Private Key:
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----
---
Program 4: Encrypt a Number
from [Link] import RSA
from [Link] import PKCS1_OAEP
key = [Link](2048)
public_key = [Link]()
private_key = key
number = "12345"
cipher = PKCS1_OAEP.new(public_key)
encrypted = [Link]([Link]())
print("Encrypted Number:")
print(encrypted)
cipher = PKCS1_OAEP.new(private_key)
decrypted = [Link](encrypted)
print("\nDecrypted Number:")
print([Link]())
Output
Encrypted Number:
b'...'
Decrypted Number:
12345
---
Program 5: Encrypt Student Details
from [Link] import RSA
from [Link] import PKCS1_OAEP
key = [Link](2048)
public_key = [Link]()
private_key = key
name = input("Enter Student Name: ")
roll = input("Enter Roll Number: ")
data = "Name: " + name + ", Roll No: " + roll
cipher = PKCS1_OAEP.new(public_key)
encrypted = [Link]([Link]())
print("\nEncrypted Data:")
print(encrypted)
cipher = PKCS1_OAEP.new(private_key)
decrypted = [Link](encrypted)
print("\nDecrypted Data:")
print([Link]())
Sample Output
Enter Student Name: John
Enter Roll Number: 101
Encrypted Data:
b'...'
Decrypted Data:
Name: John, Roll No: 101
---
Program 6: Check RSA Encryption and Decryption
from [Link] import RSA
from [Link] import PKCS1_OAEP
key = [Link](2048)
public_key = [Link]()
private_key = key
message = "RSA Practical"
cipher = PKCS1_OAEP.new(public_key)
encrypted = [Link]([Link]())
cipher = PKCS1_OAEP.new(private_key)
decrypted = [Link](encrypted).decode()
if message == decrypted:
print("RSA Encryption and Decryption Successful")
else:
print("RSA Failed")
Output
RSA Encryption and Decryption Successful