Showing posts with label RSA Algorithm Encryption and Decryption in Python.. Show all posts
Showing posts with label RSA Algorithm Encryption and Decryption in Python.. Show all posts

Tuesday, September 1, 2020

Implement RSA Algorithm Encryption and Decryption in Python.

  • RSA (Rivest–Shamir–Adleman) is an algorithm used by modern computers to encrypt and decrypt messages. 
  • It is an asymmetric cryptographic algorithm
  • Asymmetric means that there are two different keys. 
  • This is also called public key cryptography, because one of the keys can be given to anyone.

EXAMPLE :-


import math

print("RSA ENCRYPTION/DECRYPTION")
print("*****************************************************")

# Input Prime Numbers
print("PLEASE ENTER THE 'x' AND 'y' VALUES BELOW:")
x = int(input("Enter a prime number for x: "))
y = int(input("Enter a prime number for y: "))
print("*****************************************************")

def prime_check(k):
if (k == 2):
return True
elif ((k < 2) or ((k % 2) == 0)):
return False
elif (k > 2):
for i in range(2, k):
if not (k % i):
return false
return True


check_x = prime_check(x)
check_y = prime_check(y)
while (((check_x == False) or (check_y == False))):
x = int(input("Enter a prime number for x: "))
y = int(input("Enter a prime number for y: "))
check_x = prime_check(x)
check_y = prime_check(y)

n = x * y
print("RSA Modulus(n) is:", n)

r = (x - 1) * (y - 1)
print("Eulers Toitent(r) is:", r)
print("*****************************************************")

def egcd(e, r):
while (r != 0):
e, r = r, e % r
return e

def eugcd(e, r):
for i in range(1, r):
while (e != 0):
a, b = r // e, r % e
if (b != 0):
print("%d = %d*(%d) + %d" % (r, a, e, b))
r = e
e = b

def eea(a, b):
if (a % b == 0):
return (b, 0, 1)
else:
gcd, s, t = eea(b, a % b)
s = s - ((a // b) * t)
print("%d = %d*(%d) + (%d)*(%d)" % (gcd, a, t, s, b))
return (gcd, t, s)

def mult_inv(e, r):
gcd, s, _ = eea(e, r)
if (gcd != 1):
return None
else:
if (s < 0):
print("s=%d. Since %d is less than 0, "
"s = s(modr), i.e., s=%d." % (s, s, s % r))
elif (s > 0):
print("s=%d." % (s))
return s % r

for i in range(1, 1000):
if (egcd(i, r) == 1):
e = i
print("The value of e is:", e)
print("*****************************************************")

print("RSA ALGORITHM:")
eugcd(e, r)
print("END OF THE STEPS USED TO ACHIEVE EUCLID'S ALGORITHM.")
print("*****************************************************")
print("RSA EXTENDED ALGORITHM:")
d = mult_inv(e, r)
print("END OF THE STEPS USED TO ACHIEVE THE VALUE OF 'd'.")
print("The value of d is:", d)
print("*****************************************************")
public = (e, n)
private = (d, n)
print("Private Key is:", private)
print("Public Key is:", public)
print("*****************************************************")

def encrypt(pub_key, n_text):
e, n = pub_key
x = []
m = 0
for i in n_text:
if (i.isupper()):
m = ord(i) - 65
c = (m ** e) % n
x.append(c)
elif (i.islower()):
m = ord(i) - 97
c = (m ** e) % n
x.append(c)
elif (i.isspace()):
spc = 500
x.append(500)
return x


def decrypt(priv_key, c_text):
d, n = priv_key
txt = c_text.split(',')
x = ''
m = 0
for i in txt:
if (i == '500'):
x += ' '
else:
m = (int(i) ** d) % n
m += 65
c = chr(m)
x += c
return x

message = input("You like Encrypted or Decrypted?
                (Separate numbers with ',' for Decryption):")
print("Your Message is ::", message)

choose = input("Type '1' for Encryption and '2' for Decryption.")

if (choose == '1'):
enc_msg = encrypt(public, message)
print("Your Encrypted Message is:", enc_msg)
print("Thank you for using the RSA Encryption Algorithm. Goodbye!")

elif (choose == '2'):
print("Your Decrypted Message is:", decrypt(private, message))
print("Thank you for using the RSA Encryption Algorithm. Goodbye!")

else:
print("You entered the wrong option.")
print("Thank you for using the RSA Encryption Algorithm. Goodbye!")

OUTPUT

RSA ENCRYPTION/DECRYPTION
*****************************************************
PLEASE ENTER THE 'x' AND 'y' VALUES BELOW:
Enter a prime number for x: 3
Enter a prime number for y: 5
*****************************************************
RSA Modulus(n) is: 15
Eulers Toitent(r) is: 8
*****************************************************
The value of e is: 999
*****************************************************
RSA ALGORITHM:
8 = 0*(999) + 8
999 = 124*(8) + 7
8 = 1*(7) + 1
END OF THE STEPS USED TO ACHIEVE EUCLID'S ALGORITHM.
*****************************************************
RSA EXTENDED ALGORITHM:
1 = 8*(1) + (-1)*(7)
1 = 999*(-1) + (125)*(8)
s=-1. Since -1 is less than 0, s = s(modr), i.e., s=7.
END OF THE STEPS USED TO ACHIEVE THE VALUE OF 'd'.
The value of d is: 7
*****************************************************
Private Key is: (7, 15)
Public Key is: (999, 15)
*****************************************************
You like Encrypted or Decrypted? (Separate numbers with ',' for Decryption):3,15,20,17,13
Your Message is :: 3,15,20,17,13
Type '1' for Encryption and '2' for Decryption.2
Your Decrypted Message is: MAFIH
Thank you for using the RSA Encryption Algorithm. Goodbye!