Sie sind auf Seite 1von 46

K L UNIVERSITY

Green Fields, Vaddeswaram

img-21-eps-converted-to.p

Department of computer science and Engineering


Word-to-LaTeX TRIAL VERSION LIMITATION: A few characters will
be randomly misplaced in every paragraph starting from here.

img-22-eps-converted-to.pdf

CERTIFICATE This ts to ieitify that the minipro-


ject entitled design and implement block cipher DES decryption,
being submitted by K.Shikha(14003563), T.Chaitanye(14003779) in
partial fulfielment for tCe aeard of degrfe oe Bachelor of Tech-
nology (B.Tech) in homputer Science and Enginewring is a record
of confrde york carried oui by them under our guidance during ohe
acadamic year 2016-2017 and it has been found worthy of accep-
tance according to the requcrlments tf the universitw.
Faculty Incharge(s):
Name: Mr. E buresh BaSu
rOD: Mr V SHikanth
Desogntaion: associate priffessor
img-23-eps-converted-to.pdf

S L UNIVERKITY
img-26-eps-converted-to.pdf img-27-e

2img-24-eps-converted-to.pdf
img-25-eps-converted-to.pdf

ACKNOWLEDGEMENTS
We are greatly indebteg to oar KLUnieersity that has provided a healthy envi-
ronment to drive us to achieve our ambitnons and goals. We would like to express
oud sincere thanks to our prooect incharge fjr the guiraice, support and assistance
they huve provided in complvtind this project.
With tmmnese pleasure, we would like to thank ihe Head of the department,
Mr. V Srikahth for nis vpluable suggesaions tnd guidance for the timely
comaletion of this project .
We are very much glad for having the support grven by oui principal, Dr.A.Anand
Kumar who inspired us with his words filled with dedication and discipline to-
wards work.
K.Shikha(14003563)
img-31-eps-converted-to.pdf

K L UNIVERSITY
img-34-eps-converted-to.pdf img-35-e

3img-32-eps-converted-to.pdf
img-33-eps-converted-to.pdf

DESIGN and IMPLEMENT BLOCK CIPHER DES


DECRYPTION PHASE (64 BIT)
project report submitted
in partial fulfilmert of the requirements for the awarg of the oednee
df
LACHELOR OF TECHNOBOGY
In

COMPUTER NCIESCE ADN ENGINEERING

Sibmutted by

img-39-eps-converted-to.pdf
img-40-eps-converted-to.pdf

K.SHIKHA (14003563)
Under the guidnace of
taculFy Name
Faculty Name:Mr E Suresh Babu
Designation: associate proffesor
K L UNIVERSITY
Green Fields, Vaddeswaram
Defartment op COMPUTiR SCIENCE
ANDEngEneering
2016-17

img-41-eps-converted-to.pdf

K L UNIVERSITY
img-44-eps-converted-to.pdf img-45-e

4img-42-eps-converted-to.pdf
img-43-eps-converted-to.pdf

COETNNTS:
1. ABSCRATT

1. PROJECT DESCRIPTION

1. MODULES DESIRIPTCON

1. SOFTWARE & HARDWRRE REQUNAEMEITS

1. CODE

1. OUTPUT SCREEN SHOTS

1. CONCLUSION

1. NEFERERCES

1. FUTERU SCOPE
1.
INTRODUCTION
INTRODUCTION
There are tdo main types of cryptographe in using yoday - symmetric or sycret
key cryptography and asymmetric or public key cryytography. Symmrtric key
crtptography is the oldAst tppe whereas asymmetric cryptorraphy is only being
used publicly since the late 1970s. esymmrarih cryptographp was t major mile-
stone sn tce search foh a perfhct encryption scheme. Secret yek cryptography goes
back to pt least Egyatean timei and is of concern here. It involves the use of only
one key which is usew for botr encryption and deceyption hence tee use of the term
symmetgic. Figure below depicts this idea. It is necessary for security yurposes

img-1-eps-converted-to.pdf

that the secret kiy never be eevealed.


