Pages

IMPLEMENT RAIL FENCE CIPHER

Algorithm:
The Rail Fence cipher is a form of transposition cipher that gets its name from the way in which it is
encoded. In the rail fence cipher, the plaintext is written downwards on successive "rails" of an imaginary
fence, then moving up when we get to the bottom. The message is then read off in rows. For example, using
three "rails" (level=3) and a message of 'SKY IS THE LIMIT’, encipherment would be:
S  Y  S  H  L  M  T
  K  I  T   E  I    I
And would be read as (One rail at a time from top to bottom): SYSHLMTKITEII

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char cipher[10][100],encrypted[100],decrypted[100],plaintext[100]="skyisthelimit" ;
int i,j,level,k=0,l=0;
clrscr();
printf("enter level:");
scanf("%d",&level);

      for(i=0;i<level;i++)
      {
       for(j=0;j<strlen(plaintext);j++)
       {
           cipher[i][j]=' ';
       }
      }
      printf("%d %d",cipher[i][j]);
       for(i=0;i<strlen(plaintext);i++)
       {
       cipher[k%level][l++]=plaintext[i];
       k++;
       }
       for(i=0;i<level;i++)
       {
        cipher[i][l]='\0';
       }
       for(i=0;i<level;i++)
       {
      printf("row %d=%s\n",i,cipher[i]);
       }
       k=0;
       for(i=0;i<level;i++)
       {
      for(j=i;j<strlen(plaintext);j+=level)
      {
          encrypted[k++]=cipher[i][j];
      }
       }
       encrypted[k]='\0';
       printf("encrypted text=%s\n",encrypted);
          k=l=0;
          for(i=0;i<=strlen(plaintext)/level;i++)
          {
          for(j=0;j<level;j++)
          {
              decrypted[l++]=cipher[j][k++];
          }
          }
          decrypted[k]='\0';
          printf("decrypted text=%s",decrypted);

       getch();
}