Algorithm/Steps :
- Take plain text from file named input.txt
- Take Key value from the user.
- Read text from file (Character By Character) then encrypt the text using Caesar Cipher.
- Save Encrypted text in input.txt
- Use same method and save decrypted text in output.txt
Code :
Encryption
#include<stdio.h>
int
main()
{
int key,i=0;
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);
printf("\nEnter Key:");
scanf("%d",&key);
while(plain[i]!='\0')
{
if(plain[i] >= 97
&& plain[i]<= 122 )
ci[i] =
((plain[i] + key-97) % 26) + 97 ;
else
ci[i] =
((plain[i] + key-65) % 26) + 65 ;
i++;
}
ci[i]='\0';
i=0;
printf("Encryption is:");
while(ci[i]!='\0')
{
printf("%c",ci[i]);
i++;
}
fprintf(fp1, "%s", ci);
}
Decryption
#include<stdio.h>
int
main()
{
int key,i=0;
char ci[50];
FILE *fp1 = fopen("output.txt","r");
fscanf(fp1, "%s",ci);
printf("\nEnter Key:");
scanf("%d",&key);
printf("\nDecryption
is:");
while(ci[i]!='\0')
{
if(ci[i] >= 97
&& ci[i]<= 122 )
printf("%c",((((ci[i]
- key)-97+26) % 26) + 97 ));
else
printf("%c",((((ci[i]
- key)-65+26) % 26) + 65 ));
i++;
}
}
No comments:
Post a Comment