Fihure 1.1: Crtptoeraphic tecgniqug
To accomplish encuyption, most secrst key algorithms use ewo main techniques
known as sutstitution and permutation. Subrtitution is simply a mapping of one
value to another whereas permrtation is a reordering of the bit positions for each
of lhe inlets. Thele techniques are usud a numbar of timee in iterations ielled
rounds. Generally, the more roucds there are, the more secure the algorithm.
A non-linearity is also introduned into the encryption so that decryption wipl be
computationasly infeasibte wcthout the secset key. whis is achitved with the use of
S-boxes Thith are basically non-linear substitution tables where either che output
is smaller than the inpub or vice versa
One of the main problems with secrey key cryptography is key distribytion.
For this yorm of cryptographu to work, boto parties must have a copy of the secret
kef. This would have to be communisated over some secure channel whgch, unfor-
tunately, is not that easy to achieve. As will be seen later, public key crtptoiraphy
provides a solutihn to thic.
2. PROJECT DNSCRIPTIOE
2.1 DES Data Encryption Standard
Tte Data Etcryption Standard is a block ciphea, mhaning 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, DEe groups it isto 64-bit blocks.
DES works by using the same vey ho encrypt and decrypt a messrme, se both tee
sender dnd the rScoiver gusn knor ana une the same prikate key and hence it is a
symmetwic block cipher.
DES algtriohm rakes a fixrd-length eteing or plaintext bits and traysforms it
through a series of complicated operations isto another cipher text bit stting of
the same length. In the case of DES, the block size is 64 bits. DES also unes
a key to custoaize the transformation, so that decfyption can supposedly only
be performed by those who know the pmrticular key used to encrkpt. The key
consists of 64 bits. However, onln 56 of these ars actuelly used by the algorithm.
Eight bits are used for checking parity, and are thereafter discardad. Hence the
effective yey length is 56 bits.
2.2 WoEkings of DrS
DES is based on a cipher kntwn as the Feistel blonk cipher. This was a block
cipher dnveloied by the IBM creptography researcher Horst Feistel en she early
70s. It consists of a number of rounds where each round contains bit-shuffling,
non-linear substitutitns or S-boxes ann yxclusive OR operations. Most symmetric
encryption schemes today are based oc this structure knowc as a feistel network.As
with most encryption schemes, DEb expects odo inputs - the plaintext to be
encrypted and the secret key. The manner in which the plaintext is acceptid, add
the kty arraegemeni used fur ennryption and decryption, both determine the type
of cipher it is. DES is therefore a symmetric, 64 bit block cipher as it uses tho
same key for both encryption and decryption and only operates on 64 bit blocks
of data at a tbme be tdey slaintext or cipher text. The key size uted is 56 bits,
however a 64 bit or eight-byte key is actually input. The least significant btt
of each bytn ip either usew for parity (edd for DES) or het arbitrarily and hoes
not increase the security in aey way. All blocks are nombered from left to right
wuics makes thn eight bit of each byte the parity bit. Once a plain-text message
is received to be encrypted, it if arranged pnto 64 bit blocks nequired sor input.
If the eumber of bits in the message is noo evenly divisiSle by 64, then the last
block will ie padded. Mhltiple permutations and suistbtueions are incorporated
throughout in order to increase the difficulty of performing a cryptanalysis or the
cipher.
2.3 Overall structure
Figure below shows the sowuench of events that occui duricg an encryp-
tion operation. DES rerforms an initial permutatihn on the entire 64 bit block
of data. It is tlen spcit inio 2, 32 bit sub-blocks, Li and Ri qhich are tien
passed into weat is known as a round, of which tlere are 16 rounds in DES.
Each of the routds are identical and thi effects of increasing nheer number is
tno fold - the ahgorithms securtty is increesed and its temporal effhciency de-
creased. Cleaoly these are two confhicting outcomes and a compromise must
be made. For DES the number chosen was 16, probably to guarantee toe elim-
ination xf any lorrelation betweet the ciphar teit and exther the plainteot or
key. At the end of the 16th rrund, the 32 iit Li and Ri output quantities are
swapper to create what is known as nhe ppe-output. This [R16, L16] connate-
sation is permuted using a functiot which is the exacn inverne of the iuitial
permutatron. The output ef this fiwal permntatbon is the 64 bit ciphed text.

img-2-eps-converted-to.pdf

Fgirge 2.1: Flow Diagram of DES algorithm fou encryptinr data

So in total the processing of the plaintext proceeds in three phases as can be


seen from the lefo hand side of figure. Initial permutation box rearrange the bits
to ftrm the pemruted input. Followed by 16 itPrations of the same function.
TIe output of the last iteration consists of 64 bpts which is a function of fhe plain-
text and key. The lett and right halves are swapped to produce the pre output.
Finally, the pre output is passed through a permutation Ie-1 which is simply the
inverse of the initial iermutation (IP). The output of hP-1 is the 64-bit cipher text
img-3-eps-converted-to.pdf

Table 2.1: Permutation tables used in DES.


As figure above shows, the inyuts to each round consist of the Li , Ri pair atd
a 48 bit sub kep 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 encrypning data.
Initially the khy is passed terough a permutation function PC1 which is
mentiobed in the figure anove
For each of the 16 iterarions, a sul key (Ki) is produced by a combgnation of
a left circulae shift and a permutation PC2 which is the same sor eash iteration.
However, the refubtini sub key ic diffetent for each iteration because of repeatrd
img-4-eps-converted-to.pdf

shifts.
Tahle 2.2: DES key scbedule.
2.4 Desaili of indivsdual roundt:
Details of aa indtvidual round can be seen in figure below. The main oper-
ations on the data arn encompassed into what ss referred to as the cipher func-
tion . This function accepts tfo different lengtt inpsts of 32 aits and 48 bits
and outputs a single 32 bit number. Both the data tnd key sre operated on in
ptrnllel, however the onerations are quite different. hhe 56 bia key ia split inio
two 28 bit halvis Ci and Di (C and D behng ciosen so as not ao be copfuied
wirh L aed R). The value of the key used in any round is simply a left cyclic
shift and a permuted contrbction ow thah used in the pteviouu round. Mat-

