我使用tiny-AES-c library来加密Qt ByteArray,但加密或解密没有按预期工作,在解密结束时我没有得到未加密的ByteArray。
我使用的代码是:
void MainWindow::compressImageWithEncryption()
{
qDebug() << "compressImageWithEncryption";
//grabbing a screenshot of the desktop
auto img = QGuiApplication::primaryScreen()->grabWindow(QApplication::desktop()->winId()).toImage();
//saving the screenshot as a JPG formated byte array
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "JPG");
//displaying the screenshot to the ui
auto img_from_bytes = QImage::fromData(bytes, "JPG");
ui->lblDesktop->setPixmap(QPixmap::fromImage(img_from_bytes));
//compress the screenshot byte array
QByteArray compressed_bytes = qCompress(bytes,9);
//settings encryption key and iv
const uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
const uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
//encrypt compressed bytes
std::vector<uint8_t> encrypted_bytes(compressed_bytes.size());
auto uint8_compressed_bytes = reinterpret_cast<uint8_t*>(compressed_bytes.data());
AES_CBC_encrypt_buffer(
encrypted_bytes.data(),
uint8_compressed_bytes,
compressed_bytes.size(),
key,
iv
);
//ok encryption is done.
//decrypt compressed bytes
std::vector<uint8_t> decrypted_bytes(encrypted_bytes.size());
AES_CBC_encrypt_buffer(
decrypted_bytes.data(),
encrypted_bytes.data(),
encrypted_bytes.size(),
key,
iv
);
//ok decryption is done.
//uncompress the encrypted compressed bytes
auto uncompressed_bytes = qUncompress(reinterpret_cast<const unsigned char*>(decrypted_bytes.data()),decrypted_bytes.size());
qDebug() << "uncompressed_bytes size:" << uncompressed_bytes.size();
qDebug() << "compressImageWithEncryption completed";
}
这段代码的作用是它获取桌面的屏幕截图,压缩它,加密它,解密它然后解压缩它。 但是在解压缩步骤中我收到错误: qUncompress:Z_DATA_ERROR:输入数据已损坏。 我在这做错了什么? 您可以从此链接https://www.sendspace.com/file/3j6qbi
下载完整的Qt项目