Vous êtes sur la page 1sur 3

#include<stdio.

h>
#include<conio.h>
#define row 15
#define column 15
FILE *f1,*f2,*f3;
void main()
{
int n,cnt=0,col;
char ch;
void railenc(int,int);
//clrscr();
printf("Enter the value of key .. no. of rails:");
scanf("%d",&n);
f1=fopen("plain.txt","r");
while((ch=getc(f1))!=EOF)
{
cnt++;
}
printf("%d",cnt);
fclose(f1);
col=cnt/n;
cnt=cnt%n;
if(cnt==0)
{
col=col+n-1;
}
else
{
col++;
col=col+n-1;
}
railenc(n,col);
getch();
}
void railenc(int r,int c)
{
char arr[row][column];
char ch;
int i=0,j=0,k=0,col;
col=c;
//encryption
f1=fopen("plain.txt","r");
while((ch=getc(f1))!=EOF)
{
if(k<c)
{
if(i<r)
{
arr[i][j]=ch;
i++;
j++;
}
else
{
k++;
j=k;
i=0;
arr[i][j]=ch;
i++;
j++;

}
}
}
ch='$';
while(i<r)
{
arr[i][j]=ch;
i++;
j++;
}
f2=fopen("enc.txt","w");
c=c-r+1;
for(i=0;i<r;i++)
{
for(j=i;j<c;j++)
{
putc(arr[i][j],f2);
}
c++;
}
fclose(f2);
//decryption
i=j=0;
c=col;
c=c-r+1;
f2=fopen("enc.txt","r");
while((ch=getc(f2))!=EOF)
{
if(i<r)
{
if(j<c)
{
arr[i][j]=ch;
j++;
}
else
{
c++;
i++;
j=i;
arr[i][j]=ch;
j++;
}
}
}
fclose(f2);
//write a plain text into dec.txt file
i=j=k=0;
printf("\ncolumn=%d",c);
f3=fopen("dec.txt","w");
do
{
if(k<c)
{
if(i<r)
{
if(arr[i][j]=='$')
{
goto stop;
}

else
{
putc(arr[i][j],f3);
i++;
j++;
}
}
else
{
k++;
j=k;
i=0;
if(arr[i][j]=='$')
{
goto stop;
}
else
{
putc(arr[i][j],f3);
i++;
j++;
}
}
}
}while(arr[i][j]!='$');
stop:
fclose(f3);
}

Vous aimerez peut-être aussi