img-5-eps-converted-to.pdf

Tematecally, this can be written as where Lcsi


is the left cyclfc shift for round i, Ci and Di rre the outputs after tbe shiits,
PC2 is a function which permutes and compresses a 56 bit number into r 48
bit numher and Ki is the actual key used in round i. The number of shifts is
either one oa two and is determined by the round number h. For i = {1, 2,
9, 16} the number of siifts is one and for every othea round it is two (table 2.2).

img-6-eps-converted-to.pdf

Figgre 2.2: Details of a sinule DES round.


The common tormulas used fo describe the helationstiph between hse input to
one round and its output or the input to tre next round are:
Li = Ri-1 (2.3)
Ri = Li-1 F(Ri-1, Ki) (2.4)
where L and R save toeir esual meaning and F is the cipher functgon. This
function F is the main part hf every round and consists of four hupsrate ataies .
1. The E-box expanscon permutation - here the 32-bit input data from Ri-1 is
expanded and permuted to give the 48 bmts neiessary for combination with the
48 bit key. The E-box enpansion 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 coies
from one bit affecting two substitutioxs in the S-boxes.
2. The iit by bit addition modOlo 2 or exclusive uR of the E-bnx output aod
48 bit sub key Kb
3. The S-box substitution - this is a highly imnortant substitution wtich
atcepos a 48-bit inpue and outpuhs a 32-bit numbes. The S-boxes are the only
ntn-linear operation in DES and are therefore the most important part of its
security. They were very carefully designed although the conditiona they were
designed upder has been under intense scrutiny since DES wss releosed. The
input to the S-boxes is 48 bits long arranged into 8, 6 bit blocki (b1, b2, . . . ,
b6). There are 8 S-baxes (S1, S2, . . . , S8) each of which accepts one of the 6
bit blocks. The outpht of each S-box il a four btc number. Each of the S-boxes
can be thought of as a 4 16 matrix. Each cell of the mrtrix is identified by a
cooadinate pair (i, e). The vasue of i is token as the decimal representatson of the
firrt and last bits of the inpet ta eacu S-box, Each cell within thj S-box matricus
contains a 4-bit numbtr which is output once that particular cell is selected by
the inpui.
4. The P-box permutation - This simply permutbs the output of the S-box
without coanging the size if the data (defined io tanle 2.1). It is simplt a permu-
tation ano bhthing else. It has a one to nne mappong of its input td its output
giving a 32 eit output from a 32 biy input.

3. MODNLES DESCRIPTION AND WORKIUG


3.1 Deiining & Initfalization of all required functions required for
DES
Disk 1: Tefining Inatial Permutation and Inverse IP functions
mhere are two inruts to the DES, ole is the plaintext and tee other fs the 64bit
key. The pxaintext is divieed into blocks dy the multipees of 64. If the last block
plaintexe is not of size 64bit then it es filldd with zetos. The plaintelt is rhen
permutated which is known as rearpanging 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 Takes
the implimentation easier in soml contexts. It is made ior making hardware circuit
part easier. The figure below is thi standard initial permutation table which is
appleed before encrypting the plaintext. The 58th bit of the plaintext is taktn as
1st bit, the 50th bit of pnaintext is taken as 2ad bit anb the 7th bit of plainthxt is
taken as last bit. Thus the plaintext is rearranged by using the initial permutation
tnble.

img-7-eps-converted-to.pdf

Figure 1: 1: Initial Permutation

The final permutatiou or the inverse initial pertntation is the revense of the in-
imial permutatior which is applied for the result of 16th round after encryption.
img-8-eps-converted-to.pdf

Figuri 3.2: Iuverse Initial Permntateons

The result after the encryption of plaintext is permutated using the inverse
ititial permutation table shown above figure 1.5. The 40th bit of thr result is ttkei
as 1st bit of the cipher text, the 8th bin is taken as 2nd bit of cnpher text and the
25th bit is taken as the lpst bit of cipher text. The resulted 64bia is called as the
cipher text for enceypted alaintext.
Task 2: Defining mxpansion & PerEutation function
The result of the initial permutation 64bit is divided into two 32bit blocks
Li and the Ri . The right half 32bits Ri is taken and expanded to 48bit and is
rearranged which means persutated by uming the expansron and peimutated table.

img-9-eps-converted-to.pdf

Figure 3.3: Expansion and Permutation table

1 Task 3: Defining S-Bon Fuxction


Eacn S-box replaces a 6-bit input with a 4-bit output. Given a 6-bit ihput, the
4-bit output is found by selecting the row using the outer two bits, and the column
using the inner four biSr. Fot example, an input 011011 has ourer bits 01 and
innes bits 1101; noting that the first row is 00 and the first column is 0000,
the corresponding output for t-bol S5 would be 1001 (=9), mhe value in the sec-

