J'ai un code PHP simple pour le chiffrement/description 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));
}
et je cherche la MÊME méthode pour c++ dans l'application QML. Jusqu'à présent, j'ai ceci (je ne sais pas comment gérer l'IV)
cryptage.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
cryptage.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;
}
pouvez-vous m'aider à le finir s'il vous plait ? L'essentiel est que je ne sais pas comment gérer l'IV comme le fait PHP (un nombre aléatoire, hex2bin) et l'IV est enregistré dans la chaîne encodée (ENCODED_STRING::USED_IV) afin que l'IV puisse être utilisé plus tard pour le décodage
Dans votre code PHP, le vecteur d'initialisation (IV) est généré avec un générateur de nombres aléatoires. Vous pouvez faire la même chose avec Qt. Par exemple:
QByteArray iv = QByteArray(16, 0);
for(int i=0; i<16; ++i) {
iv[i] = static_cast<char>(QRandomGenerator::system()->bounded(255));
}
Au lieu de cela, l' exemple que vous suivez crée une chaîne avec n'importe quel texte arbitraire, puis en calcule un hachage MD5. L'utilisation de la méthode ci-dessus est plus directe, mais vous pouvez faire de même ou générer une chaîne aléatoire avant de calculer le hachage.
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots