Week 2
Week 2
• Julius Caesar (58 B.C.) used the shift cipher (additive cipher)
technique to communicate with his officers.
Ceaser Cipher
Ceaser Cipher
Shift (Key) = 3
Ceaser Cipher
https://computerscienced.co.uk/site/caesar-cipher-wheel/caesar-cipher/
Ceaser Cipher
Ceaser Cipher
Exercise
• Write a program to encrypt and decrypt messages using
Ceaser Cipher.
Ceaser Cipher: History
Modular Arithmetic
• Modular arithmetic is a system of arithmetic for integers,
which considers the remainder.
• Similarly:
a mod m = (((a 2 mod m) 2 mod m) 2 mod m) 2 mod m
16
▪23
Modular Arithmetic: Division
• So far, for mod m arithmetic, we have addition, subtraction and
multiplication.
• What about division?
• Division is defined through a multiplicative inverse.
• In regular arithmetic:
• The multiplicative inverse of 5 is 1/5, because 5·1/5 = 1
• In modular arithmetic, things are not so easy:
Find x, where 5 x 1 (mod 7)
which is equivalent to finding an x and a k (both integers) such
that:
5x = 7k + 1
• The general problem is to find x such that:
1 = (a x) mod m
or :
a −1 x (mod m)
Modular Arithmetic: Multiplicative Inverse
• Identity element of multiplication is 1.
• Hence, x is a multiplicative inverse of a modulo m if a * x and 1
are congruent modulo m:
• a * x ≡ 1 mod m
• The multiplicative modular inverse does not always exist!
• The multiplicative modular inverse of a modulo m exists if and
only if a and m are relatively prime, i.e., gcd (a, m) = 1.
▪27
Modular Arithmetic: Multiplicative Inverse
• One way of finding the inverse of a modulo m is to extend
Euclid's greatest common divisor algorithm.
▪28
Extended Euclidean Algorithm
• An extension of the Euclidean Algorithm that computes the
greatest common divisor (GCD) of integers a and b.
▪29
Extended Euclidean Algorithm
Extended Euclidean Algorithm
q2 = 1914 / 899 = 2
r2 = 1914 % 899 = 116
s2 = 1-2*0 = 1
t2 = 0-2*1 = -2
q3 = 899 / 116 = 7
r3 = 899 % 116 = 87
s3 = 0-7*1 = -7
t3 = 1-7*(-2) = 15
gcd (1914, 899) a=1914, b=899
i ri-2 ri-1 si-2 si-1 ti-2 ti-1
2 1914 899 1 0 0 1
3 899 116 0 1 1 -2
Extended Euclidean Algorithm
Iteration 4: (i=4)
q4 = 116 / 87 = 1
r4 = 116 % 87 = 29
s4 = 1-1*(-7) = 8
t4 = -2-1*15 = -17
q5 = 87 / 29 = 3
r5 = 87 % 29 = 0
s5 = -7-3*(8) = -31
t5 = 15-3*(-17) = 66
gcd (1914, 899) a=1914, b=899
i ri-2 ri-1 si-2 si-1 ti-2 ti-1
2 1914 899 1 0 0 1
3 899 116 0 1 1 -2
4 116 87 1 -7 -2 15
5 87 29 -7 8 15 -17
Extended Euclidean Algorithm
Iteration 6: (i=6)
q6 = 29 / 0 = 3
r6 = 87 % 29 = 0
s6 = -7-3*(8) = -31
t6 = 15-3*(-17) = 66
gcd (1914, 899) a=1914, b=899
i ri-2 ri-1 si-2 si-1 ti-2 ti-1
2 1914 899 1 0 0 1
3 899 116 0 1 1 -2
4 116 87 1 -7 -2 15
5 87 29 -7 8 15 -17
6 29 0 8 -31 -17 66
Extended Euclidean Algorithm
S = 8, t = -17
▪40
Extended Euclidean Algorithm
Verification:
19 × 11 (mod 26) ≡
209 (mod 26) ≡ 1 mod (26)
▪44
Zn*
• The multiplication table for Z10* provides some surprises:
• Notice anything?
1 3 7 9
1 1 3 7 9
3 3 9 1 7
7 7 1 9 3
9 9 7 3 1
p −1
a 1 mod p
▪47
Euler's Totient Function
▪48
Euler's Totient Function
Euler's Totient Function