Using a simple functor called Encryptor
struct Encryptor {
char m_bKey;
Encryptor(char bKey) : m_bKey(bKey) {}
char operator()(char bInput) {
return bInput ^ m_bKey++;
}
};
I can easily encrypt this file using
std::ifstream input("in.plain.txt", std::ios::binary);
std::ofstream output("out.encrypted.txt", std::ios::binary);
std::transform(
std::istreambuf_iterator<char>(input),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(output),
Encryptor(0x2a));
however attempting to return this by calling
std::ifstream input2("out.encrypted.txt", std::ios::binary);
std::ofstream output2("out.decrypted.txt", std::ios::binary);
std::transform(
std::istreambuf_iterator<char>(input2),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(output2),
Encryptor(0x2a));
only works partially. Here are the file sizes:
in.plain.txt: 7,700 bytes
out.encrypted.txt: 7,700 bytes
out.decrypted.txt: 4,096 bytes
In this case, it seems that this method works only for the first 2**12bytes and probably only for its multiple (can it be my file system block size?). Why do I have this behavior and what is the workaround for this?
source
share