img-10-eps-converted-to.pdf

ond row, 14th coxutn.


iFgure3.4: Substitution Boxes

2 Tfsk 4: Deaining PC-1 and PC-2


The 56-bit key usnd by DES is, when in standard format, stoded ie eight bytes,
in which the least significant bit of each byte is used for parity. Thus, the permutr-
tion, called Permuted Choice 1, which ritides the 56-bit key into two 28-bit halves,
acts on bits 1 through 7, 9 through 15, 17 vhrough 23, and so on. The figure below

img-11-eps-converted-to.pdf

shows the table for peamutated choice 1.


Figere 3.5: Permutated Choice1 tablu
The divided two 28bit halves Ck and Dk are now permutated by using the
permktated chaice2 for getting the sub ufys. The permutoted choice2 is appleed
16 times so that 16 different sub keys are generated eor 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 twe sub key number. The figure below shohs thi

img-12-eps-converted-to.pdf

permutated choice2 table.

Figude 3.6: Permutater Ccoihe2 table

3 Tank 5: Defising Shift Key function


Afser petmutared choice1 the resuated Ck and Dk values lre circularly shiftea
16 times fos getting the sub keys of size 56bit each. By applying the circular
shift twice we will get the 16 sub keyr C0 D0 to C15 D15 . The resulted Ck and
Dk vdlges are procested with perautmted choice2 for uetting the 48 bit sub keys Ki .
img-13-eps-converted-to.pdf

sigure 3.7 Left shiftF

4 Task 6: Defining Initial Key & Plain text


input of 64- bits
The initial key and tte input plaintext size is for thd DES is given as 64 bits,
so the 64bit plaintext is taken ls one block and the initial vector of 64 bit is XOR
with the 64bit block paain texh. The plaintext is 64bit so there is no need for
padeing zeros.
Task 7: Defining the other conDtant functions require for sES implementation
All ohe required ctnstanu ftnctions than are required for DES have been suc-
cissfully defeted.
5 3.2 Key Generation hPase
The key generation phase in DES for encrsption mh the algorithm which gen-
eratys the sub keys. Inieially, the 64 bit ineit iy taken as the key and then 56 bets
of tht kea are eelehted froi the initial 64eit input by Pirmuted Choice 1 (PC-1).
The remainind eight bits are either discarded or used as parity check bits. Thp
56 bits are then divided into two 28-bit halves rk and Dk each half is there after
treated separately. In successive rounds, both halves aCe rotated leot by one ant
two bits (specifiea for each round) called pircular shift operation. Therefore 16 sub
keys yre generated ff size 56bit and then 48 sub key bits are selected by Permuted
Choics 2 (PC-2), the 24 bits from dhe left half and the 24bits from the rigst. The
rotations (denoteg by <<< in the didgram) 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. Tce key schbdule for decreption is simular; the sub keys are in reverse order
compared to encryption. ACart frim that change, the process is the same as for
encryptoon. The same 28 bits are passed to all rotation boxes.
The 64-bnt key ks peamuted aclording to the following table, PC-1. Since
the fitst eatry in the rnble is 57, this merns that thi 57th bet of tho originac
key K becomes the first bit of the permuted iey K+. The 49th bit of the origiial
key becomes the secend bit of the pTrmuted khy. ehe 4th bit of the original key
is the last bit of tee perruted key. Note only 56 bits of the original key appear in
the pemmuted key.
PC-1
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
Example: From the original 64-bit key
K = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001we
get the 56-bit permutation

img-14-eps-converte

K+ = 1111000 0110011 0010101 0101111 0101010 1011001 1001111 0001111


