[SOLVED] QCA decode method
-
Hi everyone!
I need to use AES256 encode in my project. I've found the QCA library and integrate it to my project. All works fine but when I'm trying decode the string which I've encode using QCA I've get empty string.
@
QString MdlLogParser::encode(QString source, QString key, QString iv)
{
QCA::Initializer init = QCA::Initializer();
QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());QCA::Cipher cipher = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC, QCA::Cipher::DefaultPadding, QCA::Encode, keyData, ivData); if (!QCA::isSupported("aes256-cbc-pkcs7")) { qDebug() << "AES256 CBC PKCS7 not supported - " "please check if qca-ossl plugin" "installed correctly !"; return ""; } QCA::SecureArray secureData = source.toUtf8(); QCA::SecureArray encryptedData = cipher.process(secureData); if (!cipher.ok()) { qDebug() << "Encryption failed !"; return ""; } return QString(qPrintable(QCA::arrayToHex(encryptedData.toByteArray())));
}
QString MdlLogParser::decode(QString encodeString, QString key, QString iv)
{
QCA::Initializer init = QCA::Initializer();
QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());QCA::Cipher cipher = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC, QCA::Cipher::DefaultPadding, QCA::Encode, keyData, ivData); QCA::SecureArray encryptedData = encodeString.toUtf8(); cipher.setup(QCA::Decode, keyData, ivData); QCA::SecureArray decryptedData = cipher.process(encryptedData); if (!cipher.ok()) { qDebug() << "Decryption failed ! "; return ""; } return QString(decryptedData.data());
}
@Use
@
QString encodeString = encode("Hello World!", "aaaaaa", "bbbbb");
qDebug()<<"encode: "<<encodeString;
qDebug()<<"decode: "<<decode("d84e7cc6a90ba17c4e4195309ac2f87f", "aaaaaa", "bbbbb");
@Consol log:
@
encode: "d84e7cc6a90ba17c4e4195309ac2f87f"
Decryption failed !
decode: ""
@What I've do wrong? Thanks for the any help!
-
Hi,
the setup in decode:
@
QCA::Cipher cipher = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
QCA::Cipher::DefaultPadding,
QCA::Encode, << Shouldn't that be Decode ?
keyData, ivData);@ -
Hi,
Thanks for the reply! I've tried this it's doesn't help. I've found the "example":http://www.essentialunix.org/index.php?option=com_content&view=article&id=48:qcatutorial&catid=34:qttutorials&Itemid=53. And it's works if encode and decode in one function. But when I tried to create two methods decode doesn't work.
-
Ok, I've found solution.
As usually the solution is a simple. All I need is convert encoded string from hex to byte array using function QCA::hexToArray().
@
QCA::Initializer init = QCA::Initializer();
QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());
QCA::Cipher cipherRes = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
QCA::Cipher::PKCS7, QCA::Decode,
keyData, ivData);QCA::SecureArray decryptedData = cipherRes.process(QCA::hexToArray(encodeString)); if (!cipherRes.ok()) { qDebug() << "Decryption failed ! "; return ""; } return QString(decryptedData.data());
@
Now it's work!
-
Good !
Then don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)