0% found this document useful (0 votes)
18 views51 pages

Week 2

Uploaded by

ZAID MUGHAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views51 pages

Week 2

Uploaded by

ZAID MUGHAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 51

Information Security

Dr. Irfan Yousuf


Department of Computer Science (New Campus)
UET, Lahore
(Week 2: September 09 - 13, 2024 )
Outline
• Ceaser Cipher
• Modular Arithmetic
Cryptography
• Cryptography is a technique of securing information and
communications through use of codes so that only those for
whom the information is intended can understand it and
process it. The prefix “crypt” means “hidden” and suffix
“graphy” means “writing”.

• In computer science, cryptography refers to secure


information and communication techniques derived from
mathematical concepts and a set of rule-based calculations
called algorithms, to transform messages in ways that are
hard to decipher.
Cipher
• A secret or disguised way of writing; a code.
• In cryptography, a cipher is an algorithm for performing
encryption or decryption—a series of well-defined steps that
can be followed as a procedure.
Ceaser Cipher
• The Caesar cipher is the simplest and oldest method of
cryptography.

• The Caesar cipher method is based on a mono-alphabetic


cipher and is also called a shift cipher or additive cipher.

• 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.

• In modular arithmetic, numbers "wrap around" upon


reaching a given fixed quantity (this given quantity is
known as the modulus) to leave a remainder.
Modular Arithmetic

• Modular Arithmetic is one of the main tools provided by number


theory
• The quotient of n divided by m is n / m , where m and n are
positive integers
• The remainder of this division is called 'n mod m'
• So, the following holds:
n = m n / m + n mod m
where the first term is the quotient and the second the
remainder.
Modular Arithmetic

• Another way of putting this is:


• Given any positive integer n and any integer m, if we divide m
by n, we get an integer quotient, q, and integer remainder, r,
that obey the following relationship:
m = qn + r (0  r  n; q = m / n)
• The remainder, r, is often referred to as a residue of m modulo
n, and is the smallest non-negative integer that differs from m
by a multiple of n.
For example,
m = 11; n = 7; 11 = 1 7 + 4 r=4
m = −11; n = 7; − 11 = (−2)  7 + 3 r =3
Modular Arithmetic

• Two integers, a and b are said to be congruent (denoted by  )


if:
a  b (mod m)  a mod m = b mod m
that is, "a is congruent to b modulo m"
• Alternatively, in arithmetic modulo m, a and b are equivalent if
their difference, (a - b), is a multiple of m; that is, m | (a - b)
• The set of integers Zm = {0,1, ... m - 1} form the complete set of
residues modulo m -- there are only m different integers, mod m
• The operation a mod m denotes the residue of a, such that the
residue is some integer from 0 to m - 1. This operation is known
as a modular reduction.

10  2 (mod 4) because 4 | (10 − 2)


Modular Arithmetic

• Congruence is an equivalence relation -- that is, it satisfies:

1) The reflexive law : aa


2) The symmetric law : a  b  b  a
3) The transitive law : abcac

• Finding the smallest non-negative integer to which k is congruent


modulo n is called reducing k modulo n
Modular Arithmetic

• We can also add and subtract congruent elements without losing


congruence:
a  b and c  d  a + c  b + d (mod m)
 a − c  b − d (mod m)

• Multiplication also works:


a  b and c  d  ac  bd (mod m), for integers b, c
Modular Arithmetic

• Modular arithmetic is like ordinary arithmetic. It is:


• commutative (for addition and multiplication)
a+b=b+a
• associative
(a + b) + c = a + (b + c)
and
• distributive
a(b + c) = (ab) + (ac)
and
(b + c)a = (ba) + (ca)
Modular Arithmetic
• A very important property of modular arithmetic is:
• Reducing each intermediate result modulo m yields the same
result as doing the entire calculation, and then reducing the
result to modulo m:

(a + b) mod m = ((a mod m) + (b mod m)) mod m


(a − b) mod m = ((a mod m) − (b mod m)) mod m
(a  b) mod m = ((a mod m)  (b mod m)) mod m
(a  (b + c)) mod m = (((a  b) mod m) + ((a  c) mod m)) mod m

• This means that we can do modular arithmetic without


worrying about whether we will exceed some large arithmetic
bound -- so such calculations can be done on computers, even
for large integer values.
Modular Arithmetic

• Here are the possible values of (a + b) mod 8:

and (a·b) mod 8:


Modular Arithmetic: Exponentiation

• Recall that exponentiation is defined:


