I need to make a program that asks the user to input a file name, then a key, then an output file name. I need to use an XOR encryption that encrypts character by character (or by every 100, etc)
What I have so far; it runs and all, but when I encrypt, then decrypt, i dont get the same thing as the original file. What am I doing wrong?
Well you are doing several things... interestingly
First> why in the world are you using char arrays? AND strings?
second>You do know that when you xor each char multiple times with the same char set, you're not doing yourself any good, it's exactly the same as xoring the chars together and then xoring each char with the result...
third>Read my article. You'll see why this form of encryption is completely useless.
What I'd do, if I were you, is stop using character arrays, and come up with a little better encryption algorith. Of course if it's an assignment, it doesn't matter =)
Once you do that, I'll bet you can get it working ;)
1) because im not entirely sure what im doing. At first i went with all stings, but came up with errors; google searches lead me to use some character arrays. Which would be better to use?
2) That confused me a bit, but yeah again im not entirely sure what im doing; my professor is a huge douche and he doesnt even bother teaching the info before assigning the project, so im very lost when it comes to the encryption part.
3) Yeah its an assignment, so i have to use xor...
Im gonna play around with it more later today and hopefully put up an updated version later today.
now if we xor the original data with the final key
1 2 3 4 5
10111100^
01011011
________
11100111 (compare to final encrypted data)
11100111 (final encrypted data)
they're equal.
Soo what I'm saying is rather than encrypting every single byte (string.size) times, just xor the string's individual characters together and encrypt each byte (1) time!!!
I encrypt that again with the same key and get "example ՕKhle"
and when I try to encrypt something with multiple words or lines it gets even more fucked up.
Can someone point out where my errors are? and possibly what i need to do to fix them?
It might be worth noting that using this method will give an obvious indication of what (part by part) of the key is, especially when xoring against a space character. This might not be a problem for your assignment - but it is yet another product of the primitive scheme used here.
I figured as much, but strings seem to give me problems, I put the strings in there, but now my program wont end. it will ask for the users input on input file, key, and output file, then just stay there with nothing happening. (usually it would go to the "system pause" saying "press any key to continue."
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
usingnamespace std;
int main(int argc, char *argv[])
{
ifstream in_stream;
string filein, fileout, key, str((istreambuf_iterator<char>(in_stream)), istreambuf_iterator<char>());
ofstream out_stream;
int e;
cout << "Enter the name of the file\n";
cin >> filein; //User inputs incoming file name
in_stream.open (filein.c_str()); //Open the file
cout << "Enter the encryption key\n";
cin >> key; //User inputs the key
cout << "Enter the name of the outgoing file\n";
cin >> fileout; //User inputs the outgoing file name
out_stream.open (fileout.c_str()); //Opens the file
while(!in_stream.eof())
{
int L = key.size(); //Declaring the size of the key
for(int i=0; i<str.size();i++) //Loop for the encryption
{out_stream << (str[i] ^ key[i%L]);} //XOR encryption
}
in_stream.close (); //close the incoming file
out_stream.close (); //close the outgoing file
system("PAUSE");
return EXIT_SUCCESS;
}
while(!in_stream.eof())
{
int L = key.size(); //Declaring the size of the key
for(int i=0; i<str.size();i++) //Loop for the encryption
{out_stream << (str[i] ^ key[i%L]);} //XOR encryption
}
you never actually read anything in from in_stream here, it's an infinite loop. Get rid of the while loop.
oh i see, but i think im doing something wrong with the encryption
the main file says "example" encrypted with the key "hi" turns into "13179424513" encrypted again with "hi" turns into "8990899481939093938891"
It's possible, it's also possible that the reading in function is failing. When I do my binary file handling I always use vectors, but I'm just crazy...
Post your new code, And I'll actually look at it in my IDE. Right off the bat I can't see the problem.
You're outputting the int value of the chars, static cast to a char to get what you want.
change this
{out_stream << (str[i] ^ key[i%L]);}
to this
{out_stream << char(str[i] ^ key[i%L]);}
Now the problem lies in the way xor returns the value. When you xor 2 char's it returns the ascii value as a result. When we use char() <-[cstyle cast] we temporarily cast the result of what is returned to a char.