Assignment – 1
Topic : RSA Algorithm in Cryptography
Submitted by : Aditya Ranjan (02)
Rishi Raj Chaurasia (31)
RSA algorithm is asymmetric cryptography algorithm. This
means that it works on two different keys i.e. Public
Key and Private Key. Public Key is given to everyone and
Private key is kept private.
Algorithm:
1. Select two large prime no's “p” and “q”.
2. Calculate n = p*q
3. Calculate Euler's Toitient function ( f ) = (p - 1)*(q - 1)
4. Find a small exponent say “e” such that gcd(e,f) = 1 and
1 < e < f.
5. Find “d” such that (e*d) mod( f ) = 1.
6. If value of plain text is M (should be less than n) then
For Encryption : C = Me mod n
For Decryption : P = Cd mod n
Implementation ( in Python ):
# RSA Algorithm in Cryptography
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
def RSA_Algorithm():
p = int(input('Enter the value of first prime no. (p) : '))
q = int(input('Enter the value of second prime no. (q) : '))
M = int(input('Enter the value of PlainText (less than '+str((p*q))+') : '
))
n = p*q
f = (p-1)*(q-1) # Euler's Toitient function
for e in range(2,f):
if gcd(e,f)== 1:
break
d = 1
while(True):
if((e*d)%f == 1):
break
else:
d += 1
Cipher =pow(M,e)%n # for Encryption
Plain = pow(Cipher,d)%n # for Decryption
print('n = '+str(n)+', e = '+str(e)+', f = '+str(f)+', d = '+str(d)+', cip
her = '+str(Cipher)+', Plain = '+str(Plain))
RSA_Algorithm()
Output:
Enter the value of first prime no. (p) : 23
Enter the value of second prime no. (q) : 89
Enter the value of PlainText (less than 2047) : 1549
n = 2047, e = 3, f = 1936, d = 1291, cipher = 1800, Plain = 1549