0% found this document useful (0 votes)
13 views

Lec 10 - Cryptography

Uploaded by

Abc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lec 10 - Cryptography

Uploaded by

Abc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter 10

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]

Picture source: https://masterthecrypto.com/public-vs-private-blockchain-whats-the-difference/

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.

Once the end of the alphabet was


reached, the letters would start over.
...

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

ASCII code conversion


Convert to
Convert to character
number

90 67
Add 65
Take
away 65
25 2
+k 28 Modulo 26

Caesar Cipher
Plaintext: Zebra k=3 Enc(x) = (x + k) % 26

ASCII code conversion


Convert to
Convert to character
number

101 104
Add 97
Take
away 97
4 7
+k 7 Modulo 26

5
Caesar Cipher – source code
def CaesarEnc(text,s): Output:

result = "" Cipher: Cheud


for i in range(len(text)):
char = text[i]

if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
else:
result += chr((ord(char) + s-97) % 26 + 97)
return result

print ("Cipher: " + CaesarEnc("Zebra",3))

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

Picture source: https://academickids.com/encyclopedia/index.php/ROT13

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.

Picture source: https://www.starhop.com/blog/2020/4/15/at-home-stem-activities-diy-escape-room

Atbash Cipher
Plaintext: Zebra Enc(x) = 25 - x

ASCII code conversion

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

ASCII code conversion

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

• The first row of this table


has the 26 English letters.
• Starting with the second
row, each row has the
letters shifted to the left
one position in a cyclic
way.
• For example, when B is
shifted to the first position
on the second row, the
letter A moves to the end.

Picture source: https://pages.mtu.edu/~shene/NSF-4/Tutorial/VIG/Vig-Base.html

Vigenère Cipher – using the table


Vigenère Table is given:
Step 1: to generate a key
 User provide a keyword.
 The key is generated by repeating the keyword circularly
when the plaintext length is less than the keyword length.
Given keyword: LOVE generated key: LOVELOVELO
key L O V E L O V E L O

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.

Vigenère Table – Example explain

Example:
• Key: LOVE
• Plaintext: MULTIMEDIA

• Key: LOVELOVELO
plaintext
• Plaintext: MULTIMEDIA

• Ciphertext: X

Picture source: https://pages.mtu.edu/~shene/NSF-4/Tutorial/VIG/Vig-Base.html

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

Vigenère Cipher - coding


Using Formula (No Vigenère Table):
Step 1: To generate a key (same as previous slide)
Step 2: To encrypt a text
 Take the first letter of the plaintext and the first letter of
the key
 Add their value (each letter have a value based on ASCII
Table, such as 'A' is 65, 'B' is 66, etc).
 The result of the addition modulo 26 (26 refers to the
total number of alphabet) gives the value of the ciphered
letter.

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

Picture source: https://www.asc.ohio-state.edu/demarneffe.1/LING5050/material/characters.html

13
Vigenère Cipher – source code
# To generate key # to encrypt

def generateKey(string, keyword): def encrypt(string, key):


key = list(keyword) encrypt_text = []

if len(string) == len(key): for i in range(len(string)):


return(key) x = (ord(string[i]) +ord(key[i])) % 26
else: x += ord('A')
for i in range(len(string) -len(key)): encrypt_text.append(chr(x))
key.append(key[i % len(key)])
return("" . join(encrypt_text))
return("" . join(key))
# To test
string = "MULTIMEDIA" Output:
keyword = "LOVE"
key = generateKey(string, keyword) key is LOVELOVELO
print("The key is", key) The ciphertext is XIGXTAZHTO
encrypt_text = encrypt(string,key)
print("The ciphertext is", encrypt_text)

Vigenère Cipher – source code explain


Example: x = ('L' + 'M') % 26
 Key: LOVE = (76 + 77) % 26
 Plaintext: MULTIMEDIA = 23
x = x + ('A’)
LOVE LOVELOVELO = 23 + 65
MULTIMEDIA MULTIMEDIA = 88 -> 'X'

The process is repeated for


XIGXTAZHTO next letter until the end of the
plaintext

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

Columnar Transposition Cipher


 The Columnar Transposition Cipher is a form of
transposition cipher.
 Columnar Transposition involves writing the plaintext
out in rows, and then reading the ciphertext off in
columns one by one.
 Both the width of the rows and the permutation of the
columns are usually defined by a keyword.

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

Columnar Transposition Cipher


Example:
Plaintext: MULTIMEDIA MELAKA keyword: SOLID

M U L T I I T L U M

M E D I A A I D E M IAKX TIAX LDLX UEEX MMMA

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

 MULTIMEDIA -> MULTIEDA F G H K N

 The rest of the boxes in the key matrix is O P Q R S


filled by the rest of alphabets that are not V W X Y Z
present in the keyword in ascending order. Key matrix formed by the
 Letter ‘I’ and ‘J’ are considered as same. keyword “multimedia”

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

but same column, then encrypt the E D A B C

letter with letter in different row with F G H K N


same column. O P Q R S
 C -> N, I -> C V W X Y Z

Playfair Cipher
Step 3 (rule):
 Encrypt: [ET]
 Position of ‘E’ and ‘T’ are in different M U L T I

row and different column, then encrypt E D A B C

the letter with letter present at the F G H K N


intersection of ‘E’ and ‘T’. O P Q R S
 E -> B, T -> M V W X Y Z

21
Playfair Cipher
Step 3 (rule):
 Encrypt: [YQ]
 Position of ‘Y’ and ‘Q’ are in different M U L T I

row and different column, then E D A B C


encrypt the letter with letter present F G H K N
at the intersection of ‘Y’ and ‘Q’. O P Q R S
 Y -> X, Q -> R V W X Y Z
 Ciphertext: OPNCBMXR

22

You might also like