Wednesday, March 23, 2016

An attempt on Hill Cipher.

An attempt on Hill Cipher.
It has been a possible attempt to write on Hill Cipher Step by step in order to not only makes it a child’s play but also strive to makes it more and more understandable.
Why it is Hill Cipher?
It is Hill Cipher because the mathematician Laster Hill developed at in 1929.
Points should be kept in mind!
Ø  It is a classical cipher, as if Caesar cipher, Play fair cipher and so on.
Ø  It is a symmetric cipher, which means key is shared between both parties- sender and receiver-.
Ø  It is a substitution and matrix based cipher, means with the help of using mathematics matrix operation the position of letters are be changed.
Let’s play the game now.
Step 1: first of all select a key. But, select the key in matrix, as I selected:
[2 3]
[3 4] Note: This key will be shared BTW both parties.
Step 2:
Just select a plain text which you want to encrypt. As I selected “RACEY”.
Step 3:
Break the message into pairs of letter which means create the pair of letters. Start pairing from left to right.
As I do so.
RA
CE
Y
1st pair
2nd pair
What about Y?
 How long your message will be, you should break and pair it.
Considerable: But Y is a single letter how a single letter can create a pair?
Step 4:
It’s very interesting as we know that the hill cipher needs to break the message and create pairs. But, if, the message is break and created pairs where still a letter remains for pairing, which have no other extra letter to fulfill the pair demand. In such case we use the concept of Pillar Letter
Step 4: what is pillar letter?
The pillar is a helper which helps the other letter to fulfil the requirements of pairing. In our case the pillar letter will help” Y” to create its pair. You can select any letter which is used as a pillar but the second party must be informed which is your pillar letter. Mostly, “X” is used as pillar letter. I am also using X is pillar letter. Now the pairs will be.
This is the sequence of pairing the letters.
RA
CE
YX
1st pair
2nd pair
3rd pair
 Step 5:
Now put the letters into their position. Mostly, the letters position started from 0 and end 25 like array.
As I put them like.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Step 6:
Start putting the letters:
1st pair: R {17} A {0} second pair: C {2} E {4} third pair: Y {24} X {22}.

Step 7:
Now apply the Matrix operation with the help of key on each pair.
As I did so.
1st pair:
  [2 3] [17] Mode 26
 [3 4] [0]                                         
 Perform the matric like this: multiply the row of First table with the column of second table. AS,
(2*17) + (3*0) =34+0=34
(3*17)+ (4*0) =51+0=51
 As you know we are using 26 mode because of total letters are 26.
Use the mode 26 and get the output.
[34] Mode 26 =8
[51] =====     =25
 What come after the mode find the position of that letter. Which will be your cipher letter. As I did for 1st which is on 8th and second is on 25th.
 8[I] and 25[Z]. It Will be first pair output.
Your first Pair cipher: 8[I] and 25[Z] =IZ
Note: Don’t worry if you don’t know how to perform the mode operation. Just wait or see below. In last step.
Second pair:
Second pair: C {2} E {4}
  [2 3] [2] Mode 26 = [16]
 [3 4] [4] ------------ = [22]
(2*2) + (3*4) =4+12=16
(3*2) + (4*4) =6+16=22
The output of second pair will be: 16[Q] and 22[W]. Or QW.

 3rd pair:
Third pair is: Y {24} X {22}.
  [2 3] [24] Mode 26 = [114] = [10]
 [3 4] [22] ------------ = [162] = [6]
(2*24) + (3*22) =48+66=114
(3*22) + (4*24) =66+96=162
Output will be: [10] =K and [6] =G.
Finally, we have encrypted our message. Pair by pair and step by step. Which is.
Plain text is “RACEY”.
Cipher text is: “IZQWKG”.
Last:
How to perform Mode operation?
 Keep in mind we have length of 26 letters, 0-25. If we multiply the Matrix key with the letter’s position which can exceed the length of 26. Therefore, we need to put a limit through which we will be able to not let the letters to cross the length of 26.
