Issue
Here I have a simple code for Encryption & Decryption a File in C as below,
#include <stdio.h>
int Encrypt(char * FILENAME, char * NEW_FILENAME)
{
FILE *inFile; //Declare inFile
FILE *outFile; //Declare outFile
char Byte;
char newByte;
int n;
int i=0;
inFile = fopen(FILENAME,"rb");
outFile = fopen(NEW_FILENAME, "w");
if(inFile == NULL || outfile == NULL){
printf("Error in opening file");
return 1;
} else {
printf("\nFile Opened, Encrypting");
while(1){
while ( !feof( inFile ) ){
Byte=fgetc(inFile);
newByte=Byte+25;
fputc(newByte,outFile);
}
printf("End of File");
break;
}
fclose(inFile);
fclose(outFile);
}
}
int Decrypt (char *FILENAME, char *NEW_FILENAME)
{
FILE *inFile; //Declare inFile
FILE *outFile; //Declare outFile
char Byte;
char newByte;
int i=0;
inFile = fopen(FILENAME,"rb");
outFile = fopen(NEW_FILENAME, "w");
if(inFile == NULL || outfile == NULL){
printf("Error in opening file");
return 1;
} else {
printf("File Opened, Decrypting");
while(1){
printf(".");
while ( !feof( inFile ) ){
Byte=fgetc(inFile);
newByte=Byte-25;
fputc(newByte,outFile);
}
printf("End of File");
break;
}
fclose(inFile);
fclose(outFile);
}
}
int main()
{
char encFile[200];
char newencFile[200];
char decFile[200];
char newdecFile[200];
int choice;
printf("Enter 1 to Encrypt / 2 to Decrypt");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the Source Filename: ");
scanf("%s",encFile);
printf("Enter the Destination Filename: ");
scanf("%s",newencFile);
Encrypt(encFile, newencFile);
break;
case 2:
printf("Enter the Source Filename: ");
scanf("%s",decFile);
printf("Enter the Destination Filename: ");
scanf("%s",newdecFile);
Decrypt(decFile, newdecFile);
break;
}
return 0;
}
This code works but at the end of file it also contain "ÿæ"
such characters so it can't open in default text-editor in linux & becomes useless to store private details. I want to get same to same stuff back in the discript file.
Please help for the same, Thanks in advance.
Solution
Before I explain the solution to the above code, Let me tell you these things first. Beaware of unneccessary defination of variables in your program. I see lots of memory wastage in your code.Everyone can write a program but the one makes it more simpler gets the crown.Remember that when you work on programming.
I see Unneccessary variable i,defination in your Encrypt and Decrypt functions. and There is no need to go for a new byte char defination You can sort off make it as
byte=byte+25 \\ even that makes your work no need of Newbyte char variable.
Erase the int n,int i definations to make your code better.And the most important thing you really got to know is
you cannot use char a;a=getc(Filepointer);
remember the EOF returns -1 when it reaches to the end of file and if your using a char .It doesnot work. Char data type can only handle positive data .Even if you assign -1 to char, it internally saves it as positive number you can check that thing on google.
so never use char data type for checkin EOF use int datatype
and as Ronald mentioned you make it as
while((Byte=getc(Filepointer))!=EOF)
Thats the error you made.Make it as int datatype.And please check the previous questions.Becuase its actually a duplicate question.Some people can Treat as downvote.
The better style for encrypting and decrypting is using bitwise operations rather than addition
Byte=Byte^200; To encrypt \\ just a sample
Byte=Byte^200; To decrypt \\ you can make more complicated
That how you gotta do. It gives you more security compared to adding 25 to your ACSII char.YOu can even encrypt it giving some predefined rules to it.Hope you got the answer and solved you problem.Thank you
Answered By - niko Answer Checked By - Marilyn (WPSolving Volunteer)