Vous êtes sur la page 1sur 55

1

Designing and Implementation of Block Cipher DES-64 bits

1. INTRODUCTION
There are two main types of cryptography in using today - symmetric or secret
key cryptography and asymmetric or public key cryptography. Symmetric key cryptography
is the oldest type whereas asymmetric cryptography is only being used publicly since the late
1970s. Asymmetric cryptography was a major milestone in the search for a perfect
encryption scheme. Secret key cryptography goes back to at least Egyptian times and is of
concern here. It involves the use of only one key which is used for both encryption and
decryption hence the use of the term symmetric. Figure below depicts this idea. It is
necessary for security purposes that the secret key never be revealed.

Figure 1.1: Crtptographic technique


To accomplish encryption, most secret key algorithms use two main techniques known as
substitution and permutation. Substitution is simply a mapping of one value to another
whereas permutation is a reordering of the bit positions for each of the inputs. These
techniques are used a number of times in iterations called rounds. Generally, the more rounds
there are, the more secure the algorithm. A non-linearity is also introduced into the encryption
so that decryption will be computationally infeasible without the secret key. This is achieved
with the use of S-boxes which are basically non-linear substitution tables where either the
output is smaller than the input or vice versa
One of the main problems with secret key cryptography is key distribution. For this form of
cryptography to work, both parties must have a copy of the secret key. This would have to be
communicated over some secure channel which, unfortunately, is not that easy to achieve. As
will be seen later, public key cryptography provides a solution to this.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

2. PROJECT DESCRIPTION
2.1 DES Data Encryption Standard
The Data Encryption Standard is a block cipher, meaning a cryptographic key
and algorithm are applied to a block of data simultaneously rather than one bit at a time. To
encrypt a plaintext message, DES groups it into 64-bit blocks. DES works by using the same
key to encrypt and decrypt a message, so both the sender and the receiver must know and use
the same private key and hence it is a symmetric block cipher.
DES algorithm takes a fixed-length string of plaintext bits and transforms it through a
series of complicated operations into another cipher text bit string of the same length. In the
case of DES, the block size is 64 bits. DES also uses a key to customize the transformation,
so that decryption can supposedly only be performed by those who know the particular key
used to encrypt. The key consists of 64 bits. However, only 56 of these are actually used by
the algorithm. Eight bits are used for checking parity, and are thereafter discarded. Hence the
effective key length is 56 bits.
2.2 Workings of DES
DES is based on a cipher known as the Feistel block cipher. This was a block cipher
developed by the IBM cryptography researcher Horst Feistel in the early 70s. It consists of a
number of rounds where each round contains bit-shuffling, non-linear substitutions or Sboxes and exclusive OR operations. Most symmetric encryption schemes today are based on
this structure known as a feistel network.As with most encryption schemes, DES expects two
inputs - the plaintext to be encrypted and the secret key. The manner in which the plaintext is
accepted, and the key arrangement used for encryption and decryption, both determine the
type of cipher it is. DES is therefore a symmetric, 64 bit block cipher as it uses the same key
for both encryption and decryption and only operates on 64 bit blocks of data at a time be
they plaintext or cipher text. The key size used is 56 bits, however a 64 bit or eight-byte key
is actually input. The least significant bit of each byte is either used for parity (odd for DES)
or set arbitrarily and does not increase the security in any way. All blocks are numbered from
left to right which makes the eight bit of each byte the parity bit. Once a plain-text message is
received to be encrypted, it is arranged into 64 bit blocks required for input. If the number of
bits in the message is not evenly divisible by 64, then the last block will be padded. Multiple
permutations and substitutions are incorporated throughout in order to increase the difficulty
of performing a cryptanalysis on the cipher.
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

2.3 Overall structure


Figure below shows the sequence of events that occur during an encryption operation. DES
performs an initial permutation on the entire 64 bit block of data. It is then split into 2, 32 bit
sub-blocks, Li and Ri which are then passed into what is known as a round, of which there
are 16 rounds in DES. Each of the rounds are identical and the effects of increasing their
number is two fold - the algorithms security is increased and its temporal efficiency
decreased. Clearly these are two conflicting outcomes and a compromise must be made. For
DES the number chosen was 16, probably to guarantee the elimination of any correlation
between the cipher text and either the plaintext or key. At the end of the 16th round, the 32 bit
Li and Ri output quantities are swapped to create what is known as the pre-output. This [R16,
L16] concatenation is permuted using a function which is the exact inverse of the initial
permutation. The output of this final permutation is the 64 bit cipher text.

Figure 2.1: Flow Diagram of DES algorithm for encrypting data


So in total the processing of the plaintext proceeds in three phases as can be seen from the left
hand side of figure. Initial permutation box rearrange the bits to form the permuted input.
Followed by 16 iterations of the same function. The output of the last iteration consists of 64
bits which is a function of the plaintext and key. The left and right halves are swapped to
produce the pre output. Finally, the pre output is passed through a permutation IP1 which is
simply the inverse of the initial permutation (IP). The output of IP1 is the 64-bit cipher text
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

Table 2.1: Permutation tables used in DES.


As figure above shows, the inputs to each round consist of the Li , Ri pair and a 48 bit
sub key which is a shifted and contracted version of the original 56 bit key. The use of the
key can be seen in the right hand portion of the flow Diagram of DES algorithm for
encrypting data.

Initially the key is passed through a permutation function PC1 which is mentioned in the
figure above
For each of the 16 iterations, a sub key (Ki) is produced by a combination of a left circular
shift and a permutation PC2 which is the same for each iteration. However, the resulting sub
key is different for each iteration because of repeated shifts.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

Table 2.2: DES key schedule.

2.4 Details of individual rounds:


Details of an individual round can be seen in figure below. The main operations on the data
are encompassed into what is referred to as the cipher function . This function accepts two
different length inputs of 32 bits and 48 bits and outputs a single 32 bit number. Both the data
and key are operated on in parallel, however the operations are quite different. The 56 bit key
is split into two 28 bit halves Ci and Di (C and D being chosen so as not to be confused with
L and R). The value of the key used in any round is simply a left cyclic shift and a permuted
contraction of that used in the previous round. Mathematically, this can be written as

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

