#include <stdlib.h>

/*-------------------------------------------------------------------------
You need to call the following two procedures, encryption E(), 
and decryption D() in your program.  Here is the description of 
the parameters:


void E ( int N, int M,
         unsigned char k[],
         unsigned char m[],
         unsigned char c[] );
void D ( int N, int M,
         unsigned char k[],
         unsigned char m[],
         unsigned char c[] );

Parameters:
N: The length of the key.
M: The length of the message and encrypted message.
k: The key in bit representation. If the key is 0010, then
    N==4 and   k[0]==0,  k[1]==0,  k[2]==1,   k[3]==0.
m: The message in bit representation. Note that  m[0] is the first bit.
   After the routine D returned, the decrypted message will be stored in  m.
c: The encrypted message in bit representation.
   Note that  c[0] is the first bit. After the routine E returned,
   the encrypted message will be stored in c.

Usage:
See the main program for an example on how the procedures are called.

----------------------------------------------------------------------------*/



/*-------------- Include the following in your program         -------------*/
void Enc( 
         int           MessageLength,
         unsigned char K[], 
         unsigned char m[], 
         unsigned char c[] 
        )
{
  unsigned char S[256];  
   {
     int i,j;
     for (i=0; i<256;i++) S[i]=i;
     j=0;
     for (i=0; i<256; i++)
     {
        int temp;
        j    =  (j+  S[i] + K [ i % 4 ] )%256;
        temp = S[j]; S[j] = S[i]; S[i]=temp;
     }
   }
  {
     int i,j,t;
     i=0; j=0; 

     for (t=0; t< MessageLength; t++)
     {
        unsigned char temp,b;
        i = (i+1) % 256;
        j = (j+S[i]) %256;
        temp= S[i]; S[i]= S[j]; S[j]= temp;

        b = ( S[  (S[i]+ S[j]) %256 ] );
        c[t]= ( m[t]^b )&1;
     }
   }
}

void E( int N, int M, unsigned char k[], unsigned char m[], unsigned char c[] )
{

int           i,ctr1,ctr2;
unsigned char temp;
unsigned char key[4];

 ctr1 = 3;  ctr2 = 1; temp = 1;
 for (i=0; i<= 3; i++)  key[i]=0;
 
 for (i=0; i<N; i++)
   {
         if (k[i]==1) 
            { key[ctr1] = (key[ctr1] | temp ); }
         if (ctr2==8)
           { ctr2=1; temp=1; ctr1=ctr1-1; }
         else
           { ctr2=ctr2+1; temp=temp*2; }
    }

 Enc ( M, key, m, c ); 

}

void D( int N, int M, unsigned char k[], unsigned char m[], unsigned char c[] )
{
  E(N,M,k,c,m);
}

/*------------- The above is to be included in your program -----------*/



/*----      Below is an example, showing how to encrypt a message twice.--*/


int main()
{
  int i;

  unsigned char k1[10];
  unsigned char k2[10];
  unsigned char m [30];
  unsigned char c1[30];
  unsigned char c2[30];

  /*      First Key    */
  k1[0]=1;
  k1[1]=0;
  k1[2]=0;
  k1[3]=0;
  k1[4]=0;
  k1[5]=0;
  k1[6]=0;
  k1[7]=0;
  k1[8]=0;
  k1[9]=0;

  /*     Second Key    */
  k2[0]=0;
  k2[1]=1;
  k2[2]=0;
  k2[3]=0;
  k2[4]=0;
  k2[5]=0;
  k2[6]=0;
  k2[7]=0;
  k2[8]=0;
  k2[9]=0;

  /*     The message is all zeros   */
  for (i=0;i<30; i++) {  m[i]=0;}
  
  /*     Encryption                 */

  E(10,30,k1,m, c1);
  E(10,30,k2,c1,c2);


  /*   output the result           */ 

  printf ("The original  message is :" );
  for (i=0;i<30;i++) printf( "%d", m[i] );  printf("\n");

  printf ("The encrypted message is :" );
  for (i=0;i<30;i++) printf( "%d", c2[i]);  printf("\n");

  printf ("The first     key     is :" );
  for (i=0;i<10;i++) printf( "%d", k1[i] ); printf("\n");

  printf ("The second    key     is :" );
  for (i=0;i<10;i++) printf( "%d", k2[i] ); printf("\n");
}
