Experiment 2 PDF
Experiment 2 PDF
EXPERIMENT 2
AIM : Implementation and analysis of RSA cryptosystem and Digital signature
scheme using RSA/El Gamal.
THEORY:
RSA Encryption Algorithm
o Public key
o Private key
The Public key is used for encryption, and the Private Key is used for
decryption.
Decryption cannot be done using a public key.
The two keys are linked, but the private key cannot be derived from the
public key.
CODE:
import math
def gcd(a, h):
temp = 0
while(1):
temp = a % h
if (temp == 0):
return h
a=h
h = temp
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
p=3
q=7
n = p*q
e=2
phi = (p-1)*(q-1)
k=2
d = (1 + (k*phi))/e
# Message to be encrypted
msg = 12.0
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
# Encryption c = (msg ^ e) % n
c = pow(msg, e)
c = math.fmod(c, n)
print("Encrypted data = ", c)
# Decryption m = (c ^ d) % n
m = pow(c, d)
m = math.fmod(m, n)
print("Original Message Sent = ", m)
import random
from random import randint as rand
def gcd(a, b):
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
if b == 0:
return a
else:
return gcd(b, a % b)
def mod_inverse(a, m):
for x in range(1, m):
if (a * x) % m == 1:
return x
return -1
def isprime(n):
if n < 2:
return False
elif n == 2:
return True
else:
for i in range(2, int(sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
p = rand(1, 1000)
q = rand(1, 1000)
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
while primes:
p = random.choice(primes)
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
primes.remove(p)
q_values = [q for q in primes if nMin <= p * q <= nMax]
if q_values:
q = random.choice(q_values)
break
print(p, q)
n=p*q
phi = (p - 1) * (q - 1)
e = random.randrange(1, phi)
g = gcd(e, phi)
while True:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = mod_inverse(e, phi)
if g == 1 and e != d:
break
e, n = package
msg_ciphertext = [pow(ord(c), e, n) for c in msg_plaintext]
return msg_ciphertext
return (''.join(msg_plaintext))
#-------------------------------------------------------------
#driver program
if __name__ == "__main__":
bit_length = int(input("Enter bit_length: "))
print("Running RSA...")
print("Generating public/private keypair...")
public, private = generate_keypair(
p, q, 2**bit_length) # 8 is the keysize (bit-length) value.
print("Public Key: ", public)
print("Private Key: ", private)
msg = input("Write msg: ")
print([ord(c) for c in msg])
encrypted_msg = encrypt(msg, public)
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
# In[12]:
if n == 0:
return m
else:
r=m%n
return euclid(n, r)
def exteuclid(a, b):
r1 = a
r2 = b
s1 = int(1)
s2 = int(0)
t1 = int(0)
t2 = int(1)
while r2 > 0:
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science
q = r1//r2
r = r1-q * r2
r1 = r2
r2 = r
s = s1-q * s2
s1 = s2
s2 = s
t = t1-q * t2
t1 = t2
t2 = t
if t1 < 0:
t1 = t1 % a
e = int(313)
r, d = exteuclid(Pn, e)
if r == 1:
d = int(d)
print("decryption key is: ", d)
else:
print("Multiplicative inverse for the given encryption key does not exist.
Choose a different encryption key ")
M = 19070
S = (M**d) % n
M1 = (S**e) % n
if M == M1:
print("As M = M1, Accept the message sent by Alice")
else:
print("As M not equal to M1, Do not accept the message sent by Alice ")
Conclusion:
Digital signatures serve the purpose of authentication and verification of
documents and files. RSA Algorithm is a type of public key encryption
algorithm. It is asymmetric algorithm. We have implemented and analysed RSA
cryptosystem and digital signature scheme using RSA/EI Gamal.