To implement Rail-Fence cipher encryption- decryption in C. (WITH OUTPUT IMAGE)



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

void main()
{
int i,j,count,plen,devide,clen;
char p1[2][50],p2[2][50],p[100],e[100],d[100];
clrscr();

printf("Enter plaintext::::::");
gets(p);

plen=strlen(p);
devide=plen/2;

puts(p);

count=0;
for(i=0;i<devide+1;i++)
{
  for(j=0;j<2;j++)
  {  if(p[count]!='\0')
     {p1[j][i]=p[count];
     count++;
     }
     else
     p1[j][i]='\0';
  }
}

p1[j][i]='\0';


for(i=0;i<2;i++)
{printf("\n");
for(j=0;j<devide+1;j++)
  {
   printf("%c",p1[i][j]);
  }
}

count=0;
for(i=0;i<2;i++)
{printf("\n");
for(j=0;j<devide+1;j++)
  { if(p1[i][j]!='\0')
   e[count++]=p1[i][j];
  }
}
e[count]='\0';

printf("\n ENCRYPTED TEXT:::::::::::::::::::::::::::::::::::");
for(j=0;j<count;j++)
printf("%c",e[j]);

//Decryption part:::::::::::::::::::::::::::::::::::::::::::::::::::::::

clen=strlen(e);

count=0;
for(i=0;i<2;i++)
{printf("\n");
 for(j=0;j<(clen+1)/2;j++)
 {if(e[count]!='\0')
  p2[i][j]=e[count++];
 }
}

count=0;
for(j=0;j<(clen+1)/2;j++)
{
  for(i=0;i<2;i++)
  { if(p2[i][j]!='\0')
   d[count++]=p2[i][j];
  }
}

d[count]='\0';

printf("\n DERYPTED TEXT::::::::::::::::::::::::::::::::::::::::::::");
for(i=0;i<count;i++)
printf("%c",d[i]);


getch();
}
OUTPUT:

Comments