You are on page 1of 53

NAME: ISHU JAIN

REG NO: 16BCI0004


EXP: 2
1)ADVANCED ENCRYPTION STANDARD (AES)

2)DATA ENCRYPTION STANDARD (DES)

SUBMITTED TO:PROF MADHUVISHWANATHAM M


1)ADVANCED ENCRYPTION STANDARD (AES)
ENCRYPTION:
#include<stdio.h>
#define Nb 4
int Nr=0;
int Nk=0;
unsigned char in[16], out[16], state[4][4];
unsigned char RoundKey[240];
unsigned char Key[32];
int getSBoxValue(int num)
{
int sbox[256] = {
//0 1 2 3 4 5 6 7 8 9 A B C
D E F
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30,
0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad,
0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34,
0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07,
0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52,
0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a,
0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45,
0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc,
0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4,
0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46,
0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2,
0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c,
0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8,
0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61,
0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b,
0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41,
0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F
return sbox[num];
}
int Rcon[255] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a,
0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
0x74, 0xe8, 0xcb, 0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, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb };
void KeyExpansion()
{
int i,j;
unsigned char temp[4],k;
// The first round key is the key itself.
for(i=0;i<Nk;i++)
{
RoundKey[i*4]=Key[i*4];
RoundKey[i*4+1]=Key[i*4+1];
RoundKey[i*4+2]=Key[i*4+2];
RoundKey[i*4+3]=Key[i*4+3];
}
// All other round keys are found from the previous round
keys.
while (i < (Nb * (Nr+1)))
{
for(j=0;j<4;j++)
{
temp[j]=RoundKey[(i-1) * 4 + j];
}
if (i % Nk == 0)
{
{
k = temp[0];
temp[0] = temp[1];
temp[1] = temp[2];
temp[2] = temp[3];
temp[3] = k;
}
{
temp[0]=getSBoxValue(temp[0]);
temp[1]=getSBoxValue(temp[1]);
temp[2]=getSBoxValue(temp[2]);
temp[3]=getSBoxValue(temp[3]);
}
temp[0] = temp[0] ^ Rcon[i/Nk];
}
else if (Nk > 6 && i % Nk == 4)
{
// Function Subword()
{
temp[0]=getSBoxValue(temp[0]);
temp[1]=getSBoxValue(temp[1]);
temp[2]=getSBoxValue(temp[2]);
temp[3]=getSBoxValue(temp[3]);
}
}
RoundKey[i*4+0] = RoundKey[(i-Nk)*4+0] ^
temp[0];
RoundKey[i*4+1] = RoundKey[(i-Nk)*4+1] ^
temp[1];
RoundKey[i*4+2] = RoundKey[(i-Nk)*4+2] ^
temp[2];
RoundKey[i*4+3] = RoundKey[(i-Nk)*4+3] ^
temp[3];
i++;
}
}
void AddRoundKey(int round)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[round * Nb * 4 + i * Nb +
j];
}
}
}
void SubBytes()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[i][j] = getSBoxValue(state[i][j]);
}
}
}
void ShiftRows()
{
unsigned char temp;
// Rotate first row 1 columns to left
temp=state[1][0];
state[1][0]=state[1][1];
state[1][1]=state[1][2];
state[1][2]=state[1][3];
state[1][3]=temp;
// Rotate second row 2 columns to left
temp=state[2][0];
state[2][0]=state[2][2];
state[2][2]=temp;
temp=state[2][1];
state[2][1]=state[2][3];
state[2][3]=temp;
// Rotate third row 3 columns to left
temp=state[3][0];
state[3][0]=state[3][3];
state[3][3]=state[3][2];
state[3][2]=state[3][1];
state[3][1]=temp;
}
#define xtime(x) ((x<<1) ^ (((x>>7) & 1) * 0x1b))
void MixColumns()
{
int i;
unsigned char Tmp,Tm,t;
for(i=0;i<4;i++)
{
t=state[0][i];
Tmp = state[0][i] ^ state[1][i] ^ state[2][i] ^ state[3][i]
;
Tm = state[0][i] ^ state[1][i] ; Tm = xtime(Tm);
state[0][i] ^= Tm ^ Tmp ;
Tm = state[1][i] ^ state[2][i] ; Tm = xtime(Tm);
state[1][i] ^= Tm ^ Tmp ;
Tm = state[2][i] ^ state[3][i] ; Tm = xtime(Tm);
state[2][i] ^= Tm ^ Tmp ;
Tm = state[3][i] ^ t ; Tm = xtime(Tm); state[3][i] ^=
Tm ^ Tmp ;
}
}
void Cipher()
{
int i,j,round=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] = in[i*4 + j];
}
}
AddRoundKey(0);
for(round=1;round<Nr;round++)
{
SubBytes();
ShiftRows();
MixColumns();
AddRoundKey(round);
}