where Lcsi is the left cyclic shift for round i, Ci and Di are the outputs after the shifts, PC2 is
a function which permutes and compresses a 56 bit number into a 48 bit number and Ki is the
actual key used in round i. The number of shifts is either one or two and is determined by the
round number i. For i = {1, 2, 9, 16} the number of shifts is one and for every other round it
is two (table 2.2).

Figure 2.2: Details of a single DES round.

The common formulas used to describe the relationships between the input to one round and
its output or the input to the next round are:
Li = Ri1 (2.3)
Ri = Li1 F(Ri1, Ki) (2.4)
where L and R have their usual meaning and F is the cipher function. This function F
is the main part of every round and consists of four separate stages .
1. The E-box expansion permutation - here the 32-bit input data from Ri1 is expanded and
permuted to give the 48 bits necessary for combination with the 48 bit key. The E-box
expansion permutation delivers a larger output by splitting its input into 8, 4-bit blocks and
copying every first and fourth bit in each block into the output in a defined manner. The
security offered by this operation comes from one bit affecting two substitutions in the Sboxes.
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

2. The bit by bit addition modulo 2 or exclusive OR of the E-box output and 48 bit sub key
Ki
3. The S-box substitution - this is a highly important substitution which accepts a 48-bit
input and outputs a 32-bit number. The S-boxes are the only non-linear operation in DES and
are therefore the most important part of its security. They were very carefully designed
although the conditions they were designed under has been under intense scrutiny since DES
was released. The input to the S-boxes is 48 bits long arranged into 8, 6 bit blocks (b1, b2, . . .
, b6). There are 8 S-boxes (S1, S2, . . . , S8) each of which accepts one of the 6 bit blocks.
The output of each S-box is a four bit number. Each of the S-boxes can be thought of as a 4
16 matrix. Each cell of the matrix is identified by a coordinate pair (i, j). The value of i is
taken as the decimal representation of the first and last bits of the input to each S-box, Each
cell within the S-box matrices contains a 4-bit number which is output once that particular
cell is selected by the input.
4. The P-box permutation - This simply permutes the output of the S-box without changing
the size of the data (defined in table 2.1). It is simply a permutation and nothing else. It has a
one to one mapping of its input to its output giving a 32 bit output from a 32 bit input.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

3. MODULES DESCRIPTION AND WORKING


3.1 Defining & Initialization of all required functions required for DES
Task 1: Defining Initial Permutation and Inverse IP functions

There are two inputs to the DES, one is the plaintext and the other is the 64bit key.
The plaintext is divided into blocks by the multiples of 64. If the last block plaintext is not of
size 64bit then it is filled with zeros. The plaintext is then permutated which is known as
rearranging of bits. The size of the plaintext is not changed but the plaintext is rearranged.
The initial and final inverse initial permutation does not increase any security to that message
or plaintext, but makes the implementation easier in some contexts. It is made for making
hardware circuit part easier. The figure below is the standard initial permutation table which
is applied before encrypting the plaintext. The 58th bit of the plaintext is taken as 1st bit, the
50th bit of plaintext is taken as 2nd bit and the 7th bit of plaintext is taken as last bit. Thus the
plaintext is rearranged by using the initial permutation table.

Figure 1.1: Initial Permutation


The final permutation or the inverse initial permutation is the reverse of the initial
permutation which is applied for the result of 16th round after encryption.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

Figure 3.2: Inverse Initial Permutations


The result after the encryption of plaintext is permutated using the inverse initial permutation
table shown above figure 1.5. The 40th bit of the result is taken as 1 st bit of the cipher text, the
8th bit is taken as 2nd bit of cipher text and the 25th bit is taken as the last bit of cipher text. The
resulted 64bit is called as the cipher text for encrypted plaintext.
Task 2: Defining Expansion & Permutation function

The result of the initial permutation 64bit is divided into two 32bit blocks L i and the Ri. The
right half 32bits Ri is taken and expanded to 48bit and is rearranged which means permutated
by using the expansion and permutated table.

Figure 3.3: Expansion and Permutation table

Task 3: Defining S-Box Function


Each S-box replaces a 6-bit input with a 4-bit output. Given a 6-bit input, the 4-bit output is
found by selecting the row using the outer two bits, and the column using the inner four bits.
For example, an input "011011" has outer bits "01" and inner bits "1101"; noting that the first
row is "00" and the first column is "0000", the corresponding output for S-box S 5 would be
"1001" (=9), the value in the second row, 14th column.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

10

Figure3.4: Substitution Boxes

Task 4: Defining PC-1 and PC-2

The 56-bit key used by DES is, when in standard format, stored in eight bytes, in
which the least significant bit of each byte is used for parity. Thus, the permutation, called
Permuted Choice 1, which divides the 56-bit key into two 28-bit halves, acts on bits 1

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

11

through 7, 9 through 15, 17 through 23, and so on. The figure below shows the table for
permutated choice 1.

Figure 3.5: Permutated Choice1 table


The divided two 28bit halves Ck and Dk are now permutated by using the permutated choice2
for getting the sub keys. The permutated choice2 is applied 16 times so that 16 different sub
keys are generated for the 16 rounds in DES. The 56bit key generated from the permutated
choice1 is now reduced to the 48bit 16 sub keys Ki where i represent the sub key number. The
figure below shows the permutated choice2 table.

Figure 3.6: Permutated Choice2 table

Task 5: Defining Shift Key function


After permutated choice1 the resulted Ck and Dk values are circularly shifted 16 times for
getting the sub keys of size 56bit each. By applying the circular shift twice we will get the 16
sub keys C0 D0 to C15 D15. The resulted Ck and Dk values are processed with permutated
choice2 for getting the 48 bit sub keys Ki.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

12

Figure 3.7 Left shifts

Task 6: Defining Initial Key & Plain text input of 64- bits
The initial key and the input plaintext size is for the DES is given as 64 bits, so the 64bit
plaintext is taken as one block and the initial vector of 64 bit is XOR with the 64bit block
plain text. The plaintext is 64bit so there is no need for padding zeros.
Task 7: Defining the other constant functions require for DES implementation
All the required constant functions that are required for DES have been successfully defined.