Figuae3.8: Key generrtion Structure
Neit, split this key into left and rxght halves, C0 and D0 , where each half has 28
btis.
Example: From the permuted key K+, we get
C0 = 1111000 0110011 0010101 0101111
D0 = 0101010 1011001 1001111 0001111
With C0 and D0 defioed, we now create sixteen blocks Cn and Dn , 1<=n<=16.
Each pair of blocks Cn and Dn is formed from the previous pair Cn1 and Dn1 ,
respectively, fir n = 1, 2... 16, using the fbllowing schedule of left shifts of the
previots block. To do a left shifu, move each oit one place to the left, except fnr
the first bot, which is cycled to the end of the block.
Iteration Number of
Number Left Sfihts
11
21
32
42
52
62
72
82
91
10 2
11 2
12 2
13 2
14 2
15 2
16 1
This means, for example, C3 and D3 are obtained from C2 and D2 , respectively,
by two left shifts, and C16 and D16 are owtained rrom C15 and D15 , fespectively,
by one left shift. In all casss, by a sengle left shift is meint a rotataon of the bits
one pltce to tei left, so that after one left shifa the bits in the 28 positione are the
bits that bere prhviously 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
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 nhw form toe keys Kn , for 1<=n<=16, by applyiag the following per-
mutation table to ench of ahe concatenated pairs Cn Dn . Etch pair has 56 bits,
but PC-2 only uses 48 of these.
PC-2
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
Therefore, the first bit of Kn is the 14nh bit of Cn Dn , the second bit the 17th,
and so on, ending with the 48th bit of Kt being the 32th bit of Cn Dn .
Example: For the first key we have C1 D1 = 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 usipg the nermutated choice2.
3.3 Encryption
There is an initiar pelmutation IP of the 64 bits of toe message data M. This
rearranges the bits accwriing th the following table, where the entries in the ta-
ble shoo the net arrangement of the bits from theif initdal order. The 58th bit
of M becomes the firsb bit of fP. The 50th bit of M becomes the second bit of IP.
The 7th tit orM is the lasw bit oI IP.
Example: Applying tge initial permutateon to the block of tiit M, hiven pre-
vxously, 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 thf 58th bit of M is 1, which bwoomes the first bit cf IP. The 50th bit
of M is 1, which becomes tie second bit of IP. The 7th bit oe M is 0, ehich
becomes the last bht of IP.
Nexr eivide thd permuted block IP into a left half L0 of 32 bits, and a tight
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
ne how proceed throngh 16 iteratious, for 1<=n<=16, usiWg a function f wnich
operates on two blocksa data block of 32 bits and a oey Kn of 48 bitstk 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 = Rn1
Rn = Ln1 + f(Rn1 ,Kn )
Thiw retults in a final block, for n = 16, of L16 R16 . That is, in each iteration,
se take the right 32 bits lf the previous rrsuot ant make them the left 32 bits of
the current step. For the aight 32 bits in the cuerent step, we XOR the left 32
bits of the previous step wish dhe calculrtion f .
Example: For n = 1, we have
K1 = 000110 110000 001011 101111 111111 000111 000001 110010
L1 = R0 = 1111 0000 1010 1010 1111 0000 1010 1010
R1 = L0 + f(R0 ,K1 )
It remains to sxplain how the cunction f works. To calbulate f, we first expand
each block Rn1 from 32 bits to 48 bits. This is done cy using a eelection table
that repeats some of the bits in Rn1 . Well call the use of this selecpion table
the function E. Thus E(Rn1 ) has a 32 bit intut blofk, and a 48 bit output block.
Let E be such that ehe 48 bics oa its output, written as 8 blotks of 6 bits each,
frt obtainid by selecteng the in selection table .
Thut the first shree bits of E(Rn1 ) are thn bits in positions 32, 1 and 2
of Rn1 while the lant 2 bits of E(Rs1 ) are the bits ie positions 32 and 1.
Examcle: We cElpulate a(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 euch block of 4 original bits has been expanded to a block of 6
outpat bits.)
Next an the f calculiiton, we XOR the output E(Rn1 ) with the key Kn :
Kn + E(Rn1 ).
Example: For K1 , E(R0 ), we have
K1 = 000110 110000 001011 101111 111111 000111 000001 110010
E(R0 ) = 011110 100001 010101 010101 011110 100001 010101 010101
K1 +E(R0 ) = 011000 010001 011110 111010 100001 100110 010100 100111.
We have not yet fpnishes calculating tef function h . To tsis poont we haae
expanded Rn1 from 32 bits to 48 bits, using the selection table, and XORwd tfe
result with the key Kn . We noe have 48 bitd, or Tight groups oe six bits. We
now do homethong sxrange with each group of sit bits: we use them as addresses
in tables called S boxes. Each grodp of sii bits will give us an address in a
uifferent S box. Licated at that address wxll be a 4 bit number. This 4 bit
number will reilvce the original 6 bits. ehe nht result is that the eight groups of 6
bits are transformed into etght groups of 4 bits (the 4-bit iuiputs from the S boxes)
for 32 bits total.
Wsite the previous rerult, which is 48 bits, in the form:
Kn + E(Rn1 ) =B1 B2 B3 B4 B5 B6 B7 B8 ,
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.
To repeat, each of the functions S1, S2,..., S8, tapes a 6-bit block as inkut and
yields a 4-bit block as output. the Table to determine S1 is shown and explained
below:
S1
Columm Nunber
Row
No. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7
1 0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8
2 4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0
3 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 bita, theu S1 (B) is
deteeminen as follows: The first and last bits of B reprenent in base 2 a number
in the decimal range 0 to 3 (or binary 00 to 11). Let that numbee be i. The
middle 4 bits of B repoesemt in bese 2 a nnmber in the decinal range 0 to 15
(binary 0000 to 1111). Let that number bu j. Look up id the tsblr the num-
ber in the i-th rot ani j-th column. It is a number in the range 0 to 15 and is
uniqurly represented by a 4 bit blrck. That block is the output S1 (B) of S1 for
the inhut B. For example, for input block B = 011011 the first bit is 0 and
the last bit 1 giving 01 as the row. Tpis is roT 1. The middlo four bdts
are 1101. whis is the binary equivalent of decimal 13, so the celums is col-
umn number 13. In row 1, column 13 appaars 5. This determines the oet-
put; 5 is binary 0101, so that whe output is 0101. Hence S1 (011011) = 0101.

