Bangladesh Army University of Science
and Technology (BAUST), Saidpur
Assignment
Department : Computer Science and Engineering.
Course Title : CSE 4103
Course No :Computer Graphics.
Assignment No : 02
Assignment Name: RSA Algorithm in Cryptography.
Submitted By Submitted To
Name of the Teacher: Ananna Hoque
Name: Khondoker Abu Naim
Shathi
Id: 200101103
Level: 4 Term: 1
Designation: Lecturer
Date of Experiment:
Date of submission:
Signature: …………………………………………
RSA Algorithm in Cryptography
RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means
that it works on two different keys i.e. Public Key and Private Key. As the name
describes that the Public Key is given to everyone and the Private key is kept private.
The algorithm works as follow:
1. Select two prime numbers p and q where p≠q.
2. Calculate n=p*q
3. Calculate ⱷ(n)=(p-1)*(q-1).
4. Select such that, e is relatively prime to ⱷ(n)
i.c. (ⱷ(n),e = 1 and 1<e< ⱷ(n)
5. Calculate d= 𝑒 −1 mod ⱷ(n) or ed=1 mod ⱷ(n)
6. Public key = {e,n},private key={d,n}
7. Find out cipher text using this formula,
C=𝑃𝑒 mod n where, P< n and
C= Cipher text, P =plain text, e= Encryption key and n=block size.
8. P= 𝐶 𝑑 mode n. Plain text can be obtain using the given formula.
Where, d= decryption key.
RSA Algorithm step by step explanation:
Step - 1: Select two prime numbers p and q where p≠q.
example: Two prime numbers p = 3, q = 7
Step-2: Calculate n =p* q.
example: n=p* q= 3 * 7 = 21.
Step - 3: Calculate ⱷ(n)= (p - 1) * (q-1).
example: ⱷ(n) = (3-1) * (7-1)=2* 6=12.
Step - 4: Select e such that, e is relatively prime to ⱷ(n)
i.e. (e, ⱷ(n)) = 1 and 1 < e < ⱷ(n)
example: Select e = 3 gcd (3, 12) = 1 .
Step-5: Calculate d= 𝑒 −1 mod ⱷ(n) or ed = 1 mod ⱷ(n).
example: d = ((k*phi)+1)/e =((2*12)+1) / 5 = 5
Step-6: Public key ={e,n}, private key ={d,n}
Step-7:Find out the cipher text using the formula,
C=𝑃𝑒 mod n where P<n where C= cipher text, P= Plain text e= Encryption key and n=
block size
Step-8: P=𝐶 𝑑 mod n Plain text P can be obtain using the given formula where d= decryption key.
Code with explanation and Output:
import math
def gcd(a, h):
temp = 0
while(1):
temp = a % h
if (temp == 0):
return h
a = h
h = temp
p=3
q=7 #here p and q is a prime number abd both are not same
n= p*q #multiply both prime number
print("n =", n)
phi=(p-1)*(q-1) #hare calculate phi and its not a prime number
print("phi =", phi)
e=3 #e is the relativly prime of phi
while(e<phi): # e must be co-prime to phi and smaller
than phi.
if (math.gcd(e, phi) == 1):
break
else:
e= e+1
print("e =", e)
# Private key (d stands for decrypt)
# choosing d such that it satisfies
# d*e = 1 + k * totient
k = 2
d = ((k*phi)+1)/e #here calculate "d" and its a private key
print("d =", d)
print(f'Public key: {e, n}')
print(f'Private key: {d, n}')
msg = 5 # Message to be encrypted
print(f'Original message:{msg}')
# Encryption c = (msg ^ e) % n
C = pow(msg, e)
print("C =", C)
C = math.fmod(C, n)
print("C =", C)
print(f'Encrypted message: {C}')
# Decryption m = (c ^ d) % n
M = pow(C, d)
print("M =", M)
M = math.fmod(M, n)
print(f'Decrypted message: {M}')