0

I need to do XOR file encryption (in any format), for this I must read from the file one by one group of bytes, encrypt them, and then write the result to the output file.

How can I organize reading and writing bytes?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 3
    Are you asking just how to read and write from a file? You might start with a good book or tutorial on C++. [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – crashmstr Jul 28 '14 at 12:57

1 Answers1

0

You could read one by one but i recommend read and write block of byte. Performance wise is much better accessing the file as little as needed, it's and slow operation. According to the memory constrain you could increase the block size of the second sample code (actual 1024, to the number of byte you want).

Sample code, one-by-one encryption:

#include <iostream>
#include <fstream>
#include <vector>

int main(int argc, char* argv[]) {
    char bytes;
    std::ifstream ifs("ficheroentrada", std::ios_base::binary);
    std::ofstream ofs("ficherosalida", std::ios_base::binary);
    while (!ifs.eof()) {
        ifs.get(bytes);
        bytes ^= 0xAA; // xor constant or variable
        ofs.put(bytes);
    }
    ifs.close();
    ofs.close();

    return 0;
}

Code for Block encryption using XOR (This one is more recommended)

#include <iostream>
#include <fstream>
#include <vector>

int main(int argc, char* argv[]) {
    std::vector<char> bytes(1024); // change the block size
    std::ifstream ifs("e:\\save_out.txt", std::ios_base::binary);
    std::ofstream ofs("e:\\save_out2.txt", std::ios_base::binary);
    while (!ifs.eof()) {
        ifs.read((char*)&bytes[0], bytes.size());
        unsigned int size_read = ifs.gcount();
        for (unsigned int idx = 0; idx < size_read; idx++) {
            bytes[idx] ^= 0xAA; // xor constant or variable
        }
        ofs.write((char*)&bytes[0], size_read);
    }
    ifs.close();
    ofs.close();

    return 0;
}
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
  • 1
    First, the stream will buffer, even if you read one character at a time from it. It might be slightly faster reading by blocks, but probably not significantly so. Just using `ifs.get()` and `ofs.put()` won't be significantly slower. And of course, using `ifs.eof()` as the loop condition is wrong; in your first example, it will result in reading one extra byte. (Also, there's no point in the `close` unless you check the status afterwards.) – James Kanze Jul 28 '14 at 13:22
  • Yes, it's true the stream will buffer, but there is no control in the size of the buffer and if the buffer would be affected or not by other programs. Thank for the _get and put_ (updated), the _close_ calls it's true that there are not necessary, but i like to called explicitly, there is any opposite reason, not to called?. – NetVipeC Jul 28 '14 at 13:42
  • @NetVipeC Normally, it's best to leave the buffering up to the stream; the implementation _should_ have chosen the optimal values for the platform. – James Kanze Jul 28 '14 at 14:00