The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Saturday, November 10, 2012

implement S-AES BLOCK cipher encryption and decryption technique

#include<stdio.h>
#include<conio.h>
#include<string.h>

unsigned char mssg[100];
unsigned char ch[2];
unsigned char rcon[] = {0x80,0x30};
int ind;
int k[3];

unsigned char nib[2][2];

unsigned char M[][2] = { {0x1,0x4},
                        {0x4,0x1}};

unsigned char IM[][2] = { {0x9,0x2},
                          {0x2,0x9}};

unsigned char SBox[][4] = { {0x9,0x4,0xA,0xB},
                           {0xD,0x1,0x8,0x5},
                           {0x6,0x2,0x0,0x3},
                           {0xC,0xE,0xF,0x7}};

unsigned char ISBox[][4] = { {0xA,0x5,0x9,0xB},
                            {0x1,0x7,0x8,0xF},
                            {0x6,0x0,0x2,0x3},
                            {0xC,0x4,0xD,0xE}};
               
                              
void get_mssg()
{
   printf("\n Enter the Plaintext : ");
   gets((char *)mssg);
}

void print_nib()
{
  printf("\n %x %x",nib[0][0],nib[0][1]);
  printf("\n %x %x",nib[1][0],nib[1][1]);  
}

void Nib_Sub(int ind)
{
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
    {
      if(ind == 0)
        nib[i][j] = SBox[(nib[i][j] & 0xC)>>2][nib[i][j] & 0x3];
      else
        nib[i][j] = ISBox[(nib[i][j] & 0xC)>>2][nib[i][j] & 0x3];            
    }      
  } 
}

void Shift_Row()
{
  unsigned char tmp;
  tmp = nib[1][0];
  nib[1][0] = nib[1][1];
  nib[1][1] = tmp;     
}

void Mix_Col(int ind)
{
  unsigned char a,b;
  a = (ind == 0)? 0x1 : 0x9; 
  b = (ind == 0)? 0x4 : 0x2; 
 




  unsigned char tmp;
  tmp = nib[0][0];
  nib[0][0] = (a*tmp) xor (b*nib[1][0]);
  nib[1][0] = (b*tmp) xor (a*nib[1][0]);

  tmp = nib[0][1];
  nib[0][1] = (a*tmp) xor (b*nib[1][1]);
  nib[1][1] = (b*tmp) xor (a*nib[1][1]); 
}

void Make_Key(int& i)
{
  unsigned char cho[2],tcho;
 
  cho[0] = (k[i] & 0xFF00) >> 8;
  cho[1] = k[i] & 0x00FF;
 
  tcho = (SBox[(cho[1]&0xC0)>>6][(cho[1]&0x30)>>4]) |
((SBox[(cho[1]&0x0C)>>2][cho[1]&0x03])<< 4);
 
  cho[0] = cho[0] xor rcon[i] xor tcho;
  cho[1] = cho[0] xor cho[1];
 
  k[++i] = (cho[0]<<8) | cho[1]; 
}


void Add_Key(int i)
{
  nib[0][0] = nib[0][0] xor ((k[i] & 0xF000)>>12);
  nib[1][0] = nib[1][0] xor ((k[i] & 0x0F00)>>8);
  nib[0][1] = nib[0][1] xor ((k[i] & 0x00F0)>>4);
  nib[1][1] = nib[1][1] xor (k[i] & 0x000F);  
}

void enc_S_AES()
{
   int i = 0;

   nib[0][0] = (ch[0] & 0xF0)>>4;
   nib[1][0] = ch[0] & 0x0F;
   nib[0][1] = (ch[1] & 0xF0)>>4;
   nib[1][1] = ch[1] & 0x0F;
 
   k[0] = 0x2D55;
 
   Add_Key(i);
   while(i<2)
   {
     Shift_Row();
     Nib_Sub(0);   
     Mix_Col(0);
     Make_Key(i);
     Add_Key(i);        
   }
   
   ch[0] = (nib[0][0] << 4)| nib[1][0];
   ch[1] = (nib[0][1] << 4)| nib[1][1]; 
}

void dec_S_AES()
{
   int i = 2;
 
   nib[0][0] = (ch[0] & 0xF0)>>4;
   nib[1][0] = ch[0] & 0x0F;
   nib[0][1] = (ch[1] & 0xF0)>>4;
   nib[1][1] = ch[1] & 0x0F;
  
   Add_Key(i);
 
   while(i>0)
   {
     Nib_Sub(1);
     Shift_Row();
     Add_Key(--i);
     Mix_Col(1);             
   }
   


   ch[0] = (nib[0][0] << 4)| nib[1][0];
   ch[1] = (nib[0][1] << 4)| nib[1][1]; 
}

void en_mssg()
{
  int i = 0;
  int j = strlen((char *)mssg);
  ind = 0;

  while(i < j)
  {        
    ch[0] = mssg[i];

    if(mssg[i+1] == '\0')
    {
      ind = 1;
      ch[1] = ((mssg[i]- 65 + 12)% 26)+65;           
    }
    else
     ch[1] = mssg[i+1];
    enc_S_AES();
  
    mssg[i] = ch[0];
    mssg[i+1] = ch[1];          
    i += 2;
  }

  if(i > j)
   mssg[i] = '\0';
 
  printf("\n The Ciphertext is : ");
  puts((char *)mssg);
}

void de_mssg()
{
  int i = 0;
  int j = strlen((char *)mssg);
 
  while(i < j)
  {        
    ch[0] = mssg[i];
    ch[1] = mssg[i+1];
  
    dec_S_AES();
  
    mssg[i] = ch[0];
    mssg[i+1] = ch[1];          
    i += 2;
  }

  if(ind == 1)
   mssg[i-1] = '\0';
 
  printf("\n The Decrypted Plaintext is : ");
  puts((char *)mssg);
}

int main()
{
  get_mssg();
  en_mssg();
  de_mssg();
  getch();
  return 0;
}

No comments:

Post a Comment