Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I use the Qt Cryptographic Architecture to encrypt data with AES-128 correctly?
Forum Updated to NodeBB v4.3 + New Features

How can I use the Qt Cryptographic Architecture to encrypt data with AES-128 correctly?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antonyprojr
    wrote on last edited by
    #1

    I have this code to get md5 hash and encrypt and decrypt the string with AES-128 algorithm for testing:
    @
    #include <QtCrypto>

    void encryptSomeData()
    {
    QString s = "Hello, world!";

    QCA::init();
    if(!QCA::isSupported("md5"))
    {
        qDebug() << "Error! This algorithm is not supported!";
    }
    else
    {
        QCA::Hash hash("md5");
        hash.update(s.toAscii());
        QString hashResult = hash.final().toByteArray().toHex();
        qDebug() << "MD5 hash result of "+s+" is: "+hashResult;
    }
    
    
    QCA::Initializer init = QCA::Initializer();
    if (QCA::isSupported("aes128-cbc-pkcs7"))
    {
        QCA::SymmetricKey key = QCA::SymmetricKey(16);
        QCA::InitializationVector iv = QCA::InitializationVector(16);
        QCA::Cipher cipher = QCA::Cipher(QString("aes128"), QCA::Cipher::CBC,
                                         QCA::Cipher::DefaultPadding, QCA::Encode,
                                         key, iv);
        QCA::SecureArray secureData = s.toAscii();
        QCA::SecureArray encryptedData = cipher.process(secureData);
        if (!cipher.ok())
        {
            qDebug() << "Encryption failed !";
            return;
        }
        qDebug() << QString(qPrintable(QCA::arrayToHex(encryptedData.toByteArray())));
        cipher.setup(QCA::Decode, key, iv);
        QCA::SecureArray decryptedData = cipher.process(encryptedData);
        if (!cipher.ok())
        {
            qDebug() << "Decryption failed !";
            return;
        }
        qDebug() << QString(decryptedData.data());
    }
    else
    {
        qDebug() << "AES128 CBC PKCS7 not supported - "
                    "please check if qca-ossl plugin "
                    "installed correctly!";
        return;
    }
    

    }@

    And i get:
    @"MD5 hash result of Hello, world! is: 6cd3556deb0da54bca060b4c39479839"
    AES128 CBC PKCS7 not supported - please check if qca-ossl plugin installed correctly!@

    Hash is ok, but something is wrong with AES-128, in the .pro file i added:
    @INCLUDEPATH += "qca-2.0.3\qca-2.0.3\include\QtCrypto"
    LIBS += "qca-2.0.3\qca-2.0.3\bin\qca2.dll"@

    What i'm doing wrong?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      QCA uses plugin DLLs that actually implement the various algorithms. You have to put those plugins in a "crypto" subfolder in the Qt library path.
      This is explained "here":http://delta.affinix.com/qca/ in the "How does it work?" section.
      One good location for this folder is next to your application executable as this folder is in the Qt library paths by default see "that":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#libraryPaths for more information on the Qt library paths.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antonyprojr
        wrote on last edited by
        #3

        How can I use QCA OSSL plugin?
        I downloaded qca-ossl-2.0.0-beta3.tar.bz2 but i don't know how to use it with QCA.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved