Qt C++ - How to protect a file

Viewed 961

My applications generates a .zip containing important files and I don't want that the end user can get access to those data. To compress I use the QuaZIP library.

My first idea was to set a password to the zip file but it seems complicated and not very secure but if you have a solution it will be enough for my need.

Otherwise do you have any idea to easily secure a file? Can I encrypt it?

Thanks a lot. I'm Qt 5.11 and MSVC2017.

1 Answers

The easiest protection: just change the file extension (from .zip to .bin for example). For example using QFile::rename():

QFile::rename("your_file.zip", "your_file.bin");

Another way:
Read all the data from the file and encrypt it. You can use SimpleCrypt:

SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f023)); //some random number. This is your encryption key
...

QByteArray buffer = file.readAll();

QByteArray encryptedBuffer = crypto.encryptToByteArray(buffer.data());

// Write the encrypted data to a new file
QFile newFile("new_file.bin");
...
newFile.write(encryptedBuffer);

Then you can send this newFile.

On the recipient side, read all the data to a buffer from the file you received:

QByteArray encryptedBuffer = receivedFile.readAll();

SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f023)); //same random number: key should match encryption key

QByteArray yourDecryptedData = crypto.decryptToByteArray(encryptedBuffer);
Related