@vvtvvtvvt , it all depends on the encryption method. For most cases, the method proposed by @KoVadim is absolutely suitable. Or you can not use buffering explicitly, it is already “embedded” in the library functions for working with files.
A simple example of such a program in C. (You can train yourself and translate yourself to C ++ streams).
// Простое потоковое шифрование файла любой длины на том же месте // ./a.out filename [password] // для расшифровки запустить еще раз с тем же password #include <stdio.h> #include <string.h> int main (int ac, char *av[]) { if (ac < 2) { fprintf (stderr, "Usage: %s filename [password]\n", av[0]); return 1; } char *fname, *pass; fname = av[1]; pass = av[2] ? av[2] : fname; FILE *in = fopen(fname, "r"), *out = fopen(fname, "r+"); // чтение и запись БЕЗ УРЕЗАНИЯ файла if (!in || !out) { perror(fname); return 2; } rewind(out); // требуется для перевода файла в режим записи int c, i = 0, l = strlen(pass); while ((c = fgetc(in)) != EOF) { c ^= pass[i]; fputc(c, out); i = (++i) % l; // циклически перебираем символы pass } return fclose(out) + fclose(in); }
By the way, g ++ also compiles it.
If you wish, you can complicate (somewhat strengthen) such an algorithm. For example, you can calculate HASH (pass) and encrypt it by recalculating HASH (previous HASH) each time you read N characters (say, HASH size).