bricke / Qt-AES

Native Qt AES encryption class
The Unlicense
501 stars 187 forks source link

Empty Encoded QByteArray #67

Closed ArchNemSyS closed 2 weeks ago

ArchNemSyS commented 2 weeks ago

I have built QtAES (linux cmake gcc) and all of the tests passed.

Upon integrating it into my project as a library I copied the 128bit key in ECB mode sample into main as below :

Concise Client main.cpp GitLab project link

#include "mainwindow.h"

#include <QApplication>
#include <QDebug>

#include <QtAES/QAESEncryption>

int main(int argc, char *argv[])
{
    //QApplication a(argc, argv);

    QByteArray plainText("top secret");
    QByteArray key("key42");

    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
    QByteArray encodedText = encryption.encode(plainText, key);

    qDebug() << "encodedText : " << QString(encodedText);

    QByteArray decodedText = encryption.decode(encodedText, key);

    qDebug() << "decodedText : " << QString(decodedText);

    //MainWindow w;
    //w.show();
    //return a.exec();
}

I receive no errors compiling or running the code, However the variable encodedText is empty ?

I did get a cryptic linker message on a clean rebuild :


AutoUic: Detected locale "C" with character encoding "ANSI_X3.4-1968", which is not UTF-8.
Qt depends on a UTF-8 locale, and has switched to "C.UTF-8" instead.
If this causes problems, reconfigure your locale. See the locale(1) manual
for more information.

Any ideas where I have gone wrong ?

ArchNemSyS commented 2 weeks ago

More coffee required, sorry

helps if you supply AES128 with a 128-bit key !

#include "mainwindow.h"

#include <QApplication>
#include <QDebug>

#include <QPasswordDigestor>
#include <QtAES/QAESEncryption>

int main(int argc, char *argv[])
{
    //QApplication a(argc, argv);
    //MainWindow w;
    //w.show();

    auto iterations = 600000;
    auto keyLen = 16;
    auto res = QPasswordDigestor::deriveKeyPbkdf2(QCryptographicHash::Algorithm::Sha256,
                                                  QString("password").toUtf8(),
                                                  QString("saltsaltsalt").toUtf8(),
                                                  iterations,
                                                  keyLen);

    auto key = res;
    QByteArray plainText("top secret");

    qDebug() << key.toHex();

    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
    QByteArray encodedText = encryption.encode(plainText, key);

    qDebug() << "encodedText : " << QString(encodedText);

    QByteArray decodedText = encryption.decode(encodedText, key);

    qDebug() << "decodedText : " << QString(decodedText);

    //return a.exec();
}

works perfectly, just got to remember to terminate my strings