So, I've been working on this object-oriented xor file encryptor of mine (Yeah, I know, what's the point), and I've come to an almost-done roadblock. It seems that for text and bitmap files my algorith functions correctly. However, ecrypting and then decrypting a wav file yields a "wrong format" erro when I try to play it in winamp. Obviously, I messed up some bits. Unfortunately, I don't know which bits or why. I've included excerpts from functions that encode and decode files. Yes, I'm using ios::binary.
Encoding:
Code:
//The actual encoding (writing)
unsigned long int counter = 0;
writer.open(encoded_path.c_str(),ios::binary);
if(!writer) return CRYPT_FAIL;
writer.write("XOR_CODEC_ENCRYPTED_FILE@",25); //write branding
writer.write(orig_name.c_str(),orig_name.size());//write original file name
writer.put('@');//separation character
for(int y = 0; y < password.size(); y++) writer.put( (password[y] ^ PASSWORD_KEY) );
writer.put('@'); //separation character
writer.write(key.c_str(),key.size()); //write key
writer.put('@'); //separation character
char in_byte = '\0';
cout << "Encrypting..." << endl;
while( !((reader.get(in_byte)).eof()) ) // while get() does not set eof bit (get() into in_byte)
{
//ok, so this reads through input (with get) and writes char by char (using put)
// the xor-ed data. Modulus is used so counter can increase
// to whatever, while for key[index], index { [0,255]
// Thus, the key is effectively cycled through continuously
writer.put( static_cast<char>(in_byte ^ key[counter % KEY_SIZE]) );
counter++;
}
cout << "Encryption complete." << endl;
reader.clear();//eof
reader.seekg(0,ios::beg);//rewind
//Note: as currently written, the encoder has a 4,294,967,295 byte limit ( max_val(unsigned long int) )
encoded = true;
Now, when the decoder is called, the seek pointer on the encoded file is already past my little header, so (theoretically) only the original contents of the file are written):
Code:
unsigned long int counter = 0;
char in_byte = '\0';
cout << "Decrypting..." << endl;
while( !((reader.get(in_byte)).eof()) ) // while get() does not set eof bit (get() into in_byte)
{
//ok, so this reads through input (with get) and writes char by char (using put)
// the un-xor-ed data. Modulus is used so counter can increase
// to whatever, while for key[index], index { [0,255]
// Thus, the key is effectively cycled through continuously
writer.put( static_cast<char>(in_byte ^ key[counter % KEY_SIZE]) );
counter++;
}
cout << "Decryption complete." << endl;
reader.clear();//eof
reader.seekg(0,ios::beg);//rewind
PS: I apologize in advance for using eof() a loop conditional, but I don't think that's the problem (correct me if I'm wrong).