Results 1 to 7 of 7

Thread: QCA file encryption problem

  1. #1
    Join Date
    Mar 2009
    Location
    Tunisia
    Posts
    38
    Thanks
    12
    Platforms
    Windows

    Default QCA file encryption problem

    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 :
    Qt Code:
    1. QFile clearFile("/home/walid/Music/Jennifer.mp3");
    2. if (!clearFile.open(QIODevice::ReadOnly))
    3. qDebug() << "problem while reading " ;
    4. QByteArray clearFileByteArray = clearFile.readAll();
    5. clearFile.close();
    6. qDebug() << "size of clear file " << clearFileByteArray.size() ;
    7. QCA::SecureArray arg = QCA::SecureArray(clearFileByteArray);
    8. qDebug() << arg.size() ;
    9. ......
    10. QCA::SecureArray result = pubkey.encrypt(arg, QCA::EME_PKCS1_OAEP);
    11. QByteArray encryptedFileByteArray(result.toByteArray());
    12. qDebug() << " size of encrypted FileByteArray " << result.toByteArray().size() ;
    13. QFile encryptedFile("/home/walid/Music/encrypted.mp3");
    14. if (!encryptedFile.open(QIODevice::WriteOnly))
    15. qDebug() << "problem while opening on write mode";
    16. encryptedFile.write(encryptedFileByteArray);
    17. encryptedFile.close();
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QCA file encryption problem

    Have you checked pubKey.canEncrypt() and maximumEncryptSize()?

  3. The following user says thank you to ChrisW67 for this useful post:

    oswalidos (11th May 2012)

  4. #3
    Join Date
    Mar 2009
    Location
    Tunisia
    Posts
    38
    Thanks
    12
    Platforms
    Windows

    Default Re: QCA file encryption problem

    I checked canEncrypt but not maximumEncryptSize, i'll check ASAP and replay,

    Thanks

  5. #4
    Join Date
    Mar 2009
    Location
    Tunisia
    Posts
    38
    Thanks
    12
    Platforms
    Windows

    Default Re: QCA file encryption problem

    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

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QCA file encryption problem

    I have never used QCA myself. I based my question on the code in this example, 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 and this example.

  7. #6
    Join Date
    Sep 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Post Re: QCA file encryption problem

    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

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QCA file encryption problem

    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

Similar Threads

  1. Encryption with Qt
    By safknw in forum Newbie
    Replies: 7
    Last Post: 11th July 2012, 15:06
  2. AES Encryption Decryption of a file using Qt
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 13:22
  3. Encryption api in Qt
    By safknw in forum Qt Programming
    Replies: 2
    Last Post: 1st May 2010, 04:25
  4. sqlite+qt+encryption
    By skrzypu in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 14:22
  5. Encryption and decryption
    By vermarajeev in forum General Programming
    Replies: 5
    Last Post: 27th August 2007, 21:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.