RSA ENCRYPTION & DECRYPTION - PE
p = 11
q = 13
#Compute n and phi(n)
n = p * q
phi = (p - 1) * (q - 1)
#Choose e such that 1 < e < phi(n) and gcd(e, phi) == 1
e = 7
#Modular inverse function to compute d
def modinv(a, m):
for x in range(1, m):
if (a * x) % m == 1:
return x
return None
#Calculate private key d
d = modinv(e, phi)
#Display public and private keys
print(f"Public key (e, n): ({e}, {n})")
print(f"Private key (d, n): ({d}, {n})")
#Encrypt the message m
m = 9 # This is the message (could be any number)
cipher = pow(m, e, n) # RSA encryption formula: c = m^e % n
print(f"Encrypted message: {cipher}")
#Decrypt the ciphertext to get back the original message
decrypted = pow(cipher, d, n) # RSA decryption formula: m = c^d % n
print(f"Decrypted message: {decrypted}")