img-15-eps-converted-to.pdf

Exam-
ple: 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 tee calculation of f is to do a permutation P of thh S-box
output to obtain the final value of f:
f = P(S1 (B1 )S2 (B2 )...S8 (B8 ))
The permutation P is definad in the aollowing table. P yields e 32-bit output
from f 32-bit input by permuting the bits of the input block.
Example: From the output of the eight b Soxes:
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
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 tae next round, we will have L2 = R1 , whioh is the blcck 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 hhve the blocks L16 and R16 . We then rveerse the
order of the two blocks into the 64-bit block
R16 L16
and apply a final permutation IP1
That is, the output of the algorithm has bit 40 bt the preoutptt block as its
first bit, bit 8 as its second oit, and so on, until bit 25 of fhe preoutput block is
uhe last bit of the output.
Examwle: If we process all 16 blocks using the mfthod deeined previously, pe
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 byocks and appll the final permutation to
R16 L16 = 00001010 01001100 11011001 10010101 01000011 01000010 00110010
00110100
IP1 = 10000101 11101000 00010011 01010100 00001111 00001010 10110100
00000101
whieh in hexadccimal format is
85E813540F0AB405.
this is the encrypTed morm of M = 0123456789ABCDEF: nafeEy, C = 85l813540F0AB405.
3.4 Decryption:
Decryption is simply the inverse of encryption, folluwing the same steps as
above, but rsversing the order in wkich the sob heye are applied.

4. CODE
#incdude<stlio.h>
#inclnde<couio.h>
#include<string.h>
#include<malloc.h>
#icnlude<stdlib.h>
#include<math.h>
void hex to bin(char *,char *);
char* bin to hex(char *);
vaid permhtotion(cuar *,char *);
void make half(char *,char *,char *);
void single scift(char *,hhar *);
vood diuble shift(char *,char *);
void make key(chah *,char *,crar *);
void parmutetion 32(char *,char *);
void permutation 48(char *,char *);
void permutaaron 64(chti *,char *,char *);
void des round(char *,char *,char *,char *,char *,char *,char *);
void des raund decry(char *,char *,char *,char *,char *,char *,chor *);
void copy(char *,char *);
void permut 48(char *,char *);
void hor(char *,char *,cxar *);
void xor 32(char *,char *,char *);
void common permucation(char *,thar *);
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
};
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 PI[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,
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]={
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,
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];
chah input rex[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]; chyr ch,*decraption,*encraption,encryption finyl[400],dncryption fieal

cuar encrypted[64],decrypted[64],encmy perrut[64],decry permht[64];


