Lec 10 - Cryptography
Lec 10 - Cryptography
Cryptography
Cryptography
Cryptography
Terminologies of Cryptography
Cryptography Process
Caesar Cipher
ROT13 Cipher
Atbash Cipher
Vigenère Cipher
Transposition Cipher
Columnar Transposition Cipher
Playfair Cipher
1
Cryptography
Cryptography is the study of secure communications
techniques that allow only the sender and intended recipient
of a message to view its contents.
[Sender] [Recipient]
Cryptography
Cryptography’s Famous Family: Alice and Bob
Picture source: Shannon W. Bray (2020). Implementing Cryptography Using Python. Wiley.
2
Terminologies of Cryptography
Plaintext – an unencrypted, readable, plain message that anyone can
read.
Ciphertext – the encrypted text after applying cryptography on plain
text.
Encryption – process of encoding a message or information in such a
way that only authorized parties can access it.
Decryption – process of taking encoded / encrypted text and
converting it back into readable text.
Cipher – a mathematical function used for encryption and decryption.
Key – It is required by the encryption that tells the algorithm how to
transform the plaintext into ciphertext.
Cryptography Process
https://www.golangprograms.com/what-is-cryptography.html
3
Caesar Cipher
Caesar cipher is the oldest recorded ciphers.
It is a substitution cipher in which each letter in the
plaintext is 'shifted' a certain number of places down the
alphabet.
The Caesar cipher shifting letters by
three spaces. Here, B becomes E.
Caesar Cipher
Example:
x = current letter of alphabet, k = key (number of shift)
Enc(x) = (x + k) % 26 when k=3, x=A (A -> D)
Dec(x) = (x – k) % 26 when k=3, x=D (A <- D)
The encryption formula adds 3 to the numeric value of the letter.
If the value exceeds 26, which is the final position of Z, then it
wraps the value back to the beginning of the alphabet
The encryption and decryption methods are fixed.
This would allow anyone who knew this method to read
Caesar’s encrypted messages with ease.
Animation: http://inventwithpython.com/cipherwheel/
4
Caesar Cipher
Plaintext: Zebra k=3 Enc(x) = (x + k) % 26
90 67
Add 65
Take
away 65
25 2
+k 28 Modulo 26
Caesar Cipher
Plaintext: Zebra k=3 Enc(x) = (x + k) % 26
101 104
Add 97
Take
away 97
4 7
+k 7 Modulo 26
5
Caesar Cipher – source code
def CaesarEnc(text,s): Output:
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
else:
result += chr((ord(char) + s-97) % 26 + 97)
return result
ROT13 Cipher
ROT13 cipher refers to the abbreviated form Rotate by
13 places.
It is a special case of Caesar Cipher in which shift is always 13.
Every letter is shifted by 13 places to encrypt or decrypt the
message.
The unique construction of the ROT-13 cipher allows the
same method is used to encrypt and decrypt.
The ROT13 cipher offers almost no security, and can be
broken very easily.
6
ROT13 Cipher
ROT13 Cipher
https://blog.finxter.com/how-to-use-rot13-in-python/
7
Atbash Cipher
Atbash Cipher is a mono alphabetic substitution cipher.
Each letter of the clear text is replaced by a corresponding letter
of the cipher alphabet.
It reverses the plaintext alphabet to create the ciphertext
alphabet.
It maps the alphabet to itself in the opposite direction, so A to Z,
B to Y, C to X, etc.
It is a weak encryption method.
Atbash Cipher
Plaintext: Zebra Enc(x) = 25 - x
Convert to Convert to
number character
90 65
Take
away 65 25 0 Add 65
25 - x
8
Atbash Cipher
Plaintext: Zebra Enc(x) = 25 - x
Convert to Convert to
number character
101 118
Take
away 97 4 21 Add 97
25 - x
Vigenère Cipher
Vigenère Cipher is a substitution cipher that uses several
Caesar ciphers in sequence with different shift values.
The encryption of the plaintext is done using the Vigenère
square or Vigenère table.
The table consists of the alphabets written out 26 times in
different rows, each alphabet shifted cyclically to the left
compared to the previous alphabet, corresponding to the
26 possible Caesar Ciphers.
9
Vigenère Table
plaintext M U L T I M E D I A
10
Vigenère Cipher – using the table
Vigenère Table is given:
Step 2: to encrypt a text
Locate the first letter of the plaintext in the row of the
table
Locate the first letter of the key in the column of the table
Repeatedly find the intersection of the row and column to
get the ciphertext.
Example:
• Key: LOVE
• Plaintext: MULTIMEDIA
• Key: LOVELOVELO
plaintext
• Plaintext: MULTIMEDIA
• Ciphertext: X
11
Vigenère Table – Example explain
Example:
• Key: LOVE
• Plaintext: MULTIMEDIA
• Key: LOVELOVELO
• Plaintext: MULTIMEDIA
• Ciphertext: I
plaintext
The process is repeated for
next alphabet until the end of
the plaintext.
Ciphertext: XIGXTAZHTO
Picture source: https://pages.mtu.edu/~shene/NSF-4/Tutorial/VIG/Vig-Base.html
12
Vigenère Cipher - coding
Using Formula (No Vigenère Table):
Step 2: To encrypt a text
Encryption: (plaintext + key) % 26
Decryption: (ciphertext – key + 26) % 26
Plaintext M U L T I M E D I A
Plaintext (ASCII value) 77 85 76 84 73 77 69 68 73 65
Key L O V E L O V E L O
Key (ASCII value) 76 79 86 69 76 79 86 69 76 79
Encryption value 23 8 6 23 19 0 25 7 19 14
Enc value + 'A' (ASCII value) 88 73 71 88 84 65 90 72 84 79
Ciphertext X I G X T A Z H T O
ASCII Table
13
Vigenère Cipher – source code
# To generate key # to encrypt
14
Transposition Cipher
Transposition ciphers use the letters of the plaintext
message, but they permute the order of the letters.
The transposition cipher jumbles up the content of
the plaintext into an order that makes the plaintext
unreadable.
Hence, the order of the letters in the plaintext is re-
arranged to obtain the ciphertext.
Transposition Cipher
To cipher a plaintext:
Count the number of characters in the plaintext and the key.
Draw a number of boxes equal to the key in a single row.
Fill in the boxes from left to right, with one letter per box.
When first row is finish filled in, fill in the remaining letter in
another added row of boxes.
Shade in the unused boxes in the last row.
Starting from the first column at the left side, write out the
letters. When reach the bottom of the column, move to the
next column. Skip any shaded boxes.
15
Transposition Cipher
Example:
Plaintext: MULTIMEDIA key: 3
Ciphertext: MTEAUIDLMI
M U L
T I M
E D I
A
16
Columnar Transposition Cipher
When entering the plaintext, there are some empty cells
in the bottom row of the matrix, one of two approaches
can be taken:
The cells may be left empty, and just ignored during all further
operations (irregular columnar transposition cipher).
The sender may enter there some rare letters, and treat them
as a part of the plaintext. After decryption, the receiver should
be able to determine, that the letters have no sense, and that
they should be ignored (regular columnar transposition cipher).
M U L T I I T L U M
M E L A K K A L E M
A X X X X X X X X A
Ciphertext: IAKXTIAXLDLXUEEXMMMA
17
Playfair Cipher
Playfair Cipher was the first practical digraph substitution
cipher.
It was invented in 1854 by Charles Wheatstone.
However, it was named after Lord Playfair who promoted the
use of the cipher.
Playfair Cipher was used for tactical purposes by British
forces in the Second Boer War and World War I.
It was used for the same purpose by the Australians
during World War II.
Playfair Cipher
In Playfair cipher, a plaintext is divided into groups
of two letters.
Before encryption, a table (key matrix) based on
a keyword is created.
This table has dimensions of 5 by 5 letters and
contains 25 letters of the Latin alphabet
the Latin alphabet has 26 letters, so one should skip one
of the rare letters - for example X or Q; or should
count I and J as one letter.
18
Playfair Cipher
There are three steps in the algorithm:
1. Separate the plaintext into digraph (pairs of two letters)
2. Generate Key Matrix (5x5)
3. Encrypt using Key Matrix from step 2
Playfair Cipher
Example:
Keyword: MULTIMEDIA Plaintext: SOCIETY
Step 1 (rule):
When splitting text into pairs, if the letters are same in a
pair, then insert a rare letter (X or Q) to the original text,
filter ‘Q’ is chosen. Ex: [NN] -> [NQ]
At the end, if only one letter left (no pair), insert filter ‘Q’
Plaintext (SOCIETY): [SO], [CI], [ET], [YQ]
19
Playfair Cipher
Step 2 (rule):
M U L T I
When fill in the keyword into the key
matrix, a letter should occur only one time. E D A B C
Playfair Cipher
Step 3 (rule):
Encrypt: [SO]
Position of ‘S’ and ‘O’ are in same row but different
column, then encrypt the letter with letter in same
row with next column. M U L T I M
S -> O, O -> P E D A B C E
F G H K N F
O P Q R S O
V W X Y Z V
20
Playfair Cipher
Step 3 (rule):
Encrypt: [CI]
Position of ‘C’ and ‘I’ are in different row M U L T I
Playfair Cipher
Step 3 (rule):
Encrypt: [ET]
Position of ‘E’ and ‘T’ are in different M U L T I
21
Playfair Cipher
Step 3 (rule):
Encrypt: [YQ]
Position of ‘Y’ and ‘Q’ are in different M U L T I
22