1

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++ ?

jww
  • 97,681
  • 90
  • 411
  • 885
EinderJam
  • 417
  • 1
  • 6
  • 20
  • probably after destructor of *FileSource* executed. – RbMm Aug 06 '17 at 08:49
  • *`ArraySource as(key.begin(), key.size(), true, new FileSink("key.bin"));`* - It looks like the symmetric key is being written to the filesystem in plain text. – jww Aug 06 '17 at 15:11
  • Yes,the symmetric Key was written in plain text,but only for testing purpose,while I was trying to remove the original file – EinderJam Aug 06 '17 at 15:53

1 Answers1

2

I'm not familiar with crypto++ but if they're following the RAII pattern then triggering the ~FileSource destructor should be sufficient to close the handle of the file.

In C++ you would use an anonymous scope to define the lifetime of an automatic variable. Anonymous scopes are defined using curly braces without any keywords:

using namespace std;
...
encryptor.SetKeyWithIV(key, key.size(), iv, iv.size());

// begin an anonymous scope:
{

    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 ) ) );        
}
// end the scope, causing all objects declared within to have their destructors called

const int delete_file = remove("image.jpg");
cout << delete_file << endl;
cout << "Error code is:" << GetLastError();
...

BTW, I noticed you use new without delete. I believe you can make those argument objects also automatic, like so:

using namespace std;
...
encryptor.SetKeyWithIV(key, key.size(), iv, iv.size());

// begin an anonymous scope:
{       
    FileSink                      file            ( "image.jpg.enc" );
    Redirector                    write_redir     ( file );
    ArraySource                   write_iv        ( iv, iv.size(), true, &write_redir );
    AuthenticatedEncryptionFilter filter          ( encryptor, &write_redir )
    FileSource                    write_ciphertext( "image.jpg", true, &filter );
}
// end the scope, causing all objects declared within to have their destructors called

const int delete_file = remove("image.jpg");
cout << delete_file << endl;
cout << "Error code is:" << GetLastError();
...
Dai
  • 141,631
  • 28
  • 261
  • 374
  • It works like a charm ! Thank you so much,I've been searching for a few hours on this problem. – EinderJam Aug 06 '17 at 09:29
  • 1
    *"I noticed you use new without delete. I believe you can make those argument objects also automatic..."* - Not quite correct. The preceding or outer object owns the pointer. The owning object deletes the attached transformation when its no longer needed (like when being destructed). Its an unusual pattern, and its an acquired taste. Also see [Pipelining | Ownership](https://www.cryptopp.com/wiki/Pipelining#Ownership) on the Crypto++ wiki and [Crypto++ explicit destruction during encryption/decryption?](https://stackoverflow.com/questions/42545105) – jww Aug 06 '17 at 15:06