我有一个使用C ++和QT5.4的UI应用程序,我使用CryptoPP 5.6.2来加密文件。我遇到了以下问题:
点击加密按钮后,将根据this tutorial启动新主题。
// new thread
CryptoWorkerThread = new QThread;
this->worker = new CryptoWorker(fileName.c_str(), newFileName.c_str(), key, keyLength, iv);
this->worker->moveToThread(CryptoWorkerThread);
connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(CryptoWorkerThread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), CryptoWorkerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), this, SLOT(on_CryptoWorker_finished()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(CryptoWorkerThread, SIGNAL(finished()), CryptoWorkerThread, SLOT(deleteLater()));
CryptoWorkerThread->start();
我将指针存储到主窗口类中的线程和工作者(加密按钮的父级,因此是插槽)
工人阶级:
class CryptoWorker : public QObject {
Q_OBJECT
public:
CryptoWorker(const char* sourceFileName, const char* destFileName, const byte * key, int keyLength, const byte * iv);
~CryptoWorker();
const char* sourceFileName;
const char* destFileName;
public slots:
void process();
signals:
void finished();
void error(QString err);
private:
// add your variables here
const byte* key;
const byte* iv;
int keyLength;
};
CryptoWorker::CryptoWorker(const char* sourceFileName, const char* destFileName, const byte * key, int keyLength, const byte * iv){
this->sourceFileName = sourceFileName;
this->destFileName = destFileName;
this->key = key;
this->keyLength = keyLength;
this->iv = iv;
}
CryptoWorker::~CryptoWorker(){
}
void CryptoWorker::process(){
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryptor(key, keyLength, iv);
CryptoPP::FileSource(sourceFileName, true,
new CryptoPP::StreamTransformationFilter(
encryptor,
new CryptoPP::FileSink(destFileName),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING
),
true // StreamTransformationFilter
); // FileSource
emit finished();
return;
}
现在,当线程运行时我正在使用此函数动态加密文件A到文件B:
CryptoPP::FileSource(sourceFileName, true,
new CryptoPP::StreamTransformationFilter(
encryptor,
new CryptoPP::FileSink(destFileName),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING
),
true // StreamTransformationFilter
); // FileSource
但是,线程一直停留,直到文件完成编码和写入,这可能需要几分钟。我无法杀死该帖子,因为没有地方可以进行isAlive()
检查。
我正在尝试找到一个允许我使用FileSource的解决方案,FileSink(速度与fstream或文件或qfile相比是惊人的),并且让我在某个时候取消操作。
我通过添加另一个检查新创建的加密文件B的大小的线程来解决进度监视,但是控制在给定时刻写入的字节会很酷(这样我就可以检查{{ 1}}和递增数据计数器。)
此时我陷入困境,无法找到解决方案。请帮忙。
答案 0 :(得分:1)
您可以尝试将false
传递给pumpAll
构造函数的第二个参数(FileSource
)并使用Pump
方法在块中循环工作 - 这应该允许检查isAlive
并增加计数器。