GED 102 Module 3 Topic 2 Coding Theory and Cryptography

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 96

Mathematics in the Modern World

Module 3 – Topic 2
Coding Theory and
Cryptography
Introduction
The present state of technology is partly and largely attributed to the
use of codes. For example, the images and sounds that come out in
each television set are actually objects that have been digitally
structured for easier transmission in virtual channels. The same
principle applies to cellular communication just as everyone have
become dependent with mobile phones and similar gadgets.

Coding theory traces its origin in the design and implementation of


efficient and reliable means of storing and transmitting data. It is
usually associated to encryption and decryption in cryptography as
well as to error correction in communication and networks.
Introduction: Codes vs Ciphers
Ø CODE - utilizes a set of symbols to represent an object.
Ex: “A” can be replaced by a string of “0’s” and “1’s”, say 00101.
Codes are used in product identification and labeling.
Ø CIPHER - a scheme to conceal information by switching letters or
representing them with other symbols.
It is used heavily in communication and data transmission.
Objectives
q Introduce the concept of modular arithmetic which is essential in
most coding algorithms
q Discuss the binary system of numbers with emphasis on its
applications particularly on Hamming
q Use mathematical systems to discuss cryptography, particularly
some of the classical cryptosystems from which current
sophisticated methods are based.
Modular Arithmetic
Many real-world occurrences operate in an unusually cyclical process.
For example, because of the rotational orbit which the earth makes
around the sun which is supposed to take place in 24 hours, our clock
system cycles at 24-hour period. So, in a daily time basis, 15 hours
added with 18 hours does not count 33 hours in the clock system but
rather 9 hours.

In this case, our clock system is operating in a modulo 24 arithmetic


system.
Intended Learning Outcomes
At the end of the section, students should be able to:

1. explain modular arithmetic;


2. perform modular arithmetic; and
3. apply modular arithmetic in real-life situations.
Definition

Consider two integers 𝑥 and 𝑦 and a natural number 𝑛. Then 𝑥


and 𝑦 are congruent modulo 𝑛 (or 𝑥 is congruent to 𝑦 modulo 𝑛)
if the difference 𝑥 − 𝑦 is exactly divisible by 𝑛. The symbol “≅ ” is
used to denote congruence. The natural number 𝑛 is commonly
termed as the modulus of the congruence 𝑥 ≅ 𝑦.
Definition
!"#
In symbols, 𝑥 ≅ 𝑦 (mod 𝑛) whenever is an integer.
$

The modular value is actually the remainder when a given number is


divided by the modulus. Consequently, the modular value should be
less than the modulus. As a matter of convention, only natural
numbers are allowed when getting the modular value of a number
Example 1
Verify the following congruence:
a. 27 ≅ 2 mod 5
b. 19 ≅ 4 mod 7
Example 1
a. 27 ≅ 2 mod 5
%&"% %'
Note that 27 – 2 = 25, which is divisible by 5. Infact, '
= '
= 5.
Thus, the congruence is correct.

b. 19 ≅ 4 mod 7
()"* ('
Observe that &
= &
is not an integer. The congruence is not true.
Example 2: Evaluate (15 + 18) mod 9
Solution 1:
- First add 15 + 18 to get 33
++ ,
- get the modular value of 33: =3+
) )
So, the remainder is 6
Therefore, (15 + 18) mod 9 = 6
Equivalently, we can say (15 + 18) ≅ 6 (mod 9)
Example 2: Evaluate (15 + 18) mod 9
Solution 2:
1 ----> quotient
- get the modular values of the addends 9 ) 15
(' , 9
=1+ è 15 (mod 9) = 6 -------------
) )
(- . 6 ----> remainder
=2+ è 18 (mod 9) = 0
) )
- add the modular values
(15 + 18) mod 9 = (6 + 0) mod 9 = 6
Example 3: Evaluate (72 - 35) mod 11
Solution 1:
- First obtain the difference: 72 – 35 = 37
+& *
- get the modular value of 37: =3+
(( ((
So, the remainder is 4
Therefore, (72 - 35) mod 11 = 4
Equivalently, we can say (72 - 35) ≅ 4 (mod 11)
Example 3: Evaluate (72 - 35) mod 11
Solution 2:
- First get the modular values of 72 and 35
&% ,
=6+ è 72 (mod 11) = 6
(( ((
+' %
=3+ è 35 (mod 11) = 2
(( ((
- subtract the modular values : 6 – 2 =4
Therefore, (72 – 35) mod 11 = (6 – 2) mod 11 = 4 mod 11 = 4
Example 4: Evaluate (54)(27) mod 23
Solution 1:
- First multiply: (54)(27) = 1458
(*'- )
- get the modular value of 1458: = 63 +
%+ %+
So, the remainder is 9
Therefore, (54)(27) mod 23 = 9
Equivalently, we can say (54)(27) ≅ 9 (mod 23)
Example 4: Evaluate (54)(27) mod 23
Solution 2:
- First calculate the modular values of the factors
'* -
=2+ è 54 (mod 23) = 8
%+ %+
%& *
=1+ è 27 (mod 23) = 4
%+ %+
- multiply the modular values: (8)(4) = 32 > 23
+% )
- get the modular value of 32: =1+ è 32 (mod 23) = 9
%+ %+
Therefore, (54)(27) mod 23 = 32 (mod 23) = 9
Exercise: Evaluate the following

a. (47 + 26) mod 15


b. (25 – 32) mod 9
c. (13)(32) mod 14
Identity and Inverse elements in modular arithmetic

1. Like in ordinary addition and multiplication, 0 and 1 are the identity


elements for modular addition and modular multiplication,
respectively.
2. For positive integers 𝑎 and 𝑛, the integer 𝑏 is an additive inverse
under modulo 𝑛 if 𝑎 + 𝑏 ≅ 0 (mod 𝑛).
3. For positive integers 𝑐 and 𝑛, the integer 𝑑 is a multiplicative
inverse of 𝑐 under modulo 𝑛 if (𝑐)(𝑑) ≅ 1 (mod 𝑛).
Remarks:

1. Unlike in ordinary addition and multiplication, the inverse elements


for modular addition and multiplication are not unique. In fact, the
multiplicative inverse of 𝑐 under modulo 𝑛 may not even exist if 𝑐
and 𝑛 are not relatively prime (that is, gcd(𝑐, 𝑛) must be 1).
2. If 𝑏 is an additive inverse of 𝑎 mod 𝑛, then we can write:
−𝑎 ≅ 𝑏 (mod 𝑛)
Here, the symbol “−𝑎” is aptly read “additive inverse of 𝑎”, rather
(
than simply saying “negative 𝑎”. Similarly, the symbol / or 𝑐 "( should
mean “multiplicative inverse of 𝑐” and not simply “reciprocal of 𝑐”.
Example 5: What is the additive inverse of 9 mod 15?
Here, we need only to find a number 𝑏 such that
9 + b ≅ 0 (mod 15)
Clearly, 𝑏 = 6 satisfies this congruence, that is, 9 + 6 = 15≅ 0 (mod 15).
Thus, we can write −9 ≅ 6 (mod 15) or equivalently, −9 (mod 15) = 6.
Note that 𝑏 = 21 also satisfies the congruence, since
9 + 21 = 30 ≅ 0 (mod 15)
In general, the additive inverse of 9 are those numbers that are
congruent to 6 (mod 15) (e.g., 21, 36, 51, and so on).
Exercise:

What is the additive inverse of 15 mod 12?


Example 6: What is the multiplicative inverse of 3 mod 5?
By definition, the multiplicative inverse of 3 mod 5 is a number 𝑑 that
satisfies the congruence
(3)(d) ≅ 1 (mod 5)
The possible remainders under modulo 5 are: 0, 1, 2, 3, 4
By substitution, we can verify that 𝑑 = 2 satisfies the congruence.
Thus, we can now write
(
≅ 2 (mod 5) or 3"( ≅ 2 (mod 5)
+
Other than 𝑑 = 2, there are other possible inverses of 3: 7,12,17,….
Example 7: What is the multiplicative inverse of 4 mod 12?
By definition, the multiplicative inverse of 4 mod 12 is a number 𝑑
that satisfies the congruence
(4)(d) ≅ 1 (mod 12)
The possible remainders under modulo 12 are: 0, 1, 2, 3, …,11
Doing trial-and-error: (4)(0)=0, (4)(1)=4, (4)(2)=8, (4)(3)=12, (4)(4)=16,
(4)(5)=20, (4)(6)=24, (4)(7)=28, (4)(8)=32, (4)(9)=36, (4)(10)=40,
(4)(11)=44. None of the resulting product is congruent to 1 (mod 12).
In this case, the multiplicative inverse of 4 (mod 12) does not exist.
Exercise:
Determine the
a. Multiplicative inverse of 7 mod 13
b. Multiplicative inverse of 15 mod 6
Example 8
In a 12-hour clock, determine the time:
a. 5 hours after 11 o’clock
b. 65 hours after 8 o’clock
c. 15 hours before 5 o’clock
d. 25 hours before 10 o’clock
Example 8: Solution
The problem works in modulo 12 operation.
a. 5 + 11 = 16 ≅ 4 mod 12. So the answer is 4 o’clock.
b. 65 + 8 = 73 ≅ 1 mod 12. So the answer is 1 o’clock.
c. 5 – 15 = –10 ≅ 2 mod 12. The answer is 2 o’clock.
d. 10 – 25 = –15 ≅ 9 mod 12. The answer is 9 o’clock.
Example 9:
What day of the week is 25 days from now if today is a Monday?
Solution:
The answer is Friday. Apply modulo 7. Note that 25 ≅4 mod 7.
Counting 4 days after Monday (take note, Monday is not included in
the count), you will get Friday.
Example 9:
What day of the week is August 21 in 2019 if it fell on a Saturday in
1993?
Solution:
The answer is Wednesday. The year 1993 covered 132 days from August 22 to
December 31 while 2019 will cover 233 days from January 1 to August 21. The
indicated period covers six leap years (1996, 2000, 2004, 2008, 2012, 2016), each
year contributing 366 days. All remaining years will add 365 days each. All in all,
there are 9,496 days from August 22, 1993 to August 21, 2019. Now,
9,496 ≅ 4 (mod 7).
Counting 4 days after Saturday, you will get Wednesday.
Example 10:
Concon celebrated her 12th birthday on February 25, 2018, a Sunday.
What day of the week was she born?
Solution:
The answer is Saturday. Concon was born on February 25, 2006. Verify that there
are 4,383 days covered in the 12-year period. Since you moved backward, you
should consider the number of days as negative (to distinguish the forward time
being positive). Now,
–4,383 ≅ –1 (mod 7) ≅ 6 (mod 7)
(either move one day backward or 6 days forward).
Thus, February 25 fell on a Saturday in 2006.
Remark on Leap Years

Leap years are years that are divisible by 4


except those that are divisible by 100 but not
by 400. For example, 1900 was not a leap
year.
Application: International Standard Book
Number (ISBN)
The International Standard Book Number (ISBN) was introduced as a
scheme of regulating and keeping track of various publications in
different parts of the world. From a 10-digit code, it was expanded in
2007 to a 13-digit code. Each published book, including media
variations—electronic, paperback, hardcover—is assigned a distinctive
ISBN code.
In the Philippines, the National Library of the Philippines (NLP) takes
charge of the ISBN registration, allocation, and designation for Filipino
publishers and authors.
Application: ISBN Code
An ISBN code takes the form x1x2x3 – x4x5x6 – x7x8 – x9x10x11x12 – x13.
Currently, x1x2x3 = 978 (or 979). The fourth digit x4 is allocated for the
country code while the remaining digits (except the last) are used to
identify the author and the title of the book. The last digit, x13, is called
the check digit. This can be obtained using the formula
x13 = 10 – (x1 + 3x2 + x3 + 3x4 + x5 + 3x6 + x7 + 3x8 + x9 + 3x10 + x11 +
3x12) mod 10

If x13 = 10, then the check digit is 0.


Example 11:
Determine the check digit for the book “Larry Can’t Cook”:
978-971-27-2769-?
Solution:
x13 = 10 – [9+3(7)+8+3(9)+7+3(1)+2+3(7)+2+3(7)+6+3(9)] mod 10
= 10 – (154 mod 10)
= 10 – 4
=6
The check digit is 6.
Example 12:
Determine if the ISBN code 978-971-27-2770-4 is valid or not.
Solution:
x13 = 10 – [9+3(7)+8+3(9)+7+3(1)+2+3(7)+2+3(7)+7+3(0)] mod 10
= 10 – (128 mod 10)
= 10 – 8
=2≠4
The ISBN code is not valid.
Application: Universal Product Code (UPC)
The UPC (Universal Product Code) number is a 12-digit code that
usually accompanies the bar code of a product. The last digit (or the
12th digit) is the check digit. Like in the case of the ISBN, it can be
computed using the formula
X12 = 10 – (3x1+x2+3x3+x4+3x5+x6+3x7+x8+3x9+x10 +3x11) mod 10
If x12 = 10, then the check digit is 0.
Application: Credit Card
Credit card numbers can be easily checked through the “check digit.”
In the Philippines, Visa and Mastercard are the dominant credit cards
in the market, normally having 16-digit numbers except for some Visa
cards that have 13 digits only. Credit card numbers can be checked by
doubling every other digit starting from the second to the last digit
(the last digit being the check digit), and summing up all the resulting
numbers. If the doubled digit results in a 2-digit number, simply treat
the two digits as separate digits. The card number is valid only if the
sum of the digits under modulo 10 is congruent to 0.
Application: Credit Card
Example 13:
Determine which credit card number is valid and which is not.
a. 5234 8213 3410 1298
b. 6011 0123 9145 2317
Application: Credit Card
Solution:
a. 5234 8213 3410 1298

Sum = 1+0+2+6+4+1+6+2+2+3+6+4+2+0+2+2+1+8+8
= 60 ≅ 0 (mod 10)
The indicated credit card number is valid.
b. 6011 0123 9145 2317
Sum = 53 ≅ 3 (mod 10)
Not Valid
Binary system and Hamming Codes
Other than the decimal system which is the most common number system, there
are other number systems that serve specific purposes. There is the octal system
(which uses the digits 0,1,2,…,7) widely employed in computer science and
aviation, the hexadecimal system (which uses 16 symbols: 0 – 9 and A – F) used in
most programming languages.
Then there is the binary system which uses the digits 0 and 1 under modulo 2
arithmetic. Modern computers and similar devices generally function under the
binary system as it best captures their on-off states.
Learning Outcomes
At the end of the section, students should be able to:

1. perform addition and multiplication of binary numbers;


2. formulate and interpret codes using binary digits; and
3. perform error detection and correction using hamming
codes.
Definitions
A fundamental principle in basic mathematics affirms that
any integer 𝑁 can be written as a sum of multiples of distinct
powers of another number 𝑚. In symbols,
N = anmn + an–1mn–1 +…+ a2m2 + a1m1 + a0m0
= anmn + an–1mn–1 +…+ a2m2 + a1m + a0
where 𝑁 and 𝑚 are both positive integers and
ai ∈ {0, 1, 2,…, m – 1} for 0 ≤ i ≤ n (formally, m is called the
base or radix and ai are the digits). This is more commonly
known as the place-value notation (or positional notation).
Definitions
If m = 10, then we have the usual decimal system. In this
case, write
N = an10n + an–110n–1 +…+ a2102 + a110 + a0
where, in particular, ai ∈ {0, 1, 2,…, 9} for 0 ≤ i ≤ n.
Example 14
674 = (6)102 + (7)10 + 4
The place-values in this number are 102 = 100 or hundreds,
101 = 10 or tens, and 100 = 1 or units. Thus, the hundreds digit
is 6; the tens digit is 7; and the units digit is 4.
12,458 = (1)104 + (2)103 + (4)102 + (5)10 + 8
has ten-thousands digit 1, thousands digit 2, hundreds digit 4,
tens digit 5, and units digit 8.
Definitions
The binary system is a special system used particularly in computer
science and logic designs, which uses the digits 0 and 1, symbols that
may correspond to the on-off states of most devices.
Analogous to the place-value notation in the decimal system, a binary
number B is represented as
B = an2n + an–12n–1 +…+ a222 + a121 + a020
= an2n + an–12n–1 +…+ a222 + a12 + a0
In this case, each digit ai is only either 0 or 1.
Illustration
Consider the binary number B = 1010110. In the place-value notation,
we write

1010110 = 1(26) + 0(25) + 1(24) + 0(23) + 1(22) + 1(2) + 0

The place values this time are the powers of 2, such as units, twos,
fours, eights, and so on.
Illustration
Do you wonder what the binary number B = 1010110 is in the usual
decimal system?
Use the corresponding place values:
1010110 = 1(26) + 0(25) + 1(24) + 0(23) + 1(22) + 1(2) + 0
= 1(64) + 0(32) + 1(16) + 0(8) + 1(4) + 1(2) +0
= 64 + 16 + 4 + 2
= 86
In a more formal notation, write
(1010110)2 = (86)10 = 86
Illustration: Decimal to Binary
There is a simple way to convert a decimal number to its equivalent
binary form. This is done by successive division by 2. This is outlined in
the table below:

Reading backward the digits in the remainders, we obtain


86 = 10101102
Binary Sum
Since there are only two elements in the binary system, you can
easily define the sum of two elements. The table below
summarizes the addition operation:
Example 15
1 1
11000101
+ 01100110
-------------------
100101011
Binary Codes
A binary code refers to a string of 0’s and 1’s. Each character in the
string is called a bit (short for binary digit), while a series of eight bits
is called a byte. Each key in the keyboard of a computer corresponds
to a binary code of at least five bits (but mostly 1 byte). In particular,
computers are able to translate letters and symbols into binary codes
using the American Standard Code for Information Interchange
(ASCII codes).
Binary Codes

For purposes of
discussion, consider 5-
bit codes for the
familiar keys in a
standard computer
keyboard.
Example 16
Figure out the message in the following coded words:
01101000011010001000000000100110011000000011010 10101110
Example 16

First, segregate the string into blocks of 5-bit codes:


01101 00001 10100 01000 00000 01001 10011 00000

00110 10101 01110


Example 16

Refer to the table for the corresponding key:

01101 M 00001 A 10100 T 01000 H 00000 01001 I 10011 S 00000

00110 F 10101 U 01110 N


Example 17
Write the following sentence using the 5-bit binary codes:
Be on your guard
Solution:
(a) with spaces
00010001010000001111011100000011001011111010110010
(b) without spaces
0001000101011110111011001011111010110010
Exercise
Use the 5-bit codes to answer the following:
1. Translate the following into binary codes :
I AM NEVER LATE IN CLASS

2. Recover the message in the following string:

00111011110010000000000100110000101100111001100
0001010001000001010000010000010000100101100010
01100001000001001011100010110011
Hamming Codes
Coded messages tend to be very prone to distortion when being
transmitted to available media because of the presence of some
environmental interference or unforeseen defects in the transmission
medium. Any changes that may occur in a message that may alter its
original meaning is considered an error. In the context of binary codes,
an error occurs when there is an unintentional alteration of the bits,
the “0” getting altered as “1” or the other way around.
Hamming Codes
The Hamming Code, a scheme for error-correction, was introduced by
R. W. Hamming. This scheme uses a redundant bit which is called a
parity bit (also termed check bit) which is a 1-bit that is inserted in a
given string of binary codes.
A parity bit is either ‘even’ or ‘odd’ depending on whether there is an
even or odd number of 1’s.
Forming the Hamming Code
Step 1. Insert additional bit positions
If the length of the original code is r, then add m + 1 more bit positions,
where m is the largest power of 2 that satisfies the inequality: 2m < r. The positions
(𝑃! ) of the additional bits are at the corresponding powers of 2 : i=20 = 1, 21 = 2, 22
= 4,…, 2m.
Step 2. Extract subcodes for the parity codes of each additional bit.
For P1: copy the bits at positions 1,3,5,7,… (copy 1, skip 1)
For P2: copy the bits at positions 2,3,6,7,10,11,… (copy 2,skip 2)
In general, Pk : copy k bits (starting with the kth bit) then skip k bits
Step 3. Find Pi : The value of Pi is 0 if the resulting subcode in Step 2 is of even
parity, otherwise, the value is 1.
Example 18: Find the Hamming code of 10010
10010 has 5 bits (that is, it is of length 5) ====> 𝑟 = 5
Observe: 22 = 4< 5
𝑚 = 2, 𝑚 + 1 = 3
2 =8>5
3

===> Insert 3 additional bits, at positions 20, 21, and 22 (i.e. P1, P2, P4)
===> the Hamming code has 𝑚 + 1 + 𝑟 = 3 + 5 = 8 bits

Positions 1 2 3 4 5 6 7 8

Digit P1 = ? P2 = ? 1 P4 = ? 0 0 1 0

Observe that the first bit of the original code becomes the third bit of the Hamming
code, the second bit becomes the 5th bit, and all remaining bits Moving 3 bits forward.
Example 18: Find the Hamming code of 10010

Now form the subcodes:


Subcode for P1: P1P3P5P7 = ?101 ==> parity is even ==> P1 = 0
Subcode for P2: P2P3P6P7 = ?101 ==> parity is even ==> P2 = 0
Subcode for P4: P4P5P6P7 = ?001 ==> parity is odd ==> P4 = 1

Hamming Code: 00110010


Error Detection and Error Correction

Consider again the code 10010 in Example 18. Suppose the


corresponding Hamming code is transmitted, however in the
process the code was altered to 00110110 (instead of
00110010). How will the error be detected, and more
importantly, how will the error be corrected?
Example 19
Detect and correct the error in the transmitted code
00110110.
Example 19
At the receiving end, analyze the Hamming code and the following
observations should be evident:

The presence of at least one erroneous parity renders the received


code erroneous.
Example 19
To correct the code, add the positions of the parity where an error was
detected. In this case, they are P2 and P4. Adding, obtain 2 + 4 = 6. This
means that the bit in the 6th position has been altered. Since the
received bit is 1, it must be corrected to 0.

This recovers the original code 00110010.


Exercise

Find a Hamming code for each of the following binary codes.


a. 011011
b. 100110101
Exercise: Consider the binary code:
1100010101100
a. To form the Hamming code, what is the value of m and how many
additional bits are needed?
Answer: m = 3 (note: r = 13, which is between 23 = 8 and 24 = 16)
4 additional bits are needed
b. What are the positions of these additional bits?
Answer: 1st , 2nd , 4th , 8th
c. What are the values of these bits?
Answer: P1 = 1; P2 = 0; P4 = 0; P8 = 0
d. Give the Hamming code.
Answer: 10101000010101100
Exercise

Suppose the following Hamming codes were created using even


parity, determine if the code is correct or not. If not correct, find the
correct Hamming code.
a. 000011001010
b. 101110001100
c. 010101100011
Exercise
You and your parents are knowledgeable of Hamming codes. At one
instance that they went on a vacation, they instructed you to prepare
the house as they are arriving with some friends. You asked what time
they will arrive and you received this answer:

111101000001010111101000101101
What is the message? Do you think there is an error? If there is, can
you correct it and get the real message?
Exercise: 111101000001010111101000101101
• Mark all bit positions that are powers of 2
111101000001010111101000101101
• original message
1010000101011101000101101
10100 00101 01110 10001 01101
T E N Q M
Exercise: 111101000001010111101000101101
P1 subcode: 110000001110110 ==> error!
P2 subcode: 111000101100011 ==> correct
P4 subcode: 101010100100101 ==> error!
P8 subcode: 000010100101101 ==> correct
P16 subcode: 111101000101101 ==> error!

Add the positions of the erroneous bits: 1 + 4 + 16 = 21


Change the bit in position 21 from “1” to “0”.
Exercise: 111101000001010111101000101101
Corrected Hamming code: 111101000001010111100000101101
Remove parity checks: 111101000001010111100000101101
1010000101011100000101101
10100 00101 01110 00001 01101
T E N A M
Cryptography
Communication is a process by which information is shared among individuals. The
manner of disseminating messages has evolved along with the accelerated growth
of technology. This technological growth, however, has posed bigger challenge in
terms of maintaining the integrity of information being shared.

With the advent of wireless communication and the widespread reliance on the
internet, information is at a high risk of being breached. Mathematics has
developed a system by which this problem can be addressed—through
cryptography.
Learning Outcomes
At the end of the section, students should be able to:
1. explain the basic principles of cryptography;
2. describe the different methods of data encryption and
decryption; and
3. demonstrate and apply the different processes of
cryptography.
Definitions
Cryptography is the process of converting data or information into
codes, primarily for the purpose of keeping it secured (confidential
and undistorted). It involves encryption and decryption. Encryption
basically converts a message into meaningless and usually unreadable
formats that only the intended recipient can understand. Decryption,
on the other hand, is the process of recovering the encrypted
information.
Shift Cipher

Use the natural order of the letters of the English alphabet:

The goal is to replace the letters of a given message by


another letter which is at some fixed number of positions
according to the natural order of the letters.
Shift Cipher

Formula: 𝐶 = 𝑃 + 𝐾 𝑚𝑜𝑑 26
𝑃 is the original position of a letter in the given message, 𝐶 is
the shifted position (code letter), 𝐾 is a constant that
determines the fixed number of shift positions.
Example 1: Encryption

Encrypt the message “Let us drink coffee” using the shift


cipher : 𝐶 = (𝑃 + 9) mod 26
For the letter “L”, P=11 ==> C = (11+9) mod 26 = 20
==> the code letter for “L” is “U”
For the letter “T”, P=19 ==> C = (19+9) mod 26 = 2
==> code letter for “T” is “C”
Example 1

Verify the following:


Original letter L E T U S D R I N K C O F F E E

Original position 11 4 19 20 18 3 17 8 13 10 2 14 5 5 4 4

Shifted position 20 13 2 3 1 12 0 17 22 19 11 23 14 14 13 13

Code letter U N C D B M A R W T L X O O N N

The Encrypted message is : UNCDBMARWTLXOONN


Decryption
How can the receiver of an encrypted message recover the original
message?
This is where the concept of inverse element comes in.
Encryption Cipher: C = (P + K) mod 26
Decryption Cipher: P = (C – K) mod 26

The recovery process of the encrypted message is now supposed to be


backwards, hence the –K in the formula.
Example 2: Decryption

Using the encryption formula: C = (P + 9) mod 26


Decrypt the message: RFJWCCXFJCLQVXERN
Example 2: Decryption

The decryption formula: P = (C - 9) mod 26


For the letter “R”, C=17 ==> P = (17-9)mod 26 = 8
==> the corresponding original letter is “I”
For the letter “C”, C = 2 ==> P=(2-9) mod 26 = -7 (mod 26)
Recall that -7 means the additive inverse of 7 under modulo
26 (that is, the number to be added to 7 to get 0 mod 26).
Clearly, this must be 19. So, -7 mod 26 = 19, and this
corresponds to the letter “T”.
Example 2: Decryption

Verify the following:


Encrypted R F J W C C X F J C L Q V X E R N

Position(C) 17 5 9 22 2 2 23 5 9 2 11 16 21 23 4 17 13
Original position
8 22 0 13 19 19 14 22 0 19 2 7 12 12 21 8 4
(P)
Original letter I W A N T T O W A T C H M O V I E

The original message is : I WANT TO WATCH MOVIE


Remark
In the decryption formula of Example 2 given by
P = (C - 9) mod 26
recall that -9 also denotes the additive inverse of 9 under modulo 26, and -9 mod
26 = 17. So an equivalent formula for the decryption should be
P = (C + 17) mod 26
To verify this, recall that “R” was decrypted as “I” using P=(C-9) mod 26. If P = (C +
17) mod 26 is applied, with C=17
P = (17+17) mod 26 = 34 mod 26 = 8
And the corresponding letter is also “I”.
Affine Cipher
A more complicated cipher formula involves multiplying the cipher
value of each letter of the original message. In a way, this will be more
difficult to crack than the shift cipher technique.
Encryption Cipher (m, K): C = (mP + K) mod 26
(
Decryption Cipher (m, K): P = (C – K) mod 26
0
(
As a reminder, denotes the multiplicative inverse of m with respect
0
to modulo 26 operation.
Note also that the pair (m,K) is a valid encryption cipher parameters if
m and 26 are relatively prime.
Example 3: Encryption with Affine Cipher

Encrypt the message “Let us drink coffee” using the affine


cipher pair (m,K) = (3,5).
Example 3: Encryption with Affine Cipher

Encrypt the message “Let us drink coffee” using the affine


cipher pair (m,K) = (3,5).
Encryption Formula: C = (3P + 5) mod 26
For letter “L”, P=11 ==> C=[(3)(11)+5] mod 26 = 38 mod 26 = 12
==> the code letter for “L” is “M”
Example 3: Encryption with Affine Cipher

Verify the following:


Original letter L E T U S D R I N K C O F F E E

Original position 11 4 19 20 18 3 17 8 13 10 2 14 5 5 4 4

New position 12 17 10 13 7 14 4 3 18 9 11 21 20 20 17 17

Code letter M R K N H O E D S J L V U U R R

The Encrypted message is : MRKNHOEDSJLVUURR


Example 4: Decryption

Determine a decryption formula for the pair (m,K)=(3,5)


Decrypt the message: DTFSKKVTFKLAPVQDR
Example 4(a): Decryption for (m,K) = (3,5)
!
Decryption formula: 𝑃 = (𝐶 − 𝐾) mod 26
"
! !
= ≅ 9 mod 26, since (3)(9) mod 26 = 27 mod 26 = 1
" #
-K = -5 ≅ 21 mod 26, since 5 + 21 = 26 ≅ 0 mod 26.
!
Again, recall that denotes the multiplicative inverse of 3
#
mod 26, while -5 denotes the additive inverse of 5 mod 26.
Decryption formula: P = 9(C + 21) mod 26 or
P = 9(C – 5) mod 26
Example 4(b): Decrypt “DTFSKKVTFKLAPVQDR”

Let us use P = 9(C + 21) mod 26


For letter “D”, C=3 ==> P=9(3+21) mod 26 = 216 mod 26 = 8
==> P = 8 corresponds to the letter “I”
For letter “T”, C=19 ==> P=9(19+21) mod 26 = 360 mod 26 = 22
==> P = 22 corresponds to the letter “W”
Example 4(b): Decrypt “DTFSKKVTFKLAPVQDR”

Let us use P = 9(C + 21) mod 26


Verify the following results:
Encrypted D T F S K K V T F K L A P V Q D R

Position(C) 3 19 5 18 10 10 21 19 5 10 11 0 15 21 16 3 17
Original position
8 22 0 13 19 19 14 22 0 19 2 7 12 14 21 8 4
(P)
Original letter I W A N T T O W A T C H M O V I E

Verify also the results using the decryption P = 9(C – 5) mod 26


Exercise
Apply Shift Cipher to encrypt the message “Kill The Beast” using K = 18.
Exercise

Apply Shift Cipher to decrypt the message “TMPBXHTPHN”


using K = 15.
Exercise
Apply Affine Cipher using (m, K) = (5, 21) to encrypt the message:
“Go home immediately”
Exercise
Apply Affine Cipher using (m, K) = (5, 21) to decrypt the message:
“OVXXVIENLQLFH”

You might also like