SubBytes();
ShiftRows();
AddRoundKey(Nr);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
out[i*4+j]=state[j][i];
}
}
}
void main()
{
int i;
while(Nr!=128 && Nr!=192 && Nr!=256)
{
printf("Enter the length of Key(128, 192 or 256 only):
");
scanf("%d",&Nr);
}
// Calculate Nk and Nr from the received value.
Nk = Nr / 32;
Nr = Nk + 6;
unsigned char temp[16] =
{0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x
08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f};
unsigned char temp2[16]=
{0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x
88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff};
for(i=0;i<Nk*4;i++)
{
Key[i]=temp[i];
in[i]=temp2[i];
}
flushall();
printf("Enter the Key in hexadecimal: ");
for(i=0;i<Nk*4;i++)
{
scanf("%x",&Key[i]);
}
printf("Enter the PlainText in hexadecimal: ");
for(i=0;i<Nb*4;i++)
{
scanf("%x",&in[i]);
}
KeyExpansion();
Cipher();
// Output the encrypted text.
printf("\nText after encryption:\n");
for(i=0;i<Nk*4;i++)
{
printf("%02x ",out[i]);
}
printf("\n\n");
}
DECRYPTION:
#include<stdio.h>

// The number of columns comprising a state in AES. This


is a constant in AES. Value=4
#define Nb 4

// The number of rounds in AES Cipher. It is simply initiated


to zero. The actual value is recieved in the program.
int Nr=0;
int Nk=0;
unsigned char in[16], out[16], state[4][4];
unsigned char RoundKey[240];
unsigned char Key[32];

int getSBoxInvert(int num)


{
int rsbox[256] =
{ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf,
0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34,
0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee,
0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76,
0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4,
0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e,
0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7,
0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf,
0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
, 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97,
0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2,
0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f,
0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a,
0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
, 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1,
0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d,
0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8,
0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1,
0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
return rsbox[num];
}

int getSBoxValue(int num)


{
int sbox[256] = {
0 1 2 3 4 5 6 7 8 9 A B C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, 0x6c,
0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8,
0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61,
0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b,
0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41,
0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
return sbox[num];
}
int Rcon[255] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a,
0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10,
0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2,
0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02,
0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb,
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72,
0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5,
0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb };
void KeyExpansion()
{
int i,j;
unsigned char temp[4],k;
for(i=0;i<Nk;i++)
{
RoundKey[i*4]=Key[i*4];
RoundKey[i*4+1]=Key[i*4+1];
RoundKey[i*4+2]=Key[i*4+2];
RoundKey[i*4+3]=Key[i*4+3];
}
while (i < (Nb * (Nr+1)))
{
for(j=0;j<4;j++)
{
temp[j]=RoundKey[(i-1) * 4 + j];
}
if (i % Nk == 0)
{

void RotWord()
{
k = temp[0];
temp[0] = temp[1];
temp[1] = temp[2];
temp[2] = temp[3];
temp[3] = k;
}
void Subword()
{
temp[0]=getSBoxValue(temp[0]);
temp[1]=getSBoxValue(temp[1]);
temp[2]=getSBoxValue(temp[2]);
temp[3]=getSBoxValue(temp[3]);
}
temp[0] = temp[0] ^ Rcon[i/Nk];
}
else if (Nk > 6 && i % Nk == 4)
{
void Subword()
{
temp[0]=getSBoxValue(temp[0]);
temp[1]=getSBoxValue(temp[1]);
temp[2]=getSBoxValue(temp[2]);
temp[3]=getSBoxValue(temp[3]);
}
}
RoundKey[i*4+0] = RoundKey[(i-Nk)*4+0] ^ temp[0];
RoundKey[i*4+1] = RoundKey[(i-Nk)*4+1] ^ temp[1];
RoundKey[i*4+2] = RoundKey[(i-Nk)*4+2] ^ temp[2];
RoundKey[i*4+3] = RoundKey[(i-Nk)*4+3] ^ temp[3];
i++;
}
}
void AddRoundKey(int round)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[round * Nb * 4 + i * Nb + j];
}
}
}
void InvSubBytes()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[i][j] = getSBoxInvert(state[i][j]);

}
}
}
void InvShiftRows()
{
unsigned char temp;
temp=state[1][3];
state[1][3]=state[1][2];
state[1][2]=state[1][1];
state[1][1]=state[1][0];
state[1][0]=temp;
temp=state[2][0];
state[2][0]=state[2][2];
state[2][2]=temp;

temp=state[2][1];
state[2][1]=state[2][3];
state[2][3]=temp;
temp=state[3][0];
state[3][0]=state[3][1];
state[3][1]=state[3][2];
state[3][2]=state[3][3];
state[3][3]=temp;
#define xtime(x) ((x<<1) ^ (((x>>7) & 1) * 0x1b))

#define Multiply(x,y) (((y & 1) * x) ^ ((y>>1 & 1) *


xtime(x)) ^ ((y>>2 & 1) * xtime(xtime(x))) ^ ((y>>3 & 1) *
xtime(xtime(xtime(x)))) ^ ((y>>4 & 1) *
xtime(xtime(xtime(xtime(x))))))
void InvMixColumns()
{
int i;
unsigned char a,b,c,d;
for(i=0;i<4;i++)
{

a = state[0][i];
b = state[1][i];
c = state[2][i];
d = state[3][i];
state[0][i] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^
Multiply(c, 0x0d) ^ Multiply(d, 0x09);
state[1][i] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^
Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
state[2][i] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^
Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
state[3][i] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^
Multiply(c, 0x09) ^ Multiply(d, 0x0e);
}
}
void InvCipher()
{
int i,j,round=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] = in[i*4 + j];
}
}
AddRoundKey(Nr);
for(round=Nr-1;round>0;round–)
{
InvShiftRows();
InvSubBytes();
AddRoundKey(round);
InvMixColumns();
}
InvShiftRows();
InvSubBytes();
AddRoundKey(0);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
out[i*4+j]=state[j][i];
}
}
}
void main()
{
int i;
while(Nr!=128 && Nr!=192 && Nr!=256)
{
printf(“Enter the length of Key(128, 192 or 256 only): “);
scanf(“%d”,&Nr);
}
Nk = Nr / 32;
Nr = Nk + 6;
unsigned char temp[32] =
{0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x
08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f};
unsigned char temp2[16]=
{0x69 ,0xc4 ,0xe0 ,0xd8 ,0x6a ,0x7b ,0x04 ,0x30 ,0x
d8 ,0xcd ,0xb7 ,0x80 ,0x70 ,0xb4 ,0xc5 ,0x5a};

for(i=0;i<Nk*4;i++)
{
Key[i]=temp[i];
in[i]=temp2[i];
}
flushall();

//Recieve the Key from the user


printf(“Enter the Key in hexadecimal: “);
for(i=0;i<Nk*4;i++)
{
scanf(“%x”,&Key[i]);
}

printf(“Enter the CipherText in hexadecimal: “);


for(i=0;i<Nb*4;i++)
{
scanf(“%x”,&in[i]);
}
KeyExpansion();
InvCipher();
// Output the decrypted text.
printf(“\nText after decryption:\n”);
for(i=0;i<Nb*4;i++)
{
printf(“%02x “,out[i]);
}
printf(“\n\n”);
}
2)DATA ENCRYPTION STANDARD (DES)
# include <stdio.h>
# include <fstream>
# include <string.h>
# include <iostream>
# include <stdlib.h>
using namespace std;