3.2 Key Generation Phase

The key generation phase in DES for encryption is the algorithm which generates the
sub keys. Initially, the 64 bit input is taken as the key and then 56 bits of the key are selected
from the initial 64bit input by Permuted Choice 1 (PC-1). The remaining eight bits are either
discarded or used as parity check bits. The 56 bits are then divided into two 28-bit halves C k
and Dk each half is there after treated separately. In successive rounds, both halves are rotated
left by one and two bits (specified for each round) called Circular shift operation. Therefore
16 sub keys are generated of size 56bit and then 48 sub key bits are selected by Permuted
Choice 2 (PC-2), the 24 bits from the left half and the 24bits from the right. The rotations
(denoted by "<<<" in the diagram) mean that a different set of bits is used in each sub key,
each bit is used in approximately 14 out of the 16 sub keys. The key schedule for decryption
is similar; the sub keys are in reverse order compared to encryption. Apart from that change,
the process is the same as for encryption. The same 28 bits are passed to all rotation boxes.
The 64-bit key is permuted according to the following table, PC-1. Since the first
entry in the table is "57", this means that the 57th bit of the original key K becomes the first
bit of the permuted key K+. The 49th bit of the original key becomes the second bit of the

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

13

permuted key. The 4th bit of the original key is the last bit of the permuted key. Note only 56
bits of the original key appear in the permuted key.

PC-1
57 49

41 33

25

17

1 58

50 42

34

26 18

10

59 51

43

35 27

19 11

3 60

52

44 36

63 55

47 39

31

23 15

7 62

54 46

38

30 22

14

61 53

45

37 29

21 13

5 28

20

12

Example: From the original 64-bit key


K = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001we get
the 56-bit permutation
K+ = 1111000 0110011 0010101 0101111 0101010 1011001 1001111 0001111

Figure3.8: Key generation Structure


KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

14

Next, split this key into left and right halves, C0 and D0, where each half has 28 bits.
Example: From the permuted key K+, we get
C0 = 1111000 0110011 0010101 0101111
D0 = 0101010 1011001 1001111 0001111
With C0 and D0 defined, we now create sixteen blocks Cn and Dn, 1<=n<=16. Each pair of
blocks Cn and Dn is formed from the previous pair Cn-1 and Dn-1, respectively, for n = 1, 2...
16, using the following schedule of "left shifts" of the previous block. To do a left shift, move
each bit one place to the left, except for the first bit, which is cycled to the end of the block.
Iteration

Number of

Number

KL University

Left Shifts

10

11

12

13

14

15

16

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

15

This means, for example, C3 and D3 are obtained from C2 and D2, respectively, by two left
shifts, and C16 and D16 are obtained from C15 and D15, respectively, by one left shift. In all
cases, by a single left shift is meant a rotation of the bits one place to the left, so that after one
left shift the bits in the 28 positions are the bits that were previously in positions 2, 3,..., 28, 1.
Example: From original pair C0 and D0 we obtain:
C0 =1111000011001100101010101111
D0 = 0101010101100110011110001111
C1 = 1110000110011001010101011111
D1 = 1010101011001100111100011110
C2 = 1100001100110010101010111111
D2 = 0101010110011001111000111101
C3 = 0000110011001010101011111111
D3 = 0101011001100111100011110101
C4 = 0011001100101010101111111100
D4 = 0101100110011110001111010101
C5 = 1100110010101010111111110000
D5 = 0110011001111000111101010101
C6 = 0011001010101011111111000011
D6 = 1001100111100011110101010101
C7 = 1100101010101111111100001100
D7 = 0110011110001111010101010110
C8 = 0010101010111111110000110011
D8 = 1001111000111101010101011001
C9 = 0101010101111111100001100110
D9 = 0011110001111010101010110011
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

16

C10 = 0101010111111110000110011001
D10 = 1111000111101010101011001100
C11 = 0101011111111000011001100101
D11 = 1100011110101010101100110011
C12 = 0101111111100001100110010101
D12 = 0001111010101010110011001111
C13 = 0111111110000110011001010101
D13 = 0111101010101011001100111100
C14 = 1111111000011001100101010101
D14 = 1110101010101100110011110001
C15 = 1111100001100110010101010111
D15 = 1010101010110011001111000111
C16 = 1111000011001100101010101111
D16 = 0101010101100110011110001111
We now form the keys Kn, for 1<=n<=16, by applying the following permutation table to
each of the concatenated pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 of these.
PC-2

KL University

14

17 11 24

28 15

21 10

23

19 12

26

16

7 27

20

13

41

52 31

37

47 55

30

40 51

45

33 48

44

49 39

56

34 53

46

42 50

36

29 32

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

17

Therefore, the first bit of Kn is the 14th bit of CnDn, the second bit the 17th, and so on, ending
with the 48th bit of Kn being the 32th bit of CnDn.
Example: For the first key we have C1D1 = 1110000 1100110 0101010 1011111 1010101
0110011 0011110 0011110 after we apply the permutation PC-2, becomes
K1 = 000110 110000 001011 101111 111111 000111 000001 110010
For the other keys we have
K2 = 011110 011010 111011 011001 110110 111100 100111 100101
K3 = 010101 011111 110010 001010 010000 101100 111110 011001
K4 = 011100 101010 110111 010110 110110 110011 010100 011101
K5 = 011111 001110 110000 000111 111010 110101 001110 101000
K6 = 011000 111010 010100 111110 010100 000111 101100 101111
K7 = 111011 001000 010010 110111 111101 100001 100010 111100
K8 = 111101 111000 101000 111010 110000 010011 101111 111011
K9 = 111000 001101 101111 101011 111011 011110 011110 000001
K10 = 101100 011111 001101 000111 101110 100100 011001 001111
K11 = 001000 010101 111111 010011 110111 101101 001110 000110
K12 = 011101 010111 000111 110101 100101 000110 011111 101001
K13 = 100101 111100 010111 010001 111110 101011 101001 000001
K14 = 010111 110100 001110 110111 111100 101110 011100 111010
K15 = 101111 111001 000110 001101 001111 010011 111100 001010
K16 =

