0% found this document useful (0 votes)
166 views13 pages

Experiment 2 PDF

This document describes an experiment on implementing and analyzing the RSA cryptosystem and digital signature scheme using RSA or El Gamal. It includes the theory behind RSA encryption and digital signatures. Code samples are provided to generate keys for RSA, encrypt and decrypt messages, and generate keys and sign/verify messages for a digital signature scheme. Functions like gcd, mod inverse, and Euclid's algorithm are also included in the code samples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views13 pages

Experiment 2 PDF

This document describes an experiment on implementing and analyzing the RSA cryptosystem and digital signature scheme using RSA or El Gamal. It includes the theory behind RSA encryption and digital signatures. Code samples are provided to generate keys for RSA, encrypt and decrypt messages, and generate keys and sign/verify messages for a digital signature scheme. Functions like gcd, mod inverse, and Euclid's algorithm are also included in the code samples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Mahavir Education Trust's

SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE


Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science

EXPERIMENT 2
AIM : Implementation and analysis of RSA cryptosystem and Digital signature
scheme using RSA/El Gamal.
THEORY:
 RSA Encryption Algorithm

 RSA encryption algorithm is a type of public-key encryption algorithm. To


better understand RSA, lets first understand what is public-key encryption
algorithm.
 Public Key encryption algorithm is also called the Asymmetric algorithm.
 Asymmetric algorithms are those algorithms in which sender and receiver
use different keys for encryption and decryption.
 Each sender is assigned a pair of keys:

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.

 RSA algorithm uses the following procedure to generate public and


private keys:

o Select two large prime numbers, p and q.


o Multiply these numbers to find n = p x q, where n is called the modulus
for encryption and decryption.
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science

o Choose a number e less than n, such that n is relatively prime to (p - 1) x


(q -1). It means that e and (p - 1) x (q - 1) have no common factor except
1. Choose "e" such that 1<e < φ (n), e is prime to φ (n),
gcd (e,d(n)) =1
o If n = p x q, then the public key is <e, n>. A plaintext message m is
encrypted using public key <e, n>. To find ciphertext from the plain text
following formula is used to get ciphertext C.
C = me mod n
Here, m must be less than n. A larger message (>n) is treated as a
concatenation of messages, each of which is encrypted separately.
o To determine the private key, we use the following formula to calculate
the d such that:
De mod {(p - 1) x (q - 1)} = 1
Or
De mod φ (n) = 1
o The private key is <d, n>. A ciphertext message c is decrypted using
private key <d, n>. To calculate plain text m from the ciphertext c
following formula is used to get plain text m.
m = cd mod n

 Digital signature scheme:


 Digital signatures are the public-key primitives of message authentication.
 They are used to bind signatory to the message.
 A digital signature is a cryptographic output used to verify the
authenticity of data.
 A digital signature algorithm allows for two distinct operations:

A signing operation, which uses a signing key to produce a signature over


raw data.

A verification operation, where the signature can be validated by a party


who has no knowledge of the signing key.
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science

 Message authentication − When the verifier validates the digital


signature using public key of a sender, he is assured that signature
has been created only by sender who possess the corresponding
secret private key and no one else.
 Data Integrity − In case an attacker has access to the data and
modifies it, the digital signature verification at receiver end fails. The
hash of modified data and the output provided by the verification
algorithm will not match. Hence, receiver can safely deny the
message assuming that data integrity has been breached.
 Non-repudiation − Since it is assumed that only the signer has the
knowledge of the signature key, he can only create unique signature
on a given data. Thus the receiver can present data and the digital
signature to a third party as evidence if any dispute arises in the
future.

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)

while (e < phi):

# e must be co-prime to phi and


# smaller than phi.
if(gcd(e, phi) == 1):
break
else:
e = e+1

# choosing d such that it satisfies


# d*e = 1 + k * totient

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

print("Message data = ", msg)

# 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)

OUTPUT FOR RSA:

CODE FOR DS:


from math import sqrt

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

def generate_keypair(p, q, keysize):

nMin = 1 << (keysize - 1)


nMax = (1 << keysize) - 1
primes = [2]
start = 1 << (keysize // 2 - 1)
stop = 1 << (keysize // 2 + 1)

if start >= stop:


return []

for i in range(3, stop + 1, 2):


for p in primes:
if i % p == 0:
break
else:
primes.append(i)

while (primes and primes[0] < start):


del primes[0]

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

return ((e, n), (d, n))

def encrypt(msg_plaintext, package):


Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science

e, n = package
msg_ciphertext = [pow(ord(c), e, n) for c in msg_plaintext]
return msg_ciphertext

def decrypt(msg_ciphertext, package):


d, n = package
msg_plaintext = [chr(pow(c, d, n)) for c in 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

print("Encrypted msg: ")


print(''.join(map(lambda x: str(x), encrypted_msg)))
print("Decrypted msg: ")
print(decrypt(encrypted_msg, private))

# In[12]:

def euclid(m, n):

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

return (r1, t1)


p = 823
q = 953
n=p*q
Pn = (p-1)*(q-1)
key = []
for i in range(2, Pn):
gcd = euclid(Pn, i)
if gcd == 1:
key.append(i)
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science

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 ")

OUTPUT FOR DIGITAL SIGNATURE:


Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Artificial Intelligence and Data Science

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.

You might also like