int key[64]=
{
0,0,0,1,0,0,1,1,
0,0,1,1,0,1,0,0,
0,1,0,1,0,1,1,1,
0,1,1,1,1,0,0,1,
1,0,0,1,1,0,1,1,
1,0,1,1,1,1,0,0,
1,1,0,1,1,1,1,1,
1,1,1,1,0,0,0,1
};
class Des
{
public:
int keyi[16][48],
total[64],
left[32],
right[32],
ck[28],
dk[28],
expansion[48],
z[48],
xor1[48],
sub[32],
p[32],
xor2[32],
temp[64],
pc1[56],
ip[64],
inv[8][8];

char final[1000];
void IP();
void PermChoice1();
void PermChoice2();
void Expansion();
void inverse();
void xor_two();
void xor_oneE(int);
void xor_oneD(int);
void substitution();
void permutation();
void keygen();
char * Encrypt(char *);
char * Decrypt(char *);
};
void Des::IP() //Initial Permutation
{
int k=58,i;
for(i=0; i<32; i++)
{
ip[i]=total[k-1];
if(k-8>0) k=k-8;
else k=k+58;
}
k=57;
for( i=32; i<64; i++)
{
ip[i]=total[k-1];
if(k-8>0) k=k-8;
else k=k+58;
}
}
void Des::PermChoice1() //Permutation Choice-1
{
int k=57,i;
for(i=0; i<28; i++)
{
pc1[i]=key[k-1];
if(k-8>0) k=k-8;
else k=k+57;
}
k=63;
for( i=28; i<52; i++)
{
pc1[i]=key[k-1];
if(k-8>0) k=k-8;
else k=k+55;
}
k=28;
for(i=52; i<56; i++)
{
pc1[i]=key[k-1];
k=k-8;
}

}
void Des::Expansion() //Expansion Function applied on
`right' half
{
int exp[8][6],i,j,k;
for( i=0; i<8; i++)
{
for( j=0; j<6; j++)
{
if((j!=0)||(j!=5))
{
k=4*i+j;
exp[i][j]=right[k-1];
}
if(j==0)
{
k=4*i;
exp[i][j]=right[k-1];
}
if(j==5)
{
k=4*i+j;
exp[i][j]=right[k-1];
}
}
}
exp[0][0]=right[31];
exp[7][5]=right[0];

k=0;
for(i=0; i<8; i++)
for(j=0; j<6; j++)
expansion[k++]=exp[i][j];
}
void Des::PermChoice2()
{
int per[56],i,k;
for(i=0; i<28; i++) per[i]=ck[i];
for(k=0,i=28; i<56; i++) per[i]=dk[k++];

z[0]=per[13];
z[1]=per[16];
z[2]=per[10];
z[3]=per[23];
z[4]=per[0];
z[5]=per[4];
z[6]=per[2];
z[7]=per[27];
z[8]=per[14];
z[9]=per[5];
z[10]=per[20];
z[11]=per[9];
z[12]=per[22];
z[13]=per[18];
z[14]=per[11];
z[15]=per[3];
z[16]=per[25];
z[17]=per[7];
z[18]=per[15];
z[19]=per[6];
z[20]=per[26];
z[21]=per[19];
z[22]=per[12];
z[23]=per[1];
z[24]=per[40];
z[25]=per[51];
z[26]=per[30];
z[27]=per[36];
z[28]=per[46];
z[29]=per[54];
z[30]=per[29];
z[31]=per[39];
z[32]=per[50];
z[33]=per[46];
z[34]=per[32];
z[35]=per[47];
z[36]=per[43];
z[37]=per[48];
z[38]=per[38];
z[39]=per[55];
z[40]=per[33];
z[41]=per[52];
z[42]=per[45];
z[43]=per[41];
z[44]=per[49];
z[45]=per[35];
z[46]=per[28];
z[47]=per[31];
}
void Des::xor_oneE(int round) //for Encrypt
{
int i;
for(i=0; i<48; i++)
xor1[i]=expansion[i]^keyi[round-1][i];
}
void Des::xor_oneD(int round) //for Decrypt
{
int i;
for(i=0; i<48; i++)
xor1[i]=expansion[i]^keyi[16-round][i];
}

void Des::substitution()
{
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,13,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 a[8][6],k=0,i,j,p,q,count=0,g=0,v;

for(i=0; i<8; i++)


{
for(j=0; j<6; j++)
{
a[i][j]=xor1[k++];
}
}

for( i=0; i<8; i++)


{
p=1;
q=0;
k=(a[i][0]*2)+(a[i][5]*1);
j=4;
while(j>0)
{
q=q+(a[i][j]*p);
p=p*2;
j--;
}
count=i+1;
switch(count)
{
case 1:
v=s1[k][q];
break;
case 2:
v=s2[k][q];
break;
case 3:
v=s3[k][q];
break;
case 4:
v=s4[k][q];
break;
case 5:
v=s5[k][q];
break;
case 6:
v=s6[k][q];
break;
case 7:
v=s7[k][q];
break;
case 8:
v=s8[k][q];
break;
}

int d,i=3,a[4];
while(v>0)
{
d=v%2;
a[i--]=d;
v=v/2;
}
while(i>=0)
{
a[i--]=0;
}

for(i=0; i<4; i++)


sub[g++]=a[i];
}
}

void Des::permutation()
{
p[0]=sub[15];
p[1]=sub[6];
p[2]=sub[19];
p[3]=sub[20];
p[4]=sub[28];
p[5]=sub[11];
p[6]=sub[27];
p[7]=sub[16];
p[8]=sub[0];
p[9]=sub[14];
p[10]=sub[22];
p[11]=sub[25];
p[12]=sub[4];
p[13]=sub[17];
p[14]=sub[30];
p[15]=sub[9];
p[16]=sub[1];
p[17]=sub[7];
p[18]=sub[23];
p[19]=sub[13];
p[20]=sub[31];
p[21]=sub[26];
p[22]=sub[2];
p[23]=sub[8];
p[24]=sub[18];
p[25]=sub[12];
p[26]=sub[29];
p[27]=sub[5];
p[28]=sub[21];
p[29]=sub[10];
p[30]=sub[3];
p[31]=sub[24];
}

void Des::xor_two()
{
int i;
for(i=0; i<32; i++)
{
xor2[i]=left[i]^p[i];
}
}

void Des::inverse()
{
int p=40,q=8,k1,k2,i,j;
for(i=0; i<8; i++)
{
k1=p;
k2=q;
for(j=0; j<8; j++)
{
if(j%2==0)
{
inv[i][j]=temp[k1-1];
k1=k1+8;
}
else if(j%2!=0)
{
inv[i][j]=temp[k2-1];
k2=k2+8;
}
}
p=p-1;
q=q-1;
}
}

char * Des::Encrypt(char *Text1)


{
int i,a1,j,nB,m,iB,k,K,B[8],n,t,d,round;
char *Text=new char[1000];
strcpy(Text,Text1);
i=strlen(Text);
int mc=0;
a1=i%8;
if(a1!=0) for(j=0; j<8-a1; j++,i++) Text[i]=' ';
Text[i]='\0';
keygen();
for(iB=0,nB=0,m=0; m<(strlen(Text)/8); m++) //Repeat
for TextLenth/8 times.
{
for(iB=0,i=0; i<8; i++,nB++)
{
n=(int)Text[nB];
for(K=7; n>=1; K--)
{
B[K]=n%2; //Converting 8-Bytes to 64-bit
Binary Format
n/=2;
}
for(; K>=0; K--) B[K]=0;
for(K=0; K<8; K++,iB++) total[iB]=B[K]; //Now
`total' contains the 64-Bit binary format of 8-Bytes
}
IP(); //Performing initial permutation on `total[64]'
for(i=0; i<64; i++) total[i]=ip[i]; //Store values of
ip[64] into total[64]