a 0 = e, the identity element
a n = a • a •  • a (i.e.• applied n-1 times)
a -n = (a' ) n , where a' is the inverse of a
• In ordinary arithmetic, exponentiation rapidly produces very
large numbers
• However, because of the important property of modular
arithmetic that intermediate results can be computed mod m,
then it is possible in mod m arithmetic to do powerful
exponentiation without producing very large numbers
• Remember, in cryptography, we'll be dealing with very large
values of m, so this is important.
Modular Arithmetic: Exponentiation

• For example, instead of performing the calculation:


a n mod m = (a • a • a • a •  • a)
we can instead perform fewer multiplications and use
intermediate modular reductions.
• Let's take a specific case of a 8 mod m. We can calculate it:
a mod m = ((a mod m) mod m) mod m
8 2 2 2

• Similarly:
a mod m = (((a 2 mod m) 2 mod m) 2 mod m) 2 mod m
16

a 25 mod m = (((((((a 2 mod m) • a) mod m) 2 mod m) 2 mod m) 2 mod m) • a) mod m

▪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.

• You should know that the computation of modular multiplicative


inverses is essential in cryptography, and in particular in the RSA
encryption method. This means that modular multiplicative
inverses protect your credit card!
Modular Arithmetic: Multiplicative Inverse
• Sometimes the modular multiplicative The mod 14 multiplication table.
inverse has a solution, and sometimes
it doesn't:
• The inverse of 5 mod 14, is 3
5*3 mod 14 = 1
• The inverse of 2 mod 14, doesn't
exist.
•Look at the row for 2, at right;
•It does not contain a value 1
• It turns out that a −1  x (mod m) has
a solution iff a and m are relatively
prime.
For example, look at the rows to the right. The only rows
that contain a 1 are for values that are relatively prime to
14: 1, 3, 5, 9, 11, 13
Modular Arithmetic: Multiplicative Inverse

• One way of finding the inverse of a modulo m is to use Extended


Euclidean Algorithm.

▪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.

• In addition to computing GCD, Extended Euclidean Algorithm also


finds two integers s and t such that

▪29
Extended Euclidean Algorithm
Extended Euclidean Algorithm

Find two integers s and t such that


1914s + 899t = gcd (1914, 899)
Extended Euclidean Algorithm
Iteration 2: (i=2)

q2 = 1914 / 899 = 2
r2 = 1914 % 899 = 116
s2 = 1-2*0 = 1
t2 = 0-2*1 = -2

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
Extended Euclidean Algorithm
Iteration 3: (i=3)

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

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
Extended Euclidean Algorithm
Iteration 5: (i=5)

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

1914 * 8 + 899*(-17) = gcd (1914, 899) = 29

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
Modular Arithmetic: Multiplicative Inverse

• Finding the inverse of a modulo b


• The Extended Euclidean Algorithm:
• While computing gcd(a, b), we can also find two integers s
and t such that:
gcd(a, b) = sa + bt
• If a and m are relatively prime, then the gcd(a, b) = 1, and:
1 = sa + bt = bt (mod b) (performing a reduction mod b)
and then, multiplying both sides by b-1:
b-1 = tb·b-1 = t
• So, if gcd(a, b) = 1, then t is the multiplicative inverse of a
mod b;
otherwise, there is no multiplicative inverse
▪38
Extended Euclidean Algorithm
Modular Arithmetic: Multiplicative Inverse

• Find the modular multiplicative inverse of 11 mod 26.


• To find multiplicative inverse of 11 mod 26, we should write
gcd(26,11) and a=26, b=11 (r0=26, r1=11)

▪40
Extended Euclidean Algorithm

t mod 26 ≡ (-7) mod 26 ≡ 19.

The multiplicative inverse of 11 modulo 26 is 19.

Verification:
19 × 11 (mod 26) ≡
209 (mod 26) ≡ 1 mod (26)

x is a multiplicative inverse of a modulo m if a * x and 1 are


congruent modulo m:
a * x = 1 mod m
Finite, or Galois Fields

• A field can be defined as a set of numbers that we can add,


subtract, multiply and divide together and only ever end up with
a result that exists in our set of numbers.
Modular Arithmetic
• Cryptography uses modular arithmetic a great deal, because:
• Calculating discrete logarithms and square roots mod n can be
hard problems.
• It's easier to work with on computers, because it restricts the
range of all intermediate values and results
• For a k-bit modulus n, the intermediate results of any
addition, subtraction, or multiplication will not exceed 2k
bits in length.
• We can perform modular exponentiation without
generating huge intermediate results.
Zn*
• Z is the set of all integers
• We've seen that Zn is the set of integers mod n
• Z10 = {0,1,2,3,4,5,6,7,8,9}

• Zn* is defined as the set of mod n integers that are relatively


prime to n
• Z10* = {1,3,7,9} (0 is missing because gcd(0, 10) = 10)

▪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

• Every element in Z10* is present in the table, and no other


elements other than those are present.
Furthermore, every element in Z10* is present in every row of
the table.
What does this mean for Z10*?
• It turns out that this is true for all n:
Zn* is closed under multiplication mod n
Fermat's Little Theorem
• *Fermat's Little Theorem states that for any prime number p and any
integer a, the following equation holds::

p −1
a  1 mod p

In simpler terms, Fermat's Little Theorem tells us that if we


raise any number a to the power of a prime number p and then
divide the result by p, the remainder will always be the same
as if we had divided a by p.
▪46
Fermat's Little Theorem

let's take a = 3 and p = 5. We can verify that the theorem holds


by computing:

which means that if we divide 35 by 5, the remainder is 3.


Similarly, if we divide 3 by 5, the remainder is also 3.

▪47
Euler's Totient Function

▪48
Euler's Totient Function
Euler's Totient Function

• Euler's Totient Functionφ( n) is the number of elements in Zn*


Summary
• Modular Arithmetic

You might also like