Reputation: 183
So I'm trying to making a file encrypting program in C (by the way I'm kind of new to C) so I wrote this simple XOR file encryption code:
#include <stdio.h>
#include <string.h>
int main()
{
char fileName1[35] = {'\0'}; //Name of original file
char fileName2[35] = {'\0'}; //Name of encrypted file
char keyString[20] = {'\0'}; //Key determines result encryption
FILE* originalFile; //File to be encrypted
FILE* cryptedFile; //The encrypted file
int c; //byte read from original file
printf("Enter file location followed by name of the file you want to encrypt: ");
scanf("%s", fileName1);
printf("Enter file location followed by name of the encrypted file: ");
scanf("%s", fileName2);
printf("Enter your key (Encryption changes based on Key): ");
scanf("%s", keyString);
originalFile = fopen(fileName1, "rb"); //rb to read file bytes
cryptedFile = fopen(fileName2, "wb"); //wb to write bytes to file
if(originalFile != NULL && cryptedFile != NULL){
while( (c = getc(originalFile)) != EOF ){
int x;
for(x=0; x<strlen(keyString); x++){
c ^= keyString[x];
putc(c, cryptedFile);
}
}
fclose(originalFile);
fclose(cryptedFile);
}
return 0;
}
So to test this program I created a file called file1.txt and run the encryption program giving it the second file as file2.txt and key as secret. Then I ran the program again but this time on the encrypted file2.txt and created a file3.txt with the same key secret. Since it was the same key the file3.txt should be the same as file1.txt but file3.txt has random contents in it. So what am I doing wrong?
Upvotes: 2
Views: 656
Reputation: 10541
Your encoding scheme should be corrected a bit. You are xoring every character you read with every character from the key, which doesn't produce the desired effect. You can even notice, that your encrypted file is bigger than the original one, while they should be the same using this encryption scheme.
What you need is to xor one character from the original text with one character from the key. You can fix it in the following manner (for example):
if(originalFile != NULL && cryptedFile != NULL){
int x = 0;
int keyStringLength = strlen(keyString);
while( (c = getc(originalFile)) != EOF ){
c ^= keyString[x];
putc(c, cryptedFile);
++x;
if (x >= keyStringLength) {
x = 0;
}
}
fclose(originalFile);
fclose(cryptedFile);
}
Upvotes: 0
Reputation: 409136
If you compare the file sizes of the input and output files you will see that the output files are getting larger and larger. This is because for each byte you read, you write one for each letter in the password. This means that the output file will be six times larger than the input file if your password is "secret"
.
You need to come up with an algorithm that takes the password and combines all letters of it into a single value, that you can use for the XOR operation. This algorithm is what is commonly known as hash function.
Upvotes: 0
Reputation: 15036
int x = 0, keyLen = strlen(keyString);
while( (c = getc(originalFile)) != EOF ){
c ^= keyString[x++%keyLen];
putc(c, cryptedFile);
}
Upvotes: 0
Reputation: 5105
Your program outputs too much data; try checking the sizes of file1.txt, file2.txt and file3.txt. The problem in is this section:
while( (c = getc(originalFile)) != EOF ){
int x;
for(x=0; x<strlen(keyString); x++){
c ^= keyString[x];
putc(c, cryptedFile);
}
}
There's two nested loops, so the whole inner loop is executed for each input character. Try a new c = getc()
inside the inner loop and break out of both loops if EOF is reached, or flatten the loops into one, using a c ^= keyString[(x++) % strlen(keyString)];
.
Upvotes: 4
Reputation: 573
If Im understanding your problem; You want to encrypt a file, by giving it an output file to write to plus a key
Now when you run the first file, it delivers an output which is supposedly correct. Now you are saying that if you encrypt the the encrypted file it should give the same result?
Well it shouldnt, try reversing the process and see if by decrypting the last file twice, you should end up with the original text.
Upvotes: 2