Algorithm/Steps :
- Take plain text from file named input.txt
- Take input Mapping string.
- Read text from file (Character By Character) then encrypt the text using Mapping String.
- Save Encrypted text in input.txt
- Use same method and save decrypted text in output.txt
Code :
Encryption
#include<stdio.h>
#include<string.h>
int
main()
{
char plain[50],ci[50];
FILE *fp = fopen("input.txt","r");
FILE *fp1 = fopen("output.txt","w");
fscanf(fp,
"%s", plain);
printf("\nPlain
Text is:%s",plain);
int len = strlen(plain);
char plain1[]="abcdefghijklmnopqrstuvwxyz";
char
cipher[]="ynlkxbshmiwdpjroqvfeaugtzc";
int z,j=0,i=0;
char ch;
while(plain[i]!='\0')
{
ch=plain[i];
for(j=0;j<26;j++)
{
if(plain[i]
== plain1[j])
{
z=j;
break;
}
}
ci[i]=cipher[z];
i++;
}
printf("\nCipher
Text is:");
for(i=0;i!= len;i++)
{
printf("%c",ci[i]);
fprintf(fp1,"%c",
ci[i]);
}
return
0;
}
Decryption
#include<stdio.h>
#include<string.h>
int
main()
{
char ci[50],plain[50];
FILE *fp1 = fopen("output.txt","r");
fscanf(fp1,
"%s", ci);
printf("\nEncrpted
Text is:%s",ci);
int len = strlen(ci);
char
plain1[]="abcdefghijklmnopqrstuvwxyz";
char
cipher[]="ynlkxbshmiwdpjroqvfeaugtzc";
int z,j=0,i=0;
char ch;
while(ci[i]!='\0')
{
ch=ci[i];
for(j=0;j<26;j++)
{
if(ci[i]
== cipher[j])
{
z=j;
break;
}
}
plain[i]=plain1[z];
i++;
}
printf("\nDecryption
is:");
for(i=0;i!= len;i++)
{
printf("%c",plain[i]);
}
return 0;
}
No comments:
Post a Comment