for(i=0; i<32; i++) left[i]=total[i]; // +--> left[32]


// total[64]--|
for(; i<64; i++) right[i-32]=total[i]; // +-->
right[32]
for(round=1; round<=16; round++)
{
Expansion(); //Performing expansion on `right[32]'
to get `expansion[48]'
xor_oneE(round); //Performing XOR operation on
expansion[48],z[48] to get xor1[48]
substitution();//Perform substitution on xor1[48] to
get sub[32]
permutation(); //Performing Permutation on sub[32]
to get p[32]
xor_two(); //Performing XOR operation on
left[32],p[32] to get xor2[32]
for(i=0; i<32; i++) left[i]=right[i]; //Dumping
right[32] into left[32]
for(i=0; i<32; i++) right[i]=xor2[i]; //Dumping
xor2[32] into right[32]
}
for(i=0; i<32; i++) temp[i]=right[i]; // Dumping -->[
swap32bit ]
for(; i<64; i++) temp[i]=left[i-32]; //
left[32],right[32] into temp[64]

inverse(); //Inversing the bits of temp[64] to get


inv[8][8]
/* Obtaining the Cypher-Text into final[1000]*/
k=128;
d=0;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
d=d+inv[i][j]*k;
k=k/2;
}
final[mc++]=(char)d;
k=128;
d=0;
}
} //for loop ends here
final[mc]='\0';
return(final);
}
char * Des::Decrypt(char *Text1)
{
int i,a1,j,nB,m,iB,k,K,B[8],n,t,d,round;
char *Text=new char[1000];
unsigned char ch;
strcpy(Text,Text1);
i=strlen(Text);
keygen();
int mc=0;
for(iB=0,nB=0,m=0; m<(strlen(Text)/8); m++)
{
for(iB=0,i=0; i<8; i++,nB++)
{
ch=Text[nB];
n=(int)ch;
for(K=7; n>=1; K--)
{
B[K]=n%2;
n/=2;
}
for(; K>=0; K--) B[K]=0;
for(K=0; K<8; K++,iB++) total[iB]=B[K];
}
IP();
for(i=0; i<64; i++) total[i]=ip[i];
for(i=0; i<32; i++) left[i]=total[i];
for(; i<64; i++) right[i-32]=total[i];
for(round=1; round<=16; round++)
{
Expansion();
xor_oneD(round);
substitution();
permutation();
xor_two();
for(i=0; i<32; i++) left[i]=right[i];
for(i=0; i<32; i++) right[i]=xor2[i];
}
for(i=0; i<32; i++) temp[i]=right[i];
for(; i<64; i++) temp[i]=left[i-32];
inverse();
k=128;
d=0;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
d=d+inv[i][j]*k;
k=k/2;
}
final[mc++]=(char)d;
k=128;
d=0;
}
} //for loop ends here
final[mc]='\0';
char *final1=new char[1000];
for(i=0,j=strlen(Text); i<strlen(Text); i++,j++)
final1[i]=final[j];
final1[i]='\0';
return(final);
}
int main()
{
Des d1,d2;
char *str=new char[1000];
char *str1=new char[1000];
cout<<"Enter plaintext : ";
cin >> str;
str1=d1.Encrypt(str);
cout<<"\nCiphertext : "<<str1<<endl;
cout<<"\nDecrypted Text: "<<d2.Decrypt(str1)<<endl;
}

void Des::keygen()
{
PermChoice1();

int i,j,k=0;
for(i=0; i<28; i++)
{
ck[i]=pc1[i];
}
for(i=28; i<56; i++)
{
dk[k]=pc1[i];
k++;
}
int noshift=0,round;
for(round=1; round<=16; round++)
{
if(round==1||round==2||round==9||round==16)
noshift=1;
else
noshift=2;
while(noshift>0)
{
int t;
t=ck[0];
for(i=0; i<28; i++)
ck[i]=ck[i+1];
ck[27]=t;
t=dk[0];
for(i=0; i<28; i++)
dk[i]=dk[i+1];
dk[27]=t;
noshift--;
}
PermChoice2();
for(i=0; i<48; i++)
keyi[round-1][i]=z[i];
}
}