How can I use the Qt Cryptographic Architecture to encrypt data with AES-128 correctly?
-
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?
-
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. -
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.