146RSAalgo PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

160117733-146

Experiment-6

Aim:-
To implement Encryption and Decryption using RSA algorithm.
Description:-
RSA algorithm is 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 Private key is kept private.
An example of asymmetric cryptography :
1.A client (for example browser) sends its public key to the server and requests for some data.
2.The server encrypts the data using client’s public key and sends the encrypted data.
3.Client receives this data and decrypts it.
Since this is asymmetric, nobody else except browser can decrypt the data even if a third party has
public key of browser.

Algorithm

Step 1 : Choose two prime numbers p and q.


Step 2 : Calculate n = p*q
Step 3 : Calculate ϕ(n) = (p – 1) * (q – 1)
Step 4 : Choose e such that gcd(e , ϕ(n) ) = 1
Step 5 : Calculate d such that e*d mod ϕ(n) = 1
Step 6 : Public Key {e,n} Private Key {d,n}
Step 7 : Cipher text C = Pe mod n where P = plaintext
Step 8 : For Decryption D = Dd mod n where D will give back the plaintext.

Code:-
from decimal import Decimal
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
p = int(input('Enter the value of p = '))
q = int(input('Enter the value of q = '))
no = int(input('Enter the value of text = '))
n = p*q
t = (p-1)*(q-1)

for e in range(2,t):
if gcd(e,t)== 1:
break
160117733-146
for i in range(1,10):
x = 1 + i*t
if x % e == 0:
d = int(x/e)
break
ctt = Decimal(0)
ctt =pow(no,e)
ct = ctt % n

dtt = Decimal(0)
dtt = pow(ct,d)
dt = dtt % n
print('n = '+str(n))
print(' e = '+str(e))
print(' t = '+str(t))
print(' d = '+str(d))

print(' cipher text = '+str(ct))


print(' decrypted text = '+str(dt))

Output:-

Conclusion:-
We have successfully implemented encryption and decryption using RSA algorithm.

You might also like