Jiri Zaloudek
Jiri Zaloudek

Reputation: 380

QT: AES-256-CBC Encrypt/Decript in C++ according to PHP Code

I have a simple PHP Code for encryption/decription AES-256-CBC:

function safeEncrypt($token) {
    $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length("aes-256-cbc"));
    return openssl_encrypt($token, "aes-256-cbc", "passToCrypt", 0, $enc_iv) . "::" . bin2hex($enc_iv);
}
function safeDecrypt($token) {
    $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length("aes-256-cbc"));
    list($token, $enc_iv) = explode("::", $token);
    return openssl_decrypt($token, "aes-256-cbc", "passToCrypt", 0, hex2bin($enc_iv));
}

and I am looking for the SAME way for c++ inside QML app. So far I have this (I dont know how to handle the IV)

cryption.h:

#ifndef CRYPTION_H
#define CRYPTION_H

#include <QObject>
#include <QCryptographicHash>

class Cryption : public QObject
{
    Q_OBJECT
public:
    explicit Cryption(QObject *parent = nullptr);
    Q_INVOKABLE QString encrypt(QString strToEncrypt);
    Q_INVOKABLE QString decrypt(QString strToDecrypt);

private:
    QString method;
    QString key;
    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC);

signals:

public slots:

};

#endif // CRYPTION_H

cryption.cpp:

#include "cryption.h"
#include <QCryptographicHash>

Cryption::Cryption(QObject *parent) :
    QObject(parent)
{
    // Main Settings
    method = "aes-256-cbc";
    key = "passToCrypt";
}

QString Cryption::encrypt(QString strToEncrypt)
{
    QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
//  QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);

    QByteArray encodeText = encryption.encode(strToEncrypt.toLocal8Bit(), hashKey, hashIV);
    return encodeText;
}

QString Cryption::decrypt(QString strToEncrypt)
{
    QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
//  QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);

    QByteArray decodeText = encryption.decode(encodeText, hashKey, hashIV);
    QString decodedString = QString(encryption.removePadding(decodeText));
    return decodedString;
}

can you help me finish it please? Main thing is that I dont know how to handle the IV as the PHP does (some random number, hex2bin) and the IV is saved into the encoded string (ENCODED_STRING::USED_IV) so the IV can be later on used for decoding

Upvotes: 0

Views: 1105

Answers (1)

Former contributor
Former contributor

Reputation: 2576

In your PHP code, the initialization vector (IV) is generated with a random number generator. You may do the same with Qt. For instance:

QByteArray iv = QByteArray(16, 0);
for(int i=0; i<16; ++i) {
    iv[i] = static_cast<char>(QRandomGenerator::system()->bounded(255));
}

Instead, the example you are following creates a string with whatever arbitrary text, and then calculates a MD5 hash of it. Using the above method is more direct, but you can do the same or generate a random string before calculating the hash.

Upvotes: 1

Related Questions