You are on page 1of 2

Assignment 10

Given:

-p (prime) = 43

-q (prime)= 47

-e (part of the public key)= 155

To nd - d(part of the private key);

The relation between d and e is

d*e = 1 mod Eulersphi(n) ;

For doing this we use the function Modinv to nd the mod inverse of ‘e’, which is
basically equal to ‘d’.

And we obtain the value of ‘d’ as 1583, from this calculation.

Alice sends the message -

m = “2101-CON101 INTRODUCTION TO COMP.SC. amp; ENG” to bob,

here in the code we use b string as we want the literals representing the integers to be
between 0 and 255, along with the digital signature and bob reviews whether h is equal to
h(m) produced or not and prints out the validity of the signature if needed. Here we
named the values h(m),h as hashm and hash and checked the validity(correct or
unaltered), and to print the digest of the message we used Messagesent.digest()
command and printed out the digest of the hash value h(m).

1.
e = 155

p = 43

q = 47

n = p*q

def Eulersphi(t):

return (p-1)*(q-1)

print("Eulersphi function is:", Eulersphi(n))

key1 = n,e

print("Public key (n,e)= ",key1)

# Modinv returns e −1 mod Eulersphi(n)

def Modinv(k,h):

for i in range(h):

if (k*i)%h==1:

return i

d = Modinv(e,Eulersphi(n))

key2 = n,d

print("Private key (n,d)= ",key2)

2.

from Crypto.PublicKey import RSA

M = RSA.generate(bits = 1024)

n = (M.n)

d = (M.d)

e = (M.e)

import hashlib

fi
fi
Messagesent = hashlib.sha512()

Messagesent.update(b"2101-CON101 INTRODUCTION TO COMP.SC. amp; ENG")

message= b'2101-CON101 INTRODUCTION TO COMP.SC. amp; ENG'

print("The digest value of h(m) is:")

Messagesent.digest()

from hashlib import sha512

hash = int.from_bytes(sha512(message).digest(), byteorder='big')

signature = pow(hash, M.d, M.n)

print("Message signature s is :", (signature))

hashm = pow(signature,M.e, M.n)

print("The Encryption h generated by Bob is : ",hash)

print("Hash generated by Alice is: ",hashm)

print("Validity of the signature :", hash == hashm)

Out 1.

Eulersphi function is: 1932

Public key (n,e)= (2021, 155)

Private key (n,d)= (2021, 1583)

Out 2.

The digest value of h(m) generated is:

Message signature s is :
597104975514875451599482553370672525879060980711796678717221629108740509
740709647249634642216017969309237282199028025871307570832163659265399640
077347954520853487101328830671086309758490193109614501622940403069236156
617444103422871456281508789625755477040134827735468832252491466550789326
94478354885881394301

The Encryption h generated by Bob is :


916267001182484509056494170295419515799035478731734122579215057408741309
552732598512034497443394813195318637309844627572383204291407772341463035
9083284023

Hash generated by Alice is:


916267001182484509056494170295419515799035478731734122579215057408741309
552732598512034497443394813195318637309844627572383204291407772341463035
9083284023

Validity of the signature : True

You might also like