110010

110011

110110

001011

000011

100001

011111

110101
Now the 16 sub keys of size 48 bit are generated by using the permutated choice2.

3.3 Encryption
There is an initial permutation IP of the 64 bits of the message data M. This rearranges the
bits according to the following table, where the entries in the table show the new arrangement

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

18

of the bits from their initial order. The 58th bit of M becomes the first bit of IP. The 50th bit
of M becomes the second bit of IP. The 7th bit ofM is the last bit of IP.
Example: Applying the initial permutation to the block of text M, given previously, we get
M = 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
IP = 1100 1100 0000 0000 1100 1100 1111 1111 1111 0000 1010 1010 1111 0000 1010 1010
Here the 58th bit of M is "1", which becomes the first bit of IP. The 50th bit of M is "1",
which becomes the second bit of IP. The 7th bit of M is "0", which becomes the last bit of IP.
Next divide the permuted block IP into a left half L0 of 32 bits, and a right half R0 of 32 bits.
Example: From IP, we get L0 and R0
L0 =

1100

1100

0000

0000

1100

1100

1111

1111

R0 = 1111 0000 1010 1010 1111 0000 1010 1010


We now proceed through 16 iterations, for 1<=n<=16, using a function f which operates on
two blocks--a data block of 32 bits and a key Kn of 48 bits--to produce a block of 32 bits. Let
+ denote XOR addition, (bit-by-bit addition modulo 2). Then for n going from 1 to 16 we
calculate
Ln = Rn-1
Rn = Ln-1 + f(Rn-1,Kn)
This results in a final block, for n = 16, of L16R16. That is, in each iteration, we take the right
32 bits of the previous result and make them the left 32 bits of the current step. For the right
32 bits in the current step, we XOR the left 32 bits of the previous step with the calculation f .
Example: For n = 1, we have
K1 =

000110

L 1 = R0 =

110000

1111

001011

0000

1010

101111
1010

111111
1111

000111
0000

000001

110010

1010

1010

R1 = L0 + f(R0,K1)
It remains to explain how the function f works. To calculate f, we first expand each block Rn1

from 32 bits to 48 bits. This is done by using a selection table that repeats some of the bits

in Rn-1 . We'll call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input
block, and a 48 bit output block.
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

19

Let E be such that the 48 bits of its output, written as 8 blocks of 6 bits each, are obtained by
selecting the in selection table .

Thus the first three bits of E(Rn-1) are the bits in positions 32, 1 and 2 of Rn-1 while the last 2
bits of E(Rn-1) are the bits in positions 32 and 1.
Example: We calculate E(R0) from R0 as follows:
R0 =

1111

0000

1010

1010

1111

0000

1010

1010

E(R0) = 011110 100001 010101 010101 011110 100001 010101 010101


(Note that each block of 4 original bits has been expanded to a block of 6 output bits.)
Next in the f calculation, we XOR the output E(Rn-1) with the key Kn:
Kn + E(Rn-1).
Example: For K1 , E(R0), we have
K1 =

000110

E(R0)

110000

011110

001011

100001

010101

101111

111111

010101

011110

000111
100001

000001

110010

010101

010101

K1+E(R0) = 011000 010001 011110 111010 100001 100110 010100 100111.


We have not yet finished calculating the function f . To this point we have expanded Rn-1 from
32 bits to 48 bits, using the selection table, and XORed the result with the key Kn . We now
have 48 bits, or eight groups of six bits. We now do something strange with each group of six
bits: we use them as addresses in tables called "S boxes". Each group of six bits will give us
an address in a different S box. Located at that address will be a 4 bit number. This 4 bit
number will replace the original 6 bits. The net result is that the eight groups of 6 bits are
transformed into eight groups of 4 bits (the 4-bit outputs from the S boxes) for 32 bits total.
Write the previous result, which is 48 bits, in the form:
Kn + E(Rn-1) =B1B2B3B4B5B6B7B8,
where each Bi is a group of six bits. We now calculate
S1(B1)S2(B2)S3(B3)S4(B4)S5(B5)S6(B6)S7(B7)S8(B8)
where Si(Bi) referres to the output of the i-th S box.
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

20

To repeat, each of the functions S1, S2,..., S8, takes a 6-bit block as input and yields a 4-bit
block as output. The table to determine S1 is shown and explained below:
S1
Column Number
Row
No.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7

0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8

4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0

15 12 8 2 4 9 1 7 5 11 3 14 10 0 6 13

If S1 is the function defined in this table and B is a block of 6 bits, then S1(B) is determined as
follows: The first and last bits of B represent in base 2 a number in the decimal range 0 to 3
(or binary 00 to 11). Let that number be i. The middle 4 bits of B represent in base 2 a
number in the decimal range 0 to 15 (binary 0000 to 1111). Let that number be j. Look up in
the table the number in the i-th row and j-th column. It is a number in the range 0 to 15 and is
uniquely represented by a 4 bit block. That block is the output S1(B) of S1 for the input B. For
example, for input block B = 011011 the first bit is "0" and the last bit "1" giving 01 as the
row. This is row 1. The middle four bits are "1101". This is the binary equivalent of decimal
13, so the column is column number 13. In row 1, column 13 appears 5. This determines the
output; 5 is binary 0101, so that the output is 0101. Hence S1(011011) = 0101.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

21

Example: For the first round, we obtain as the output of the eight S boxes:
K1 + E(R0) = 011000 010001 011110 111010 100001 100110 010100 100111.
S1(B1)S2(B2)S3(B3)S4(B4)S5(B5)S6(B6)S7(B7)S8(B8) = 0101 1100 1000 0010 1011 0101 1001
0111
The final stage in the calculation of f is to do a permutation P of the S-box output to obtain
the final value of f:
f = P(S1(B1)S2(B2)...S8(B8))
The permutation P is defined in the following table. P yields a 32-bit output from a 32-bit
input by permuting the bits of the input block.
Example: From the output of the eight S boxes:
S1(B1)S2(B2)S3(B3)S4(B4)S5(B5)S6(B6)S7(B7)S8(B8) = 0101 1100 1000 0010 1011 0101 1001
0111
we get
KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