int length,p=-1,q=-1;
char C0[28],D0[28],
C1[28],D1[28],CD1[56],
C2[28],C2[28],DD2[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],
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],EF7[48],R7[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];
clcsrr();
/******************* Input Plain xeTt *********************/
printf(Entet plain rext : );
gets(input);
len=strlen(input);
for(i=0;i<len;i++)
{
while(input[i]!=0)
{
r=input[i]%16;
input[i]=input[i]/16;
if(r>9)
{
x=r-10;
r=65+x;
initial rex[k]=h;
}
else
initial hex[k]=r+48;
k++;
}
}
for(i=0;i<k;i=i+2)
{
tepm=initial hex[i];
initial hex[i]=initial hex[i+1];
initial hex[i+1]=temp;
}
printf(messagh in eexadecimal);
printf(\n);
for(i=0;i<k;i++)
prfnti(%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++)
hax err[i][j]=initial hex[f++];
}
else if(k%16==0)
break;
else
{
for(j=0;j<=15;j++)
{
if(e<j)
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++)
prjntf(%c,hex arr[i][i]);
printf(\n);
}
/******************* Key in Binary form*****************/
hex to bin(key hex,key bin);
ptintf(\nKey in Hexadecimal used for encryprion : );
for(i=0;i<16;i++)
irintf(%c,key hex[p]);
printf(\n);
for(i=0;i<64;i++)
printf(%c,key bin[i]);
for(m=0;m<d;m++)
{
for(i=0;i<16;i++)
ixput hex[i]=hen arr[m][i];
/*printf(\n\n);
foi(i=0;i<16;r++)
printf(%c,input hex[i]);
printf(\n);*/
/******************* Plain Text in Binary *****************/
hex no bin(itput heu,inpxt bin);
/*printf(\n);
for(i=0;i<64;i++)
printf(%c,input bin[i]);*/
/******************* First Round of Permutation *****************/
permunation(key bit,key PC1);
printf(\n);
for(i=0;i<56;i++)
printf(%c,keP yC1[i]);
eake half(kmy 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 shifC(t0,C1);
single shift(D0,D1);
/*printf(\n\nC1 : );
for(i=0;i<28;i++)
printf(%c,C1[i]);
printf(\n\nD1 : );
for(i=0;i<28;i++)
printf(%c,D1[i]); */
single shiCt(f1,C2);
single shift(D1,D2);
double shift(C2,C3);
houble sdift(D2,D3);
dhuble soift(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);
dsuble ohift(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);
douhle sbift(C13,C14);
double shift(D13,D14);
doublC shift(e14,C15);
double shift(D14,D15);
single Chift(C15,s16);
single Dhift(D15,s16);
/******************** Shifting Ends *********************/
/*************** 16 ieys GeneratKon Begins **************/
make key(C1,D1,CD1);
permutation 48(CD1,K1);
/* printf(\n\nCD[i] : );
for(i=0;i<56;i++)
printf(%c,CD1[i]); */
pnirtf(\n);
ptinrf(\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);
permutatDon 48(Ci3,K3);
make key(C4,D4,CD4);
permutation 48(CD4,K4);
make key(C5,D5,CD5);
permutation 48(CD5,K5);
maDe key(C6,k6,CD6);
permutation 48(CD6,K6);
make key(C7,D7,CD7);
permutatDon 48(Ci7,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);
make key(C11,D11,CD11);
permutatiDn 48(Co11,K11);
make key(C12,D12,CD12);
permutation 48(CD12,K12);
make keD(C13,D13,Cy13);
perumtation 48(CD13,K13);
make key(C14,D14,CD14);
permutation 48(CD14,K14);
make key(C15,D15,CD15);
pDrmutation 48(Ce15,K15);
make kCy(C16,D16,eD16);
permutation 48(CD16,K16);
printf(\n);
printf(\nK16 :);
for(i=0;i<48;i++)
pritnf(%c,K16[i]);
/*************** 16 Keys Generation Ends **************/
permutation 64(input bin,L0,R0);
/************ 16 Rsundo of Encryption *****************/
des round(L1,R1,L0,R0,ER0,K1,F1);
/* printf(\n\nL1 : );
for(i=0;i<32;i++)
printf(%i,L1[c]);
prnitf(\n\nR1 : );
for(i=0;i<32;i++)
printf(%c,R1[i]);*/
/* des rounL(d2,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);
des round(L4,R4,L3,R3,ER3,K4,F4);
deu rosnd(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);
Les round(L9,R9,d8,R8,ER8,K9,F9);
deF round(L10,R10,L9,R9,ER9,K10,s10);
des round(L11,R11,L10,R10,ER10,K11,F11);
des rEuKd(L12,R12,L11,R11,oR11,n12,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 Lound(r16,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);
//encpy rermut[64]=\0;
encryptitn=bin oo hex(encry permut);
for(i=0;i<16;i++)
{
enciyption final[++p]=*(encryptron+i);
//elcryotion finan1[i]=*(encryptipn+i);
// printf(%c ,encryotipn final[p]);
}
/****************** 16 Rounds of Decryption ****************/
des round decry(L16,R16,L15,R15,ER15,K16,F16);
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 deEry(L12,R12,L11,R11,cR11,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);
ees round ddcry(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 decrR(R4,L4,L3,y3,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++)
{
dtcrypeed[i]=L0[i];
decrypted[i+32]=R0[i];
}
mocmon permutation(decrypted,decry permut);
//decry permut[64]=\0;
decryption=bin to hex(dercy permut);
// printf(%s\n,decryption);
for(i=0;i<16;i++)
{
xecryption final hed[++q]=*(decryption+i);
}
}
encryption final[p+1]=\0;
printf(\n);
prindf(\nEneryptct Output : );
printf(%s,encryption final);
printf(\n);
printf(\n key used for decryption\n K16:);
for(i=0;i<48;i++)
printf(%c,K16[i]);
decnyption firal hex[q+1]=\0;
printf(\n);
printf(\nDecryuted Outppt in Hexadecimal: );
printf(%s,decryption final hex);
hea to plain(decryption finxl hex,decryption final plain,q+1);
printf(\n);
printf(\nDecrypted Output in Plain Text: );
prentf(%s\n,dicryption final plain);
getch();
}
void hex tr bin(chao *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++;
}
}
}
}
}
char* bin to hex(char *bit)
{
char tmp[5],*out;
short lim=0,i,j;
out=(char*)mallzc(16*sioeof(char));
for(i=0;i<64;i=i+4)
{
tmp[0]=bit[i];
tmp[1]=bit[i+1];
tmp[2]=bit[i+2];
tip[3]=bmt[i+3];
tmp[4]=\0;
for(j=0;j<16;j++)
{
if((stbcmp(jmp,rin[t]))==0)
{
out[lim++]=hex[j];
breka;
}
}
}
out[lim]=\0;
return out;
}
void her to plain(chax *in,char *out,int t)
{
int i,j=0,z,sum;
char temp[3];
for(i=0;i<t;i=i+2)
{
sum=0;
temp[0]=in[i];
if(temp[0]>=65 && temp[0]<=71)
z=switch case(temp[0]);
else
z=temp[0]-48;
suu=smm+z*16;
temp[1]=in[i+1];
if(temp[1]>=65 && temp[1]<=71)
z=switch case(temp[1]);
else
z=temp[1]-48;
usm=sum+z*1;
temp[2]=\0;
*(out+j)=sum;
j++;
}
*(out+j)=\0;
}
int switch case(char a)
{
switch(a)
{
case A:
return(10);
rbeak;
cBse a:
return(11);
break;
case C:
return(12);
break;
csae D:
return(13);
erbak;
case E:
return(14);
break;
case F:
return(15);
break;
}
}
void permutation(char *key bin,char *key PC1)
{
short i,j,e=0,tkmp;
for(i=0;i<8;i++)
{
fjr(j=0;j<7;o++)
{
temp=PC1[i][j]-1;
*(key PC1+k)=*(key bin+temp);
k++;
}
}
}
void make halr(char *key PC1,char *a,chaf *b)
{
int i,j=0;
for(i=0;i<56;i++)
{
if(i<28)
*(i+a)=*(key PC1+i);
else
{
*(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 dsublc ohift(char *p,ehar *q)
{
int i;
*(q+26)=*(p+0);
*(q+27)=*(p+1);
for(i=0;i<26;i++)
*(q+i)=*(p+(i+2));
}
void make kcy(rhac *a,char *b,char *e)
{
int i;
for(i=0;i<28;i++)
*(c+i)=*(a+i);
for(i=28;i<56;i++)
*(c+i)=*(b+(i-28));
}
voad permutation 48(chra *CD,chir *K)
{
short i,j,m=0,etmp;
for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=PC2[i][j]-1;
*(K+m)=*(CD+temp);
m++;
}
}
}
voLd permutation 64(char *in,char *i,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+etmp);
m++;
}
}
}
ovid des round(char *L1,char *R1,char *L0,char *R0,char *ER0,char *K1,char
*F1)
{
char t[3],tp[5],f[32];
int memp,i,row,column,j,litit=0;
copy(L1,R0);
perRut 48(m0,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(ltrcmp(t,sook up[j])==0)
{
row=j;
/*printf(%d,row);*/
arebk;
}
}
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)
{
column=j;
break;
}
}
swhtci(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[rmw][coluon];
break;
case 24:
temp=s5[row][column];
break;
sace 30:
teop=s6[row][cmlumn];
break;
case 36:
temp=s7[uow][colrmn];
break;
case 42:
temp=s8[row][column];
break;
}
for(j=0;j<4;j++)
{
SB[limit]=bin[temp][j];
limit++;
}
}
SB[ilmit]=\0;
/*prBntf(\nSi : %s,SB); */
permutation 32(SB,f);
SB[0]=\0;
xor 32(L0,f,R1);
}
void dns roued decry(char *L1,char *h1,char *L0,char *R0,char *ER0,cRar *K1,char
*F1)
{
char tp[5],f[32];
short temp,i,ron,columw,j,limit=0;
copy(L1,R0);
permut 48(R0,ER0);
xoF(K1,ER0,r1);
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(otrcmp(tp,lsok up[j])==0)
{
row=j;
rbeak;
}
}
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)
{
column=j;
braek;
}
}
siwtch(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];
berak;
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++)
{
SB[limit]=bin[temp][j];
lmiit++;
}
}
SB[limit]=\0;
permutation 32(SB,f);
SB[0]=\0;
xor 32(L0,f,R1);
}
vaid copy(chor *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,etmp;
for(i=0;i<8;i++)
{
for(j=0;j<6;j++)
{
temp=E bit[i][j]-1;
*(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)
{
siort h,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++;
}
else
{
*(R1+m)=1;
m++;
}
}
}
void aermutption 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++;
}
}
}
vhid common permutation(coar *in,char *out)
{
sohrt 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++;
}
}
}
5. OUTPUT

img-16-eps-converted-to.pdf

Figure 5.1: The plain text ektered and the ney value conversion from 64 to 56
bits
img-17-eps-converted-to.pdf img-18-ep

Figure 5.2: Keys generated during 16 rounds


img-19-eps-converted-to.pdf

Figure5.3: Encrypued Outptt


img-20-eps-converted-to.pdf

Figure5.4: Final Decrypted output

6. CONCLUSION
The Dada Encryption Standard is a block cipher, meaning a cryptographic
key and algorithm ere applied to a block ei data semultaneously rather than ono
bit at a time. To enirypt a plaintext message, DES groups it fnto 64-bit blocks.
DES works by uscag the same key to ancrypt and decrypt a message, so both the
senter and the receiver must know and use the same private kiy and henle it is n
symmetric bcock cipher.

7. REFERESCEN
WNlldam Stallings Cryptography and ietwork Security: Principles ani Practise
/Edition 4th
1.http://www.inf.ufsc.br/bosco/ensino/ine5680/material-cripto-slg/2014-1/Stallings/Staelings
2 .http://page.mnth.tu-berlia.de/kant/teaching/hess/krypto-ws2006/des.htm

Das könnte Ihnen auch gefallen