I am writing a program the will write an encrypted version of a text file(input.txt) to an output file(out.txt). The encryption uses the bit wise XOR operation on the input file and a file containing(keys.txt) two keys with a new line character in between those keys. Everything seems to work right except towards the end of my encrypted output I get a series of unreadable characters.
Input.txt:
"Will you walk a little faster?" said a whiting to a snail, "There's a porpoise close behind us, and he's treading on my tail. See how eagerly the lobsters and the turtles all advance! They are waiting on the shingle-will you come and join the dance?
Keys.txt:
!
M
The code below contains two print statements to test if the code is even getting the text file correctly. I've noticed that the keys are getting grabbed fine however, the input.txt(str) is not grabbing the entire message. Can someone help me? What is the reason for this??
NOTE
My wrong output looks like this:
ov$M!4N8:@!Jm@mM$U9M(+@>U(SrmR,H),:I$U$O*9Nm@mR#@$Ma+mou%D?DjRm@mQ"S=N$R(.M"R(/D%H#EmT> m@#EmI(>9S(@)H#FmN# Xm+mmmmU,H!Gmr(DmI"VmD,F(S!XmU%DmM"C>U(S>,O)9I(9T?U!D>,M!,E;@#B(
The correct output should be:
m@#EmI(>9S(@)H#FmN# XGmmmmU,H!Gmr(DmI"VmD,F(S!XmU%DmM"C>U(S>,O)9I(9T?U!D>,M!,E;@#B(Gmu%D4,S(:@$U$O*"OmU%DmR%H#F!D`V$M!4N8.N Dm@#EmK"H#9I(+mmmm)@#B(f
The program to convert to hexadecimal (xxd myProgram.c) is used to clarify.
User should type:
gcc myProgram.c
./a.out e input.txt keys.txt
('e' stands for encrypt)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int args, char *argc[]){
int i=0;
int j=0;
int len=0;
char str[501];
char str2[2];
char c;
FILE *finp;
FILE *keyFile;
FILE *fout;
if ( strcmp(argc[1], "e") == 0 )
{
if ( (finp = fopen(argc[2],"r")) == NULL )
{
printf("Could Not Open file %s\n", argc[2]);
exit(1);
}
if ( (keyFile = fopen(argc[3],"r")) == NULL )
{
printf("Could Not Open file %s\n", argc[3]);
exit(1);
}
while((c = fgetc(finp))!=EOF)
{
str[j++] = c;
}
while((c = fgetc(keyFile)) != EOF)
{
str2[i++] = c;
}
/*Print Statement to test the keys*/
// printf("%c%c", str2[0], str2[2]);
/*Print Statement to test the input*/
// printf("%s\n", str);
/* *** START CODE THAT USES INPUT.TXT FILE and KEYS.TXT *** */
len = strlen(str);
for(i=0;i<len;i++)
{
str[i]^=str2[2];
str[++i]^=str2[0];
}
fout=fopen("out.txt","w");
if(fout==NULL)
{
printf("ERROR");
exit(1);
}
fprintf(fout, "%s", str);
fclose(finp);
return 0;
} else {
printf("SORRY!");
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *args[]){
FILE *finp, *keyFile, *fout;
int i, ch;
char key[2];
if(argc == 4 && strcmp(args[1], "e") == 0){
//Read two key character
if ((keyFile = fopen(args[3], "r")) == NULL){
printf("Could Not Open file %s\n", args[3]);
exit(1);
}
if(1!=fscanf(keyFile, " %c", &key[0]) || 1!=fscanf(keyFile, " %c", &key[1])){
printf("Could Not Read Key properly.\n");
exit(1);
}
fclose(keyFile);
if((finp = fopen(args[2], "rb")) == NULL){
printf("Could Not Open file %s\n", args[2]);
exit(1);
}
if((fout = fopen("out.dat", "wb")) == NULL){
printf("Could Not Open file out.dat\n");
exit(1);
}
i = 1;
while((ch = fgetc(finp))!=EOF){
fputc(ch ^ key[i], fout);
i = !i;
}
fclose(fout);
fclose(finp);
} else {
printf("SORRY!\n");
}
return 0;
}