But how really this mode magical operation work?
It is really too simple, just get the product of multiplied letters with key. And divide them with 26. Get the reminder, the reminder will be your cipher letter position as I did with first pair:
Output of multiplied “R”, (2*17) + (3*0) =34+0=34
Now I divided 34/26.
34 will be divided by 26 only one time.
Reminder will be 8 (try and confirm it). Take the 8 [I] in place of 17[R].
But what if the multiplied product is less than 26?
No matter, get relaxed. If, it is not greater than 26, then simply put the same number of letter in place of plain text. As I did in 2nd pair. Where both 16 and 22 are not greater than 26. Simply put both of them as: 16[Q] and 22[W] in place of C [2] E [4].

Got? If still have confusion, don’t hesitate just put your question and get the possible Anwser.

Sunday, March 06, 2016

S-DES or Simplified Data Encryption Standard

S-DES or Simplified Data Encryption Standard

The process of encrypting a plan text into an encrypted message with the use of S-DES. The process has been divided into multi-steps which may help you to understand it as easy as possible.
Points should be remembered.
1-It is a block cipher.
2-It has 8-bits block size of plain text or cipher text.
3-It uses 10-bits key size for encryption.
4-It is a symmetric cipher.
5.It has Two Rounds.
Let’s start the game!

Key Generation of S-DES or How to Generate the Key of Simplified DES

First  and foremost, we need to generate a key. With the help of this key we will encrypt the message.
Now the interesting question is, how to generate the key,  and where the key is to be used, just follow the steps.
Step 1:
Just, Select a random key of 10-bits, which only should be shared between both parties which means sender and receiver.
As I selected below!
Select key:1010000010
Note: you can select any random number of 10-bits.
Step 2:                                                                            
Put this key into P.10 Table and permute the bits.
P.10 Table:
Input
1
2
3
4
5
6
7
8
9
10
Output Should be
3
5
2
7
4
10
1
9
8
6
 As  I put key into P.10 Table
Input
1
0
1
0
0
0
0
0
1
0
Output
1
0
0
0
0
0
1
1
0
0
Now the output will be:
Key: 1000001100
Step 3:
Divide the key into two halves, left half and right half;
 {1 0 0 0 0} | {0 1 1 0 0}
 Step 4:
Now apply the one bit Round shift on each half:
Before round shift: {10000} | {01100}
After round shift: {00001}   | {11000}
The output will be:
{0 0 0 0 1} {1 1 0 0 0}
Step 5:
Now combines once again the both halve of the bits, right and left. Put them into the P8 table. What you get, that will be the K1 or First key.
Combine: 0 0 0 0 1 1 1 0 0 0
Permute into 8bit table:
P8-Table
Input
1
2
3
4
5
6
7
8
9
10
Combine-bits
0
0
0
0
1
1
1
0
0
0
Output Should be
6
3
7
4
8
5
10
9


Output bits
1
0
1
0
0
1
0
0



See the table the 1 and 2 number of bits are removed and other are permuted, as 6 in place of one, 9 in place of 8 and so on.
The output and K1 or key One will be:
K1=1 0 1 0 0 1 0 0
Step6:
As we know S-DES has two round and for that we also need two key, one key we generate in above steps (step1 to step5). Now we need to generate second bit after that we will move to encrypt the plain text or message.
It is simple to generate the second key. Simply, go in step 4 copy the both halves, each of one consists 5 bits. But be careful on taking of bits. Select those halves which are output of first round shift, don’t take the bits which are not used in first round. In simple word take the output of first round shift in above step 4.
Which are: {00001}   | {11000}
Step 7:
Now just apply two round shift circulate on each half of the bits, which means to change the position of two bits of each halves.
left half: 00001
Right half: 11000
After the two rounds shift on each half out-put of each half will be.
 Left half: 00100
Right half: 00011
Combine both together: As: 0 0 1 0 0 – 0 0 0 1 1
Step 8:
Now put the bits into 8-P Table, what you get, that will be your second key. Table is also given in step 5.
But here the combinations of bits are changed because of two left round shift from step 5. Check it in depth.
Combine bits: 0 0 1 0 0 0 0 0 1 1
P.8 Table
Input
1
2
3
4
5
6
7
8
9
10
Combine-bits
0
0
1
0
0
0
0
0
1
1
Output Should be
6
3
7
4
8
5
10
9


Output bits
0
1
0
0
0
0
1
1


The output of the bits are your Second key or K2:
K2: 0 1 0 0 0 0 1 1
Finally we create both keys successfully:
K1: 1 0 1 0 0 1 0 0 (see in step 5)
K2: 0 1 0 0 0 0 1 1 (see in step 8)

____________________________________________________________
How To Encrypt the Plain Text into Cipher Text in S-DES After Generating Keys.

Now, let’s start Encryption of plain text into cipher text.

Encryption of Plain text into Cipher text in S-DES:

Come on do it, step by step.
Note: the size of input text is 8 bit and output also will be 8-bit. Or the block size is 8-bit/one byte always.
Step 1:
Suppose this is our plain text in binary and which is 8-bit.
Plain text: 01110010
Step 2:
Put the plain text into IP-8(initial permutation) table and permute the bits.
IP-8 table
Bits number
1
2
3
4
5
6
7
8
Bits to be permute
0
1
1
1
0
0
1
0
Permute the bits
2
6
3
1
4
8
5
7
Permuted bits
1
0
1
0
1
0
0
1
 Output is: 1 0 1 0 1 0 0 1
Step 3:
Now break the bits into two halves, each half will be consisted of 4 bits. The halves will be right and left.
 Two Halves of the bits:
     Left half {1 0 1 0} -right half {1 0 0 1}
Step 4:
Take the right 4 bits and put them into E.P (expand and per-mutate) Table.
Bits of right half: 1001
E.P Table
Right half bits
1
0
0
0




Number
1
2
3
4
5
6
7
8
Expand bits
4
1
2
3
2
3
4
1
Output of bits
0
1
0
0
0
0
0
1
Output of right four bits will be 8 bits, after their expanding with the help of E.P table.
O-P: 0 1 0 0 0 0 0 1
Step 5: Now, just take the output and XOR it with First key Or K 1 (which we created in previous topic that is how to generate key.).
 XOR (⊕) them:
                     O-p: 0 1 0 0 0 0 0 1
                                  
                       K1: 1 0 1 0 0 1 0 0
Output of XOR: 1 1 1 0 0101
Step 6:
Once again split the output of XOR’s bit into two halves and each half will be consisted of 4 bits.
Splitting them into two halves:
   Left half :{ 1 1 1 0} right half :{ 0101}
Now put the each half into the s-boxes, there is only two s-boxes. S-0 and S-1.
S-0
Col
0
1
2
3
 Rows




0
01
00
11
10
1
11
10
01
00
2
00
10
01
11
3
11
01
11
10
S-1
Col
0
1
2
3
Rows




0
00
01
10
11
1
10
00
01
11
2
11
00
01
00
3
10
01
00
11


Note: put the left half into S-0 box and put the right half into S-1 Box.
But how to put them in into S-Boxes?
Take any half, (but don’t forget the above mentioned note..).
The most first and most last bit will be consider the, row and other remaining, which are, 2 and 3, will be considered the columns.
See, here I’m taking the left half: which is 1 1 1 0.
Now I will take First and last bit which are: 1 and 0. These will be row.
And I also will take 2nd and 3rd bits which are: 11. These will be column number.
 10 means=2rd row
11 means = 3rd col
See below how it will be the second row, and 3rd column. Please, remember the IP Addressing, such as 28 for 255.
12 01 =2+0=2
12 11 =2+1=3
Let’s check the 2nd row and 3rd column.
For left half we check the in S-0 . In which 2nd row and 3rd column.
The output is 00 for left half;
Now let’s take the right half and find the output of the right of in S-1 box.
Right half: 0101
Row: 0 1= 02 11=0+1=1
Col: 1 0=12 01=2+1=3
Which means the value will be in 1st row and 3rd column, let’s in check S-1.
The output will be: 11 for left half.
Step 7:
Now combines these two halves together.
Left half: {00} and right half: {11}
 It will be: 0 0 1 1
