1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
void crypt(const char *text, const int length, const char *key, char *crypted) {
int j = 0;
for (int i = 0; i<length; i++) {
if (j >= (signed) strlen(key)) {
j=0;
}
crypted[i] = text[i]^key[j];
j++;
}
}
int main(int argc, char *argv[])
{
char *key = "9cdfb439c7876e703e307864c9167a15";
char *filename_in = "image-tocrypt.jpg";
char *filename_out = "image-out.jpg";
fstream file_in(filename_in, ios::in|ios::binary);
fstream file_out(filename_out, ios::out|ios::binary);
if (!file_in || !file_out) {
cout << "Impossible d'ouvrir !";
return 0;
}
int iter = 0;
const int tranche = 192;
char buffer[tranche];
// Determine la longueur du fichier
file_in.seekg(0, ios::end);
int file_inlength = file_in.tellg();
file_in.seekg(0, ios::beg);
while (iter*tranche < file_inlength) {
int readByte;
readByte = tranche;
if (iter*tranche+tranche > file_inlength)
readByte = file_inlength-iter*tranche;
strcpy(buffer, "");
file_in.read(buffer, readByte);
char tmp[tranche];
crypt(buffer, readByte, key, tmp);
strcpy(buffer, tmp);
file_out.write(buffer, readByte);
iter++;
}
file_in.close();
file_out.close();
cout << "\nFini\n";
system("PAUSE");
return 0;
} |
Partager