Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Computer Security (Cryptography) Ch03
1. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 1
Polyalphabetic Cipher
Leon Battista invented the Polyalphabetic Substitution Cipher in year 1568.
“Poly means Many in Greek language”. This method uses a mixed alphabet to
encrypt a message. The Enigma machine is more complex but still fundamentally a
polyalphabetic substitution cipher.
In polyalphabetic substitution, each occurrence of a character may have a different
substitute. The Polyalphabetic Cipher was adapted as a twist on the standard
Caesar cipher to reduce the effectiveness of frequency analysis on the ciphertext.
The polyalphabetic cipher uses a text string (for example, a word) as a key, which
is then used for doing a number of shifts on the plaintext.
Polyalphabetic Cipher Types
A. Vigenère Cipher
B. One Time Pad Cipher
C. Playfair Cipher
D. Hill Cipher
Vigenere cipher
One such cipher is the famous Vigenere cipher, the Vigenere cipher uses
the power of 26 possible shift ciphers. A tool that was used for the Vigenère
Cipher was a (code wheel) or (Keyword). Vigenere cipher shifts the characters in a
string by some places. Now those some places are determined by the characters
that are present in the key string.
P=P1P2P3…… C=C1C2C3…….. k=[(K1,K2,….. , KM), (K1,K2,….. , KM),..]
Encryption : Ci = Pi + Ki Decryption : Pi = Ci - Ki
2. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 2
Example
Plain Text: “Computer Science”. Key = Security, Cipher Text?
Encode plain text.
Write key under coded plaintext, repeating as many times as necessary.
Encode key.
Add key character code to corresponding plain text code (mod 26).
Decoding of resulting integer is Ciphertext.
Note: If (Pencoding + Kencoding) >= 26 then - 26
Answer (Encryption)
Plain text C O M P U T E R S C I E N C E
Pencoding 2 14 12 15 20 19 4 17 18 2 8 4 13 2 4
Key S E C U R I T Y S E C U R I T
Kencoding 18 4 2 20 17 8 19 24 18 4 2 20 17 8 19
Cipher Code 20 18 14 9 11 1 23 15 10 6 10 24 4 10 23
Cipher text U S O J L B X P K G K Y E K X
+
3. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 3
Decryption
Ciphertext = USOJLBXPKGKYEKX , Key = Security . Plaintext ??
Cipher text U S O J L B X P K G K Y E K X
Cipher Code 20 18 14 9 11 1 23 15 10 6 10 24 4 10 23
Key S E C U R I T Y S E C U R I T
Kencoding 18 4 2 20 17 8 19 24 18 4 2 20 17 8 19
Pencoding 2 14 12 15 20 19 4 17 18 2 8 4 13 2 4
Plain text C O M P U T E R S C I E N C E
-
9 – 20 = – 11 : 26 – 11 = 15
4. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 4
Build Program
1. Add a three label control on the form. Set the first Text property to Plain
text , second label set to Key and third to Cipher text .
2. Add three text box controls as shown.
o Name the first text box control PTtxt
o Name the second text box control Keytxt
o Name the third text box control CTtxt
3. Add two button controls as shown.
o Name the first button control Encryption
o Name the second button control Decryption
5. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 5
Program Coding
Double-click the Encryption control. And write follow code.
Double-click the Decryption control. And write follow code.
Now you can write class VigenereCipher that contain two function Encrypt
For return Cipher text and Decrypt for return Result Plain to the end user
Depending on the key type string , this function will receive 2 parameters
when you click button Enc or Dec :
Plain text
Key Secret
Private Sub Encbtn_Click(sender As Object, e As EventArgs) Handles Encbtn.Click
CTtxt.Text = VigenereCipher.Encrypt(PTtxt.Text, Keytxt.Text)
End Sub
Private Sub Decbtn_Click(sender As Object, e As EventArgs) Handles Decbtn.Click
CTtxt.Text = VigenereCipher.Decrypt(PTtxt.Text, Keytxt.Text)
End Sub
6. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 6
Public Class VigenereCipher
Public Shared Function Encrypt(ByVal plainTxt As String, ByVal key As
String)
Dim encryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = AscW(GetChar(cipherTxt, i)) +
AscW(GetChar(key, i Mod key.Length + 1))
encryptedText += ChrW(temp)
Next
Return encryptedText
End Function
Public Shared Function Decrypt(ByVal cipherTxt As String, ByVal key
As String)
Dim decryptedText As String = ""
For i As Integer = 1 To cipherTxt.Length
Dim temp As Integer = AscW(GetChar(cipherTxt, i)) -
AscW(GetChar(key, i Mod key.Length + 1))
decryptedText += ChrW(temp)
Next
Return decryptedText
End Function
End Class
7. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 7
One Time Pad Cipher
known as Vernam Cipher is implemented using a random set of non
repeating characters as the Key. The rest of process is same as the Vigenère cipher.
The sender uses each key letter on the pad to encrypt exactly one plaintext
character. Encryption is the addition modulo 26 of the plaintext character and the
one-time pad key character
(Pencoding + Kencoding) >= 26 then - 26
Example: ( Encryption )
Plaintext: “ HOW ARE YOU ’’ Key = NCBTZQARX Ciphertext: ?
Plain text H O W A R E Y O U
Pencoding 7 14 22 0 17 4 24 14 20
Key N C B T Z Q A R X
Kencoding 13 2 1 19 25 16 0 17 23
Cipher Code 20 16 23 19 16 20 24 5 17
Cipher text U Q X T Q U Y F R
+
8. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 8
(Decryption)
Cipher text U Q X T Q U Y F R
Cipher Code 20 16 23 19 16 20 24 5 17
Key N C B T Z Q A R X
Kencoding 13 2 1 19 25 16 0 17 23
Pencoding 7 14 22 0 17 4 24 14 20
Plain text H O W A R E Y O U
-
16 – 25 = – 9 26 – 9 = 17
9. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 9
Playfair Cipher
The Playfair cipher encrypts pairs of letters, instead of single letters. This is
significantly harder to break. Each plaintext letter is replaced by a diagram in this
cipher.
User chooses a keyword and puts it in the cells of a 5 x 5 matrix.
Letter I and J always stay in one cell .
Duplicate letters appear only once.
Number of diagrams is 26 x 26 = 676.
To encrypt a message, one would break the message into groups of 2
letters. If there is a dangling letter at the end, we add an X. For
example. "Secret Message" becomes "SE CR ET ME SS AG EX".
We now take each group and find them out on the table. we apply the
following rules, in order.
Playfair Cipher Rules
1. If the digraph consists of the same letter twice (or there is only one
letter left by itself at the end of the plaintext) then insert the letter "X"
between the same letters (or at the end), and then continue with the
rest of the steps.
2. If the two letters appear on the same row in the square, then replace
each letter by the letter immediately to the right of it in the square
(cycling round to the left hand side if necessary).
3. If the two letters appear in the same column in the square, then
replace each letter by the letter immediately below it in the square
(cycling round to the top of the square if necessary).
4. Otherwise, form the rectangle for which the two plaintext letters are
two opposite corners. Then replace each plaintext letter with the letter
that forms the other corner of the rectangle that lies on the
same row as that plaintext letter (being careful to maintain the order).
10. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 10
Example
Plain text = Hide the gold in the tree stump , key = playfair example ,
Cipher text ??
Firstly we must generate the Polybius Square that we are going to use. We
do this by setting out a 5x5 grid, and filling it with the alphabet, starting with the
letters of the keyphrase, and ignoring any letters we already have in the square. We
are also going to combine "I" and "J" in the square.
We must now split the plaintext into digraphs, and split up any double letter
digraphs by inserting an "x" between them. The first image below shows the initial
digraph split of the plaintext, and the second image displays how we split up the
"ee" into "ex" and "es". In this case, when we insert this extra "x", we no longer
need to have one at the end of the plaintext.
HI DE TH EG OL DI NT HE TR EE ST UM P
P L A Y F
I R E X M
B C D G H
K N O Q S
T U V W Z
HI DE TH EG OL DI NT HE TR EX ES TU MP
11. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 11
We now take each digraph in turn and apply rule 2, 3 or 4 as necessary. Each
step is show below with a visual representation of what is done for each digraph.
15. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 15
We can now take each of the ciphertext digraphs that we produced and put
them all together.
BM OD ZB XD NA BE KU DM UI XM MO UV IF
Homework
Decrypt the Cipher text = BMODZBXDNABEKUDMUIXMMOUVIF .
When the key = playfair example
16. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 16
Hill Cipher
The Hill Cipher was invented by Lester S. Hill in 1929, and like the
other Digraphic Ciphersit acts on groups of letters. Unlike the others though it is
extendable to work on different sized blocks of letters. So, technically it is a
polygraphic substitution cipher, as it can work on digraphs, trigraphs (3 letter
blocks) or theoretically any sized blocks.
The Hill Cipher uses an area of mathematics called Linear Algebra, and in
particular requires the user to have an elementary understanding of matrices. It also
make use of Modulo Arithmetic (like the Affine Cipher). Because of this, the
cipher has a significantly more mathematical nature than some of the others.
However, it is this nature that allows it to act (relatively) easily on larger blocks of
letters.
Encryption
To encrypt a message using the Hill Cipher we must first turn our keyword into a
key matrix (a 2 x 2 matrix for working with digraphs, a 3 x 3 matrix for working
with trigraphs, etc). We also turn the plaintext into digraphs (or trigraphs) and each
of these into a column vector. We then perform matrix multiplication modulo the
length of the alphabet (i.e. 26) on each vector. These vectors are then converted
back into letters to produce the ciphertext.
2 x 2 Example
We shall encrypt the plaintext message "short example" using the
keyword hill and a 2 x 2 matrix. The first step is to turn the keyword into a matrix.
With the keyword in a matrix, we need to convert this into a key matrix.
17. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 17
We now split the plaintext into digraphs, and write these as column vectors. That
is, in the first column vector we write the first plaintext letter at the top, and the
second letter at the bottom
Now we must convert the plaintext column vectors in the same way that we
converted the keyword into the key matrix.
Now we must perform some matrix multiplication. We multiply the key matrix by
each column vector in turn.
To perform matrix multiplication we "combine" the top row of the key matrix with
the column vector to get the top element of the resulting column vector. We then
"combine" the bottom row of the key matrix with the column vector to get the
bottom element of the resulting column vector.
18. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 18
Next we have to take each of these numbers, in our resultant column vector,
modulo 26 (remember that means divide by 26 and take the remainder).
Finally we have to convert these numbers back to letters, so 0 becomes "A" and 15
becomes "P", and our first two letters of the ciphertext are "AP".
19. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 19
Encryption process of the first digraph
22. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 22
Decryption
To decrypt a ciphertext encoded using the Hill Cipher, we must find the inverse
matrix. Once we have the inverse matrix, the process is the same as encrypting.
In general, to find the inverse of the key matrix, we perform the calculation below,
where Kis the key matrix, d is the determinant of the key matrix and adj(K) is the
adjugate matrix of K.
2 x 2 Example
We shall decrypt the example above, so we are using the keyword hill and our
ciphertext is "APADJ TFTWLFJ".
Step 1 - Find the Multiplicative Inverse of the Determinant
The determinant is a number that relates directly to the entries of the matrix. It is
found by multiplying the top left number by the bottom right number and
subtracting from this the product of the top right number and the bottom left
number.
23. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 23
We now have to find the multiplicative inverse of the determinant working
modulo 26. That is, the number between 1 and 25 that gives an answer of 1 when
we multiply it by the determinant. So, in this case, we are looking for the number
that we need to multiply 15 by to get an answer of 1 modulo 26.
So the multiplicative inverse of the determinant modulo 26 is 7. We shall need this
number later.
Step 2 - Find the Adjugate Matrix
The adjugate matrix is a matrix of the same size as the original. For a 2 x 2 matrix,
this is fairly straightforward as it is just moving the elements to different positions
and changing a couple of signs.
24. Computer Security & Networks
Dr. Saif Kassim Alfraije Page 24
Again, once we have these values we will need to take each of them modulo 26 (in
particular, we need to add 26 to the negative values to get a number between 0 and
25. For our example we get the matrix below.
Step 3 - Multiply the Multiplicative Inverse of the Determinant by the
Adjugate Matrix
To get the inverse key matrix, we now multiply the inverse determinant (that was 7
in our case) from step 1 by each of the elements of the adjugate matrix from step 2.
Then we take each of these answers modulo 26.
Now we have the inverse key matrix, we have to convert the ciphertext into
column vectors and multiply the inverse matrix by each column vector in turn, take
the results modulo 26 and convert these back into letters to get the plaintext.