146RSAalgo PDF
146RSAalgo PDF
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
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))
Output:-
Conclusion:-
We have successfully implemented encryption and decryption using RSA algorithm.