I tried to encrypt image file with c++ with XOR encryption . it worked well but when i decrypt it with the same code i can't open the image , i assume they are not the same but in XOR encryption they must be the same
here is the code
void xor_encrypt(std::string const& path) {
char key[65] = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
std::ifstream input{ path,std::ios::binary };
std::ofstream output;
output = std::ofstream{ path + ".hafnium", /*added*/ std::ios::binary };
char buffer[64];
while (input.read(buffer, 64)) {
for (size_t i = 0; i < 64; i++) {
buffer[i] ^= key[i];
}
output.write(buffer,/*added*/ input.gcount());
}
input.close();
std::remove(path.c_str());
}
and here the 3 files : original image , encrypted image and encrypted 2 times image
[1] original image : https://i.stack.imgur.com/VKHKU.jpg [2] image encrypted 1 time : https://mega.nz/file/BUMG2I7K#G3PsUeYCwtTCOj2cYSH47t67_WafRZQsRHyIims-EW0 [3] image encrypted 2 times with xor : https://mega.nz/file/FUMywI5I#mvI6Ge2nEw19fDfTEVso7hKMFSggRJcGJ7_g9178LMQ
the only difference between files i see is that they hasn't the same size
why image encrypted 2 times with xor don't yield the same file ? thanks for answers
EDIT : thanks for answers , it worked well for the image file but when i take other file like a video , it don't work anymore :
original video : https://mega.nz/file/BYU3hQJI#JugOnHZ6_ajnRqHqc18j_j54MqFoIXAUHITSMxbAo48 encrypted video : https://mega.nz/file/JAMFXCRK#DxYqKAvCqda18oC47qOH0Wiec1bmJ7hSlmypczS9LXE encrypted 2 times video : https://mega.nz/file/VVUT1KiA#f0vf43PkEssAmoHuPIcY722kd1p7nvQheIlIwFkmrzk