Qt-QCA(aes128) decrypted MP4 file is corrupted.
-
Hi, Iam using QCA library to encrypt and decrypt a mp4 file.Below is the code which i used.
But some how my decrypted file is corrupt and VLC cannot play this file.VLC Output
[loas0xb26150c0] Stream #0: not enough frames to estimate rate; consider increasing probesize
[loas0xb26150c0] decoding for stream 0 failed
[loas0xb26150c0] Could not find codec parameters (Audio: aac_latm, 0 channels, s16)
[loas0xb26150c0] Estimating duration from bitrate, this may be inaccurateMycode:
@QCA::Initializer init = QCA::Initializer() ;
if(QCA::isSupported("aes128-cbc-pkcs7"))
{
QFile inputFile("/home/Bala/sample.mp4");if (!inputFile.open(QIODevice::ReadOnly)) qDebug() << "problem while reading " ; QByteArray InputFileByteArray = inputFile.readAll(); inputFile.close(); qDebug() << "size of clear file " << InputFileByteArray.size(); QCA::SecureArray inputSA = InputFileByteArray; qDebug() << "size of inputSA" << inputSA.size(); QString ki = "myencryp"; QCA::SymmetricKey key = ki.toAscii(); QCA::InitializationVector iv = ki.toAscii(); QCA::Cipher cipher(QString("aes128"),QCA::Cipher::CBC, QCA::Cipher::DefaultPadding,QCA::Encode,key,iv); QCA::SecureArray encoded = cipher.process(inputSA); qDebug() << "size of encoded" << encoded.size(); if (!cipher.ok()) { printf("update failed\n"); } cipher.setup(QCA::Decode, key, iv); QCA::SecureArray original = cipher.process(encoded); qDebug() << "size of original" << original.size(); if (!cipher.ok()) { printf("Final failed\n"); } QByteArray originaldata = original.toByteArray(); qDebug() << "size of originaldata" << originaldata.size(); QFile file1("/home/Bala/sample_decoded.mp4"); if (!file1.open(QIODevice::WriteOnly)) qDebug() << "problem while writing "; QDataStream out1(&file1); out1<< originaldata; file1.close(); }@
Please let me know if there are any issues with my code.
-
Are you sure this works? Calling @inputFile.readAll()@ is kind of dangerous in my opinion... What if the file is several gigabytes in size? Have you compared the sizes of the files ?
But I think your biggest problem is to use a QDataStream to write back the buffer. This will append a header indicating that what follows is a QByteArray with its size. You should just call @file1.write( originalData )@ otherwise you'll have a weird header that VLC will obviously not recognize. -
Hi rcari,
I used file1.write (originalData)and it works fine, thanks for your helpRegards,