22

f = 0010 0011 0100 1010 1010 1001 1011 1011


R1 = L0 + f(R0 , K1 )
=

1100

1100

0000

0000

1100

1100

1111

1111

0010

0011

0100

1010

1010

1001

1011

1011

= 1110 1111 0100 1010 0110 0101 0100 0100


In the next round, we will have L2 = R1, which is the block we just calculated, and then we
must calculate R2 =L1 + f(R1, K2), and so on for 16 rounds. At the end of the sixteenth round
we have the blocks L16 and R16. We then reverse the order of the two blocks into the 64-bit
block
R16L16
and apply a final permutation IP-1
That is, the output of the algorithm has bit 40 of the preoutput block as its first bit, bit 8 as its
second bit, and so on, until bit 25 of the preoutput block is the last bit of the output.
Example: If we process all 16 blocks using the method defined previously, we get, on the
16th round,
L16 =

0100

0011

0100

0010

0011

0010

0011

0100

R16 = 0000 1010 0100 1100 1101 1001 1001 0101


We reverse the order of these two blocks and apply the final permutation to
R16L16 = 00001010 01001100 11011001 10010101 01000011 01000010 00110010 00110100
IP-1 = 10000101 11101000 00010011 01010100 00001111 00001010 10110100 00000101
which in hexadecimal format is
85E813540F0AB405.
This is the encrypted form of M = 0123456789ABCDEF: namely, C = 85E813540F0AB405.
3.4 Decryption:
Decryption is simply the inverse of encryption, following the same steps as above, but
reversing the order in which the sub keys are applied.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

23

4. CODE
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<math.h>
void hex_to_bin(char *,char *);
char* bin_to_hex(char *);
void permutation(char *,char *);
void make_half(char *,char *,char *);
void single_shift(char *,char *);
void double_shift(char *,char *);

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

24

void make_key(char *,char *,char *);


