You are on page 1of 8

ASSIGNMENT

NAME: NJENGA MELISSA JOY NJERI

REG NO: SCT211-0020/2018

UNIT CODE: ICS 2411

UNIT: CRYPTOGRAPHY

COURSE: BSC. COMPUTER SCIENCE

YEAR: 4.2
Question 1: Prove the following

a) If a|b then a|bc ∀a, b, c ∈ ℤ:


Since a|b then there exists d ∈ ℤ such that b = ad
Therefore, to show that a|bc, bc = (ad) c
By associativity, bc = (ad) c = a (dc)
By closure property of multiplication, dc = m ∈ ℤ
Therefore, bc = am; proving that a|bc

b) If a|b and b|c, then a|c:


Since a|b and b|c then b is a multiple of a and c is a multiple of b
There are integers m and n such that b = ma and c = nb
Through substitution; c = n(ma) = nma = (nm) a
Since nm is an integer and c = (nm) a, c is a multiple of a
Therefore, proving that a|c

Question 2: Using Python, implement the following algorithms

a) Modular exponentiation algorithm


To compute 39, we have 9(1001)2 so that 39 = (38 x 31)
Squaring it then, (32 = 9), (34 = 92 = 81) and (38 = 812=6561)
Resulting to (39 = 38 x 31) = (6561 x 3) = 19683

Python Code
j=3
k=9
i = (int)(1e9+7)
result = pow(j, k) % i
print (result)
b) The sieve of Eratosthenes
It is used to find all primes not exceeding a specified positive integer
The following python code is used to find the primes not exceeding 100

Python Code
def Sieve_Of_Eratosthenes (number):
prime = [True for i in range(number+1)]
p=2
while (p * p <= number):
if (prime[p] == True):
for i in range (p * p, number+1, p):
prime[i] = False
p += 1
for p in range (2, number+1):
if prime[p]:
print(p)

if __name__ == '__main__':
number = 100
print ("The following are the prime numbers not exceeding", number),
Sieve_Of_Eratosthenes (number)
Question 3: Let ‘m’ be the gcd of 117 and 299, find ‘m’ using the Euclidean algorithm

Let a = b q + r, where a, b, q and r, are integers. Then gcd (a, b) = gcd (b, r)

a = 299

b = 117

Therefore;

299 = (117 x 2) + 65

117 = (65 x 1) + 52

65 = (52 x 1) + 13

52 = (13 x 4) + 0

Therefore m = 13

Question 4: A python program that implements the Euclidean Algorithm

Python Code

from math import *

def Euclidean Algorithm (a, b, verbose=True):

if a < b:

return Euclidean Algorithm (b, a, verbose)

print ()

while b! = 0:

if verbose: print ('%s = %s * %s + %s' % (a, b, floor(a/b), a % b))

(a, b) = (b, a % b)

if verbose: print ('GCD = %s' % a)

return a
The python code above has been tested using ‘question 3’

Question 5: Modification of the Euclidean Algorithm above

The new code should return the gcd of a and b and the Bezouts coefficients x and y, such that
𝑎𝑥 + 𝑏𝑦 = 1

Python Code

def Modified Euclidean (a, b):

if a == 0:

return b,0,1

GCD, a1, b1 = Modified Euclidean (b % a, a)

x = b1 - (b//a) * a1

y = a1

return GCD, x, y
Question 6: Find the integers p and q, solution to 1002𝑝 + 71𝑞 = m

m = GCD (1002, 71) = 1002𝑝 + 71𝑞

Using Euclidean Algorithm;

1002 = (71 x 14) + 8

71 = (8 x 8) + 7

8 = (7 x 1) + 1

7 = (1 x 7) + 0

GCD = 1

To get the coefficients p and q;

1=8–7

= 8 - (71 – 8 x 8)

= 8 – 71 + 8 x 8

= 72 -71

= (9 x 8) – 71

= 9(1002 – 71 x 14) -71

= 9 x 1002 – 8946 -71

= 9 x 1002 – 71 (126 + 1)

= 9 x 1002 – 71 x 127

= (9 x 1002) – (127 x 71)

Therefore; p = 9, q = -127
Question 7: Determine whether the equation 486x + 222y = 6 has a solution

GCD (486, 222) = 6 = 486x + 222y

Using Euclidean Algorithm:

486 = (222 x 2) + 42

222 = (42 x 5) + 12

42 = (12 x 3) + 6

12 = (6 x 2) + 0

GCD = 6 therefore, the equation has a solution

To get x and y:

6 = 42 – 12 x 3

= 42 – 3 (222 – 42 x 5)

= 42 – 3 x 222 + 3(42 x 5)

= 42 – 3 x 222 + 630

= -3 x 222 + 42(1 + 15)

= -3 x 222 + 16 x 42

= -3 x 222 + 16(486 – 222 x 2)

= -3 x 222 + 16 x 486 – 7104

= 16 x 486 – 666 – 7104

= 16 x 486 – 222(3 + 32)

= (16 x 486) – (35 x222)

Therefore, x = 16, y = -35


Question 8: Determine integers x and y

gcd (421, 11) = 421x + 11y

Using Euclidean Algorithm:

421 = (11 x 38) + 3

11 = (3 x 3) + 2

3 = (2 x 1) + 1

2 = (1 x 2) + 0

GCD = 1

To get x and y:

1=3–2

= 3 – (11 – 3 x 3)

= 3 – 11 + 3 x 3

= 3 – 11 + 3 (421 – 11 x 38)

= 3 – 11 + 1263 – 1254

= (421 – 11 x 38) – 11 + 1263 – 1254

= 421 – 418 – 11 + 1263 – 1254

= 421 + 1263 – 11 – 418 -1254

= 421(1 + 3) – 11 (1 + 38 + 114)

= (4 x 421) – (153 x 11)

Therefore; x = 4, y = -153

You might also like