I'm trying to encrypt a file using AES EAX mode and CryptoPP library. Here is the main() content:
SecByteBlock key(AES::MAX_KEYLENGTH);
rnd.GenerateBlock(key, key.size());
ArraySource as(key.begin(), key.size(), true, new FileSink("key.bin"));
SecByteBlock iv(AES::BLOCKSIZE);
rnd.GenerateBlock(iv, AES::BLOCKSIZE);
EAX<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key, key.size(), iv, iv.size());
FileSink file("image.jpg.enc");
ArraySource write_iv(iv, iv.size(), true, new Redirector(file));
FileSource write_ciphertext("image.jpg", true, new AuthenticatedEncryptionFilter(encryptor, new Redirector(file)));
const int delete_file = std::remove("image.jpg");
std::cout << delete_file << std::endl;
std::cout << "Error code is:" << GetLastError();
return 0;
The encryption part ends successfully,however,removing the original file (image.jpg) fails.The output I get is:
Error code is:32
Which is an ERROR_SHARING_VIOLATION
, meaning that "The process cannot access the file because it is being used by another process."
My question is : How can I close the file after the Filesource
line,to be able to delete the file after ? With a classic ifstream
,it would be file.close()
, but how can i do it with Crypto++ ?