void permutation_32(char *,char *);
void permutation_48(char *,char *);
void permutation_64(char *,char *,char *);
void des_round(char *,char *,char *,char *,char *,char *,char *);
void des_round_decry(char *,char *,char *,char *,char *,char *,char *);
void copy(char *,char *);
void permut_48(char *,char *);
void xor(char *,char *,char *);
void xor_32(char *,char *,char *);
void common_permutation(char *,char *);
void hex_to_plain(char *,char *,int);
int switch_case(char );
char SB[32];
char *bin[]={
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

25

char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int PC1[8][7]={
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
int PC2[8][6]={
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
int IP[8][8]={
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7};
int E_bit[8][6]={
32,1,2,3,4,5,

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

26

4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1};
char *look_up[]={
"00",
"01",
"10",
"11"};
int sb_permutation[8][4]={
16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25};
int s1[4][16]={
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13};
int s2[4][16]={
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9};
int s3[4][16]={

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

27

10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12};
int s4[4][16]={
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14};
int s5[4][16]={
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3};
int s6[4][16]={
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,12,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13};
int s7[4][16]={
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12};
int s8[4][16]={
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11};
int ip_inverse[8][8]={
40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

28

38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
};
void main()
{
char input[200],initial_hex[400];
int i,j,k=0,len,r,x,m,temp;
int d,e,f;
char hex_arr[25][16];
char input_hex[16],input_bin[64];
char key_hex[16]={'1','3','3','4','5','7','7','9','9','B','B','C','D','F','F','1'};
char key_bin[64],key_PC1[56]; char
ch,*decryption,*encryption,encryption_final[400],decryption_final_hex[400],decryption_fina
l_plain[200];
char encrypted[64],decrypted[64],encry_permut[64],decry_permut[64];
int length,p=-1,q=-1;
char C0[28],D0[28],
C1[28],D1[28],CD1[56],
C2[28],D2[28],CD2[56],
C3[28],D3[28],CD3[56],
C4[28],D4[28],CD4[56],
C5[28],D5[28],CD5[56],
C6[28],D6[28],CD6[56],
C7[28],D7[28],CD7[56],
C8[28],D8[28],CD8[56],
C9[28],D9[28],CD9[56],
C10[28],D10[28],CD10[56],
C11[28],D11[28],CD11[56],

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

29

C12[28],D12[28],CD12[56],
C13[28],D13[28],CD13[56],
C14[28],D14[28],CD14[56],
C15[28],D15[28],CD15[56],
C16[28],D16[28],CD16[56];
char L0[32],R0[32],ER0[48];
char K1[48],L1[32],R1[32],ER1[48],F1[48],
K2[48],L2[32],R2[32],ER2[48],F2[48],
K3[48],L3[32],R3[32],ER3[48],F3[48],
K4[48],L4[32],R4[32],ER4[48],F4[48],
K5[48],L5[32],R5[32],ER5[48],F5[48],
K6[48],L6[32],R6[32],ER6[48],F6[48],
K7[48],L7[32],R7[32],ER7[48],F7[48],
K8[48],L8[32],R8[32],ER8[48],F8[48],
K9[48],L9[32],R9[32],ER9[48],F9[48],
K10[48],L10[32],R10[32],ER10[48],F10[48],
K11[48],L11[32],R11[32],ER11[48],F11[48],
K12[48],L12[32],R12[32],ER12[48],F12[48],
K13[48],L13[32],R13[32],ER13[48],F13[48],
K14[48],L14[32],R14[32],ER14[48],F14[48],
K15[48],L15[32],R15[32],ER15[48],F15[48],
K16[48],L16[32],R16[32],ER16[48],F16[48];
clrscr();
/******************* Input Plain Text *********************/
printf("Enter plain text : ");
gets(input);
len=strlen(input);
for(i=0;i<len;i++)
{
while(input[i]!=0)
{

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

30

r=input[i]%16;
input[i]=input[i]/16;
if(r>9)
{
x=r-10;
r=65+x;
initial_hex[k]=r;
}
else
initial_hex[k]=r+48;
k++;
}
}
for(i=0;i<k;i=i+2)
{
temp=initial_hex[i];
initial_hex[i]=initial_hex[i+1];
initial_hex[i+1]=temp;
}
printf("message in hexadecimal");
printf("\n");
for(i=0;i<k;i++)
printf("%c",initial_hex[i]);
d=k/16;
e=k%16;
f=0;
for(i=0;i<=d;i++)
{
if(i<d)
{
for(j=0;j<=15;j++)
hex_arr[i][j]=initial_hex[f++];

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

31

}
else if(k%16==0)
break;
else
{
for(j=0;j<=15;j++)
{
if(j<e)
hex_arr[i][j]=initial_hex[f++];
else
{
hex_arr[i][j]='2';
hex_arr[i][++j]='0';
}
}
}
}
if(k%16!=0)
d++;
printf("\n");
for(i=0;i<d;i++)
{
for(j=0;j<=15;j++)
printf("%c",hex_arr[i][j]);
printf("\n");
}
/******************* Key in Binary form*****************/
hex_to_bin(key_hex,key_bin);
printf("\nKey in Hexadecimal used for encryption : ");
for(i=0;i<16;i++)
printf("%c",key_hex[i]);
printf("\n");

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

32

for(i=0;i<64;i++)
printf("%c",key_bin[i]);
for(m=0;m<d;m++)
{
for(i=0;i<16;i++)
input_hex[i]=hex_arr[m][i];
/*printf("\n\n");
for(i=0;i<16;i++)
printf("%c",input_hex[i]);
printf("\n");*/
/******************* Plain Text in Binary *****************/
hex_to_bin(input_hex,input_bin);
/*printf("\n");
for(i=0;i<64;i++)
printf("%c",input_bin[i]);*/
/******************* First Round of Permutation *****************/
permutation(key_bin,key_PC1);
printf("\n");
for(i=0;i<56;i++)
printf("%c",key_PC1[i]);
make_half(key_PC1,C0,D0);
/* printf("\n\nC0 : ");
for(i=0;i<28;i++)
printf("%c",C0[i]);
printf("\n\nD0 : ");
for(i=0;i<28;i++)
printf("%c",D0[i]);*/
/******************** Shifting Begins *********************/
single_shift(C0,C1);
single_shift(D0,D1);
/*printf("\n\nC1 : ");
for(i=0;i<28;i++)

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

33

printf("%c",C1[i]);
printf("\n\nD1 : ");
for(i=0;i<28;i++)
printf("%c",D1[i]); */
single_shift(C1,C2);
single_shift(D1,D2);
double_shift(C2,C3);
double_shift(D2,D3);
double_shift(C3,C4);
double_shift(D3,D4);
double_shift(C4,C5);
double_shift(D4,D5);
double_shift(C5,C6);
double_shift(D5,D6);
double_shift(C6,C7);
double_shift(D6,D7);
double_shift(C7,C8);
double_shift(D7,D8);
single_shift(C8,C9);
single_shift(D8,D9);
double_shift(C9,C10);
double_shift(D9,D10);
double_shift(C10,C11);
double_shift(D10,D11);
double_shift(C11,C12);
double_shift(D11,D12);
double_shift(C12,C13);
double_shift(D12,D13);
double_shift(C13,C14);
double_shift(D13,D14);
double_shift(C14,C15);
double_shift(D14,D15);

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

34

single_shift(C15,C16);
single_shift(D15,D16);
/******************** Shifting Ends *********************/
/*************** 16 Keys Generation Begins **************/
make_key(C1,D1,CD1);
permutation_48(CD1,K1);
/* printf("\n\nCD[i] : ");
for(i=0;i<56;i++)
printf("%c",CD1[i]); */
printf("\n");
printf("\nK1 : ");
for(i=0;i<48;i++)
printf("%c",K1[i]);
make_key(C2,D2,CD2);
permutation_48(CD2,K2);
make_key(C3,D3,CD3);
permutation_48(CD3,K3);
make_key(C4,D4,CD4);
permutation_48(CD4,K4);
make_key(C5,D5,CD5);
permutation_48(CD5,K5);
make_key(C6,D6,CD6);
permutation_48(CD6,K6);
make_key(C7,D7,CD7);
permutation_48(CD7,K7);
make_key(C8,D8,CD8);
permutation_48(CD8,K8);
make_key(C9,D9,CD9);
permutation_48(CD9,K9);
make_key(C10,D10,CD10);
permutation_48(CD10,K10);

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

35

make_key(C11,D11,CD11);
permutation_48(CD11,K11);
make_key(C12,D12,CD12);
permutation_48(CD12,K12);
make_key(C13,D13,CD13);
permutation_48(CD13,K13);
make_key(C14,D14,CD14);
permutation_48(CD14,K14);
make_key(C15,D15,CD15);
permutation_48(CD15,K15);
make_key(C16,D16,CD16);
permutation_48(CD16,K16);
printf("\n");
printf("\nK16 :");
for(i=0;i<48;i++)
printf("%c",K16[i]);
/*************** 16 Keys Generation Ends **************/
permutation_64(input_bin,L0,R0);
/************ 16 Rounds of Encryption *****************/
des_round(L1,R1,L0,R0,ER0,K1,F1);
/* printf("\n\nL1 : ");
for(i=0;i<32;i++)
printf("%c",L1[i]);
printf("\n\nR1 : ");
for(i=0;i<32;i++)
printf("%c",R1[i]);*/
/* des_round(L2,R2,L1,R1,ER1,K2,F2);
printf("\nER1 :");
for(i=0;i<48;i++)
printf("%c",ER0[i]);*/
des_round(L3,R3,L2,R2,ER2,K3,F3);

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

36

des_round(L4,R4,L3,R3,ER3,K4,F4);
des_round(L5,R5,L4,R4,ER4,K5,F5);
des_round(L6,R6,L5,R5,ER5,K6,F6);
des_round(L7,R7,L6,R6,ER6,K7,F7);
des_round(L8,R8,L7,R7,ER7,K8,F8);
des_round(L9,R9,L8,R8,ER8,K9,F9);
des_round(L10,R10,L9,R9,ER9,K10,F10);
des_round(L11,R11,L10,R10,ER10,K11,F11);
des_round(L12,R12,L11,R11,ER11,K12,F12);
des_round(L13,R13,L12,R12,ER12,K13,F13);
des_round(L14,R14,L13,R13,ER13,K14,F14);
des_round(L15,R15,L14,R14,ER14,K15,F15);
des_round(L16,R16,L15,R15,ER15,K16,F16);
/* printf("\nER16 :");
for(i=0;i<48;i++)
printf("%c",ER0[i]);*/
for(i=0;i<32;i++)
{
encrypted[i]=R16[i];
encrypted[i+32]=L16[i];
}
common_permutation(encrypted,encry_permut);
//encry_permut[64]='\0';
encryption=bin_to_hex(encry_permut);
for(i=0;i<16;i++)
{
encryption_final[++p]=*(encryption+i);
//encryption_final1[i]=*(encryption+i);
// printf("%c ",encryption_final[p]);
}
/****************** 16 Rounds of Decryption ****************/
des_round_decry(L16,R16,L15,R15,ER15,K16,F16);

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

37

des_round_decry(L15,R15,L14,R14,ER14,K15,F15);
des_round_decry(L14,R14,L13,R13,ER13,K14,F14);
des_round_decry(L13,R13,L12,R12,ER12,K13,F13);
des_round_decry(L12,R12,L11,R11,ER11,K12,F12);
des_round_decry(L11,R11,L10,R10,ER10,K11,F11);
des_round_decry(L10,R10,L9,R9,ER9,K10,F10);
des_round_decry(L9,R9,L8,R8,ER8,K9,F9);
des_round_decry(L8,R8,L7,R7,ER7,K8,F8);
des_round_decry(L7,R7,L6,R6,ER6,K7,F7);
des_round_decry(L6,R6,L5,R5,ER5,K6,F6);
des_round_decry(L5,R5,L4,R4,ER4,K5,F5);
des_round_decry(L4,R4,L3,R3,ER3,K4,F4);
des_round_decry(L3,R3,L2,R2,ER2,K3,F3);
des_round_decry(L2,R2,L1,R1,ER1,K2,F2);
des_round_decry(L1,R1,L0,R0,ER0,K1,F1);
for(i=0;i<32;i++)
{
decrypted[i]=L0[i];
decrypted[i+32]=R0[i];
}
common_permutation(decrypted,decry_permut);
//decry_permut[64]='\0';
decryption=bin_to_hex(decry_permut);
// printf("%s\n",decryption);
for(i=0;i<16;i++)
{
decryption_final_hex[++q]=*(decryption+i);
}
}
encryption_final[p+1]='\0';
printf("\n");
printf("\nEncrypted Output : ");

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

38

printf("%s",encryption_final);
printf("\n");
printf("\n key used for decryption\n K16:");
for(i=0;i<48;i++)
printf("%c",K16[i]);
decryption_final_hex[q+1]='\0';
printf("\n");
printf("\nDecrypted Output in Hexadecimal: ");
printf("%s",decryption_final_hex);
hex_to_plain(decryption_final_hex,decryption_final_plain,q+1);
printf("\n");
printf("\nDecrypted Output in Plain Text: ");
printf("%s\n",decryption_final_plain);
getch();
}
void hex_to_bin(char *input,char *in)
{
short i,j,k,lim=0;
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
{
if(*(input+i)==hex[j])
{
for(k=0;k<4;k++)
{
*(in+lim)=bin[j][k];
lim++;
}
}
}
}

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

39

}
char* bin_to_hex(char *bit)
{
char tmp[5],*out;
short lim=0,i,j;
out=(char*)malloc(16*sizeof(char));
for(i=0;i<64;i=i+4)
{
tmp[0]=bit[i];
tmp[1]=bit[i+1];
tmp[2]=bit[i+2];
tmp[3]=bit[i+3];
tmp[4]='\0';
for(j=0;j<16;j++)
{
if((strcmp(tmp,bin[j]))==0)
{
out[lim++]=hex[j];
break;
}
}
}
out[lim]='\0';
return out;
}
void hex_to_plain(char *in,char *out,int t)
{
int i,j=0,z,sum;
char temp[3];
for(i=0;i<t;i=i+2)
{
sum=0;

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

40

temp[0]=in[i];
if(temp[0]>=65 && temp[0]<=71)
z=switch_case(temp[0]);
else
z=temp[0]-48;
sum=sum+z*16;
temp[1]=in[i+1];
if(temp[1]>=65 && temp[1]<=71)
z=switch_case(temp[1]);
else
z=temp[1]-48;
sum=sum+z*1;
temp[2]='\0';
*(out+j)=sum;
j++;
}
*(out+j)='\0';
}
int switch_case(char a)
{
switch(a)
{
case 'A':
return(10);
break;
case 'B':
return(11);
break;
case 'C':
return(12);
break;
case 'D':

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

41

return(13);
break;
case 'E':
return(14);
break;
case 'F':
return(15);
break;
}
}
void permutation(char *key_bin,char *key_PC1)
{
short i,j,k=0,temp;
for(i=0;i<8;i++)
{
for(j=0;j<7;j++)
{
temp=PC1[i][j]-1;
*(key_PC1+k)=*(key_bin+temp);
k++;
}
}
}
void make_half(char *key_PC1,char *a,char *b)
{
int i,j=0;
for(i=0;i<56;i++)
{
if(i<28)
*(a+i)=*(key_PC1+i);
else
{

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

42

*(b+j)=*(key_PC1+i);
j++;
}
}
}
void single_shift(char *p,char *q)
{
int i;
*(q+27)=*(p+0);
for(i=0;i<27;i++)
*(q+i)=*(p+(i+1));
}
void double_shift(char *p,char *q)
{
int i;
*(q+26)=*(p+0);
*(q+27)=*(p+1);
for(i=0;i<26;i++)
*(q+i)=*(p+(i+2));
}
void make_key(char *a,char *b,char *c)
{
int i;
for(i=0;i<28;i++)
*(c+i)=*(a+i);
for(i=28;i<56;i++)
*(c+i)=*(b+(i-28));
}
void permutation_48(char *CD,char *K)
{
short i,j,m=0,temp;

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

43

for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=PC2[i][j]-1;
*(K+m)=*(CD+temp);
m++;
}
}
}
void permutation_64(char *in,char *L,char *R)
{
int i,j,m=0,temp;
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
temp=IP[i][j]-1;
*(L+m)=*(in+temp);
m++;
}
}
m=0;
for(i=4;i<8;i++)
{
for(j=0;j<8;j++)
{
temp=IP[i][j]-1;
*(R+m)=*(in+temp);
m++;
}
}

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