Step 8: Now take these 4 bits and put them in P-4 (permutation 4) table and get the result.
P 4 Table
Numbers
1
2
3
4
Input
0
0
1
1
Output should be
2
4
3
1
Out-Put
0
1
1
0
Now the output is: 0 1 1 0
Step 9:
Now get XOR the output with left 4 bits of Initial Per-mutation. The left bits of initial per-mutation are in step 3, which are 1 0 1 0.( please, in step 3).
Let them to be XOR.
                              0 1 1 0
                                 
                               1 0 1 0
Out-put will be:   1 1 0 0
Step 10:
Now get the right half of the initial permutation, which is step 3, and combine that with this out- put.
The out-put of XOR in step 9: 1 1 0 0
Right half of IP (initial permutation): 1 0 0 1
Let’s combine both. 1 1 0 0 – 1 0 0 1= 1 1 0 0 1 0 0 1
Now the output is 8 bits.: 1 1 0 0 1 0 0 1
Step 11: Now once again break the out-put into two halves, left and right;
Left: {1 1 0 0} right: {1 0 0 1}
Step 12:
Now swap both halves, which means put the left half in place of right and vice versa.
Result:
Left half: {1 0 0 1} right half: {1 1 0 0}
Step 13:
Now let’s take these halves and once again start same  procedure from step 2 or initial Permutation, BUT be careful on using key in this stage we use second key or K2 (not K1).  And put that into IP-1 (IP inverse) Table. What you get that will be your final cipher text.
Let me to do it in brief. You should check carefully what I did.
Ø  1 0 0 1 11 0 0
After initial permutation according to IP-8 table (see step 2)
Out-put will be: 0 1 0 1 1 0 1 0
Ø  Now break it into two halves
Left {0 1 0 1} right {1 0 1 0}
Ø  Now Take the right 4bits and put them into EP table, and get the result of 8 bits.
It will be: 0 1 0 1 0 1 0 1
Ø  Let XOR it with K2.
                    K2:   0 1 0 0 0 0 1 1
                                       
   Out-put of EP:   0 1 0 1 0 1 0 1
            Out- Put: 0 0 0 1 0 1 1 0
Ø  Once again split the output of XOR’s bit into two halves:
Left: {0 0 0 1} right: {0 1 1 0}
Ø  Now put each half in S-Boxes, which are S-0 and S-1:
Note: put the left half into S-0 box and put the right half into S-1 Box.
Before that get Rows and column:
Left: 0 0 0 1
Row: 0 1= 1
Col: 0 0 = 0
Let find the row and column of left, in S-0, the value is row number one and col number Zero.
It will be. 1 1                                               
Ø  now check the right half: 0 1 1 0
Row: 0 0=0
Col:  1 1= 3
Let's find the row and column of right, in S-1, the value is row number zero and col number three.
It will be:  11
Ø  Now combine both halves together.
It will be: 1 1 1 1
Ø  Now take these 4 bits and put them in P-4 ( Per-mutation 4) table and get the result.
The out-put also will be: 1111 after permutation.
Ø  Now get it XOR with left 4 bits of Initial Per-Mutation.
                 1 1 1 1
                   XOR
                 0 1 0 1
Out-put:1 0 1 0
Ø  Now get the right half of the initial permutation and combine that with this out- put.
1 0 1 0 - 0 1 1 0
Ø  Now once again break the it into two halves, left and right
Left: {1 0 1 0} right: {0 1 1 0}
Ø  Now swap both halves, which means put the left half in place of right and vice versa.
0 1 1 0 1 0 1 0
Ø  Now put it into IP-1 Table which is :
 IP-1 Table
Numbers
1
2
3
4
5
6
7
8
input
0
1
1
0
1
0
1
0
Out-put to be
2
6
3
1
4
8
5
7
Out-Put
1
0
1
0
0
0
1
1

                       Out-Put is the cipher text. Which is: 1 0 1 0 0 0 1 1
 Finally we Encrypted successfully our plain text: 01110010 into cipher text which is:
         1 0 1 0 0 0 1 1
Note: we have encrypted simply a single block. : P
                                                       The End