I have what seems to be a fairly simple problem, but I've no clue how to fix it. Basically I am learning about encryption, etc.. and made a basic encryption program which works fairly well for a first try except when you decrypt the ciphertext you end up with a few little bits left over. here is the code and before/after:
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 4)
{
cout << "Usage: xorcrypt \"inputfile\" \"outputfile\" \"keyfile\"" << endl;
exit(-1);
}
ifstream input, key;
ofstream output;
input.open(argv[1],ios::binary);
output.open(argv[2],ios::binary);
key.open(argv[3],ios::binary);
key >> skipws;
input >> skipws;
if(input==NULL)
{
cout << "Error opening inputfile." << endl;
exit(1);
}
if(output==NULL)
{
cout << "Error opening outputfile." << endl;
exit(1);
}
if(key==NULL)
{
cout << "Error opening keyfile." << endl;
exit(1);
}
while(input.eof() != true)
{
output.put(input.get() ^ key.get());
if(key.eof() && input.eof() == false)
key.seekg(ios::beg);
}
input.close();
output.close();
key.close();
return 0;
}
That is my program in its entirety, it reads in a file to be encrypted, an output file, and a file to be used as a key.
Now when I encrypt and decrypt the actual source (XOR.cpp) I end up with this:
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 4)
{
cout << "Usage: xorcrypt \"inputfile\" \"outputfile\" \"keyfile\"" << endl;
exit(-1);
}
ifstream input, key;
ofstream output;
input.open(argv[1],ios::binary);
output.open(argv[2],ios::binary);
key.open(argv[3],ios::binary);
key >> skipws;
input >> skipws;
if(input==NULL)
{
cout << "Error opening inputfile." << endl;
exit(1);
}
if(output==NULL)
{
cout << "Error opening outputfile." << endl;
exit(1);
}
if(key==NULL)
{
cout << "Error opening keyfile." << endl;
exit(1);
}
while(input.eof() != true)
{
output.put(input.get() ^ key.get());
if(key.eof() && input.eof() == false)
key.seekg(ios::beg);
}
input.close();
output.close();
key.close();
return 0;
}
ÿ
Notice the "ÿ" right at the end. I am getting this problem regaurdless of platform. I compiled with g++ on a linux box and in VS2008/Win7. While I really am just doing this for fun, any further need for encryption could probably get skewed pretty bad in a large scope with little things like this. After all, we have all spent several hours searching for the one semicolon we forgot. Any ideas on how to fix this would be appreciated.
EDIT: Seems a bit vague as I read it, just to clarify, I compiled the source and then used the XOR.cpp file as an actual argument, e.g. I entered xorcrypt XOR.cpp of.txt key.txt at the command line