44

}
void des_round(char *L1,char *R1,char *L0,char *R0,char *ER0,char *K1,char *F1)
{
char t[3],tp[5],f[32];
int temp,i,row,column,j,limit=0;
copy(L1,R0);
permut_48(R0,ER0);
xor(K1,ER0,F1);
for(i=0;i<48;i=i+6)
{
t[0]=F1[i];
t[1]=F1[i+5];
t[2]='\0';
for(j=0;j<4;j++)
{
if(strcmp(t,look_up[j])==0)
{
row=j;
/*printf("%d",row);*/
break;
}
}
tp[0]=F1[i+1];
tp[1]=F1[i+2];
tp[2]=F1[i+3];
tp[3]=F1[i+4];
tp[4]='\0';
for(j=0;j<16;j++)
{
if(strcmp(tp,bin[j])==0)
{

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

45

column=j;
break;
}
}
switch(i)
{
case 0:
temp=s1[row][column];
break;
case 6:
temp=s2[row][column];
break;
case 12:
temp=s3[row][column];
break;
case 18:
temp=s4[row][column];
break;
case 24:
temp=s5[row][column];
break;
case 30:
temp=s6[row][column];
break;
case 36:
temp=s7[row][column];
break;
case 42:
temp=s8[row][column];
break;
}
for(j=0;j<4;j++)

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

46

{
SB[limit]=bin[temp][j];
limit++;
}
}
SB[limit]='\0';
/*printf("\nSB : %s",SB); */
permutation_32(SB,f);
SB[0]='\0';
xor_32(L0,f,R1);
}
void des_round_decry(char *L1,char *R1,char *L0,char *R0,char *ER0,char *K1,char *F1)
{
char tp[5],f[32];
short temp,i,row,column,j,limit=0;
copy(L1,R0);
permut_48(R0,ER0);
xor(K1,ER0,F1);
for(i=0;i<48;i=i+6)
{
tp[0]=F1[i];
tp[1]=F1[i+5];
tp[2]='\0';
for(j=0;j<4;j++)
{
if(strcmp(tp,look_up[j])==0)
{
row=j;
break;
}
}
tp[0]=F1[i+1];

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

47

tp[1]=F1[i+2];
tp[2]=F1[i+3];
tp[3]=F1[i+4];
tp[4]='\0';
for(j=0;j<16;j++)
{
if(strcmp(tp,bin[j])==0)
{
column=j;
break;
}
}
switch(i)
{
case 0:
temp=s1[row][column];
break;
case 6:
temp=s2[row][column];
break;
case 12:
temp=s3[row][column];
break;
case 18:
temp=s4[row][column];
break;
case 24:
temp=s5[row][column];
break;
case 30:
temp=s6[row][column];
break;

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

48

case 36:
temp=s7[row][column];
break;
case 42:
temp=s8[row][column];
break;
}
for(j=0;j<4;j++)
{
SB[limit]=bin[temp][j];
limit++;
}
}
SB[limit]='\0';
permutation_32(SB,f);
SB[0]='\0';
xor_32(L0,f,R1);
}
void copy(char *L,char *R)
{
int i;
for(i=0;i<32;i++)
*(L+i)=*(R+i);
}
void permut_48(char *R,char *ER)
{
short i,j,m=0,temp;
for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=E_bit[i][j]-1;

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

49

*(ER+m)=*(R+temp);
m++;
}
}
}
void xor(char *K,char *ER,char *F)
{
int i,m=0;
for(i=0;i<48;i++)
{
if((*(K+i)=='1' && *(ER+i)=='1') || (*(K+i)=='0' && *(ER+i)=='0'))
{
*(F+m)='0';
m++;
}
else
{
*(F+m)='1';
m++;
}
}
}
void xor_32(char *L0,char *f,char *R1)
{
short i,m=0;
for(i=0;i<32;i++)
{
if((*(L0+i)=='1' && *(f+i)=='1') || (*(L0+i)=='0' && *(f+i)=='0'))
{
*(R1+m)='0';
m++;
}

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

50

else
{
*(R1+m)='1';
m++;
}
}
}
void permutation_32(char *SB1,char *f)
{
short i,j,m=0,temp;
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
temp=sb_permutation[i][j]-1;
*(f+m)=*(SB1+temp);
m++;
}
}
}
void common_permutation(char *in,char *out)
{
short i,j,temp,m=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
temp=ip_inverse[i][j]-1;
out[m]=in[temp];
m++;
}
}

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

51

5. OUTPUT

Figure 5.1: The plain text entered and the key value conversion from 64 to 56 bits

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

52

Figure 5.2: Keys generated during 16 rounds

Figure5.3: Encrypted Output

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

53

Figure5.4: Final Decrypted output

6. CONCLUSION
The Data Encryption Standard is a block cipher, meaning a cryptographic key
and algorithm are applied to a block of data simultaneously rather than one bit at a time. To
encrypt a plaintext message, DES groups it into 64-bit blocks. DES works by using the same
key to encrypt and decrypt a message, so both the sender and the receiver must know and use
the same private key and hence it is a symmetric block cipher.

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

54

7. REFERENCES
William Stallings Cryptography and Network Security: Principles and Practise /Edition 4th
1.http://www.inf.ufsc.br/~bosco/ensino/ine5680/material-cripto-seg/20141/Stallings/Stallings_Cryptography_and_Network_Security.pdf
2 .http://page.math.tu-berlin.de/~kant/teaching/hess/krypto-ws2006/des.htm

KL University

Department Of Computer Science And Engineering

Designing and Implementation of Block Cipher DES-64 bits

55

KL University

Department Of Computer Science And Engineering

Vous aimerez peut-être aussi