c++ - Qt C++ - 如何保护文件

标签 c++ qt

<分区>

我的应用程序生成一个包含重要文件的 .zip,我不希望最终用户可以访问这些数据。我使用 QuaZIP 库进行压缩。

我的第一个想法是为 zip 文件设置密码,但它看起来很复杂且不太安全,但如果您有解决方案,它就足以满足我的需要。

否则你有什么想法可以轻松保护文件吗?我可以加密吗?

非常感谢。 我是 Qt 5.11 和 MSVC2017。

最佳答案

最简单的保护:只需更改文件扩展名(例如,从.zip.bin)。例如使用 QFile::rename() :

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

另一种方式:
从文件中读取所有数据并加密。您可以使用 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);

然后你可以发送这个newFile

在接收方,从您收到的文件中读取所有数据到缓冲区:

QByteArray encryptedBuffer = receivedFile.readAll();

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

QByteArray yourDecryptedData = crypto.decryptToByteArray(encryptedBuffer);

关于c++ - Qt C++ - 如何保护文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60744819/

相关文章:

c++ - 'this' 参数的类型为 const 但函数未标记为 const

c++ - 第一个字母大写,其余字母小写

c++ - ‘{’ 之前的预期类名

qt - 在 QGraphicsView 中多次显示某个 QGraphicsItems 树

c++ - 检查 fopen 不支持的文件名

c++ - Qt。如何使用QtNetwork查看网络上的其他电脑?

c++ - OpenCV、eclipse编译问题

c++ - 是否为get_time定义了重复赋值?

已编译但未使用的 C++ 模板 - mac OS Lion

c++ - 如何改变QTextEdit的位置