PDA

View Full Version : QCA file encryption problem



oswalidos
11th May 2012, 02:17
Hello,
I want to encrypt an mp3 file and then decrypt it back, an RSA sample from the documentation works fine but when it's about encryption of a file content and not a string, problems begin.
Here is the code of encryption :

QFile clearFile("/home/walid/Music/Jennifer.mp3");
if (!clearFile.open(QIODevice::ReadOnly))
qDebug() << "problem while reading " ;
QByteArray clearFileByteArray = clearFile.readAll();
clearFile.close();
qDebug() << "size of clear file " << clearFileByteArray.size() ;
QCA::SecureArray arg = QCA::SecureArray(clearFileByteArray);
qDebug() << arg.size() ;
......
QCA::SecureArray result = pubkey.encrypt(arg, QCA::EME_PKCS1_OAEP);
QByteArray encryptedFileByteArray(result.toByteArray());
qDebug() << " size of encrypted FileByteArray " << result.toByteArray().size() ;
QFile encryptedFile("/home/walid/Music/encrypted.mp3");
if (!encryptedFile.open(QIODevice::WriteOnly))
qDebug() << "problem while opening on write mode";
encryptedFile.write(encryptedFileByteArray);
encryptedFile.close();


the output is :
size of clear file 3788800
size of encrypted File 128

that way i can't decrypt back the file, the method encrypt lost data, i donno why ...

Thanks in advance

ChrisW67
11th May 2012, 03:05
Have you checked pubKey.canEncrypt() and maximumEncryptSize()?

oswalidos
11th May 2012, 10:57
I checked canEncrypt but not maximumEncryptSize, i'll check ASAP and replay,

Thanks :)

oswalidos
11th May 2012, 20:28
You're right, maximumEncryptSize is 86, but i still don't know how to increase it, i googled this issue a lot, but it's like no one faced the problem of encrypting a whole file ... thanks in advance for any idea :)

ChrisW67
12th May 2012, 04:19
I have never used QCA myself. I based my question on the code in this example (http://delta.affinix.com/docs/qca/rsatest_8cpp-example.html), which encrypts a mere 5 bytes, and a quick scan of the classes in use. Personally I'd try a different encryption scheme for bulk data, public key crypto is generally only used to encrypt a small key for a symmetric scheme that encrypts the bulk data. See QCA::Cipher (http://delta.affinix.com/docs/qca/classQCA_1_1Cipher.html) and this example (http://delta.affinix.com/docs/qca/ciphertest_8cpp-example.html).

balagopal1
22nd September 2012, 12:06
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 inaccurate

Mycode:

@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.

Thanks &Regards

ChrisW67
23rd September 2012, 09:40
You should not be using a QDataStream to write the output file... It will be adding bytes to the front of the file to indicate the size of the QByteArray you are streaming to it. You want raw bytes so use QIODevice::write()

You could have started your own thread for this question