vendredi 1 juin 2018

QT OpenSSL C++ and PHP AES/CBC

I have to make a uniformed data encryption between a C++ library and php. This is really my first adventure into AES. I've been reading what I could and looking trying to figure it out. I came up with the code below. I just can not seem to get a matching outputs. I've been trying to figure this out for several days.

What I have noticed is that the outputs form the php and CPP version are really close. From the CPP function we get ta4SK7e1ziUMnBchxqol/TT82wNS8TmJQ/kFls+I04HuK4nP7kDT4J2zYJDB6WIx and from php we get ta4SK7e1ziUMnBchxqol/TT82wNS8TmJQ/kFls+I04H6XsCH1kfyiS/gu2oi57yY. If you look they are really close. Only the last few bytes differ.

So I know I'm close, but I really can't see what I'm don't doing correct.

Here is the C++ Code:

QByteArray encryptAES(QByteArray passphrase, QByteArray plainText, QByteArray myiv)
{
    unsigned char* Key = (unsigned char*) passphrase.data();
    unsigned char* IV = (unsigned char*) myiv.data();

    /** Setup the AES Key structure required for use in the OpenSSL APIs **/
    AES_KEY* AesKey = new AES_KEY();
    AES_set_encrypt_key(Key, 256, AesKey);

    /** take an input string and pad it so it fits into 16 bytes (AES Block Size) **/
    const int UserDataSize = (const int)plainText.size();   // Get the length pre-padding
    int RequiredPadding = (AES_BLOCK_SIZE - (UserDataSize % AES_BLOCK_SIZE));   // Calculate required padding
    for(int i=0; i < RequiredPadding; i++) {
        plainText.push_back('\0');
    }

    unsigned char * UserData = (unsigned char*) plainText.data(); // Get the padded text as an unsigned char array
    const int UserDataSizePadded = (const int)plainText.size();// and the length (OpenSSl is a C-API)

    /** Peform the encryption **/
    unsigned char EncryptedData[UserDataSizePadded] = {0}; // Hard-coded Array for OpenSSL (C++ can't dynamic arrays)
    AES_cbc_encrypt(UserData, EncryptedData, UserDataSizePadded, (const AES_KEY*)AesKey, IV, AES_ENCRYPT);

    QByteArray encrypted = QByteArray(reinterpret_cast<char*>(EncryptedData), UserDataSizePadded);

    return encrypted;
}

QByteArray decryptAES(QByteArray passphrase, QByteArray encryptedText, QByteArray myiv)
{
    unsigned char* Key = (unsigned char*) passphrase.data();
    unsigned char* IV = (unsigned char*) myiv.data();
\
    /** Setup an AES Key structure for the decrypt operation **/
    AES_KEY* AesDecryptKey = new AES_KEY(); // AES Key to be used for Decryption
    AES_set_decrypt_key(Key, 256, AesDecryptKey);   // We Initialize this so we can use the OpenSSL Encryption API

    /** Decrypt the data. Note that we use the same function call. Only change is the last parameter **/
    unsigned char DecryptedData[encryptedText.size()] = {0}; // Hard-coded as C++ doesn't allow for dynamic arrays and OpenSSL requires an array
    AES_cbc_encrypt((unsigned char*) encryptedText.data(), DecryptedData, encryptedText.size(), (const AES_KEY*)AesDecryptKey, IV, AES_DECRYPT);

    QByteArray decrypted = QByteArray(reinterpret_cast<char*>(DecryptedData), encryptedText.size());

    return decrypted;
}

int main(int argc, char *argv[])
{
    QByteArray plainText("This string was AES-256-CBC encrypted.");
    QByteArray phpEnc = QByteArray("ta4SK7e1ziUMnBchxqol/TT82wNS8TmJQ/kFls+I04H6XsCH1kfyiS/gu2oi57yY");
    QByteArray key = QByteArray::fromBase64("pE4B5J5u18U55BTJho//Fioy2bEURa5W/o7HrO1O7/s=");
    QByteArray iv = QByteArray::fromBase64("Gi9kSb/a5f0h7Mb+sRWQdQ==");
    qDebug() << "AES Test KEY: " << key.toBase64();
    qDebug() << "AES Test IV: " << iv.toBase64();
    QByteArray enc = encryptAES(key,plainText, iv);
    qDebug() << "AES Test Encrypt: " << enc.toBase64();
    qDebug() << "AES Test Decrypt: " << decryptAES(key, enc, iv);
    qDebug() << "AES Test Decrypt PHP: " << decryptAES(key, phpEnc, iv);
}

Now here is the php code:

<?php
$method = 'AES-256-CBC';
$txt = "This string was AES-256-CBC encrypted.";
$iv = base64_decode("Gi9kSb/a5f0h7Mb+sRWQdQ==");
$key = base64_decode("pE4B5J5u18U55BTJho//Fioy2bEURa5W/o7HrO1O7/s=");
echo "AES Test KEY: ". base64_encode($iv)."\n\n<br>";
echo "AES Test IV: ". base64_encode($key)."\n\n<br>"."\n\n<br>";

$fromCPP = "ta4SK7e1ziUMnBchxqol/TT82wNS8TmJQ/kFls+I04HuK4nP7kDT4J2zYJDB6WIx";

$enc = openssl_encrypt($txt, $method, $key, OPENSSL_RAW_DATA, $iv);
$dec = openssl_decrypt($enc, $method, $key, OPENSSL_RAW_DATA, $iv);

//Test if php isn't padding
$UserDataSize = strlen($txt);
$RequiredPadding == (16 - ($UserDataSize % 16));
$paddedText = $txt;
for($i=0; $i < $RequiredPadding; $i++)
    $paddedText .= "\0";

$enc2 = openssl_encrypt($paddedText, $method, $key, OPENSSL_RAW_DATA, $iv);
$dec2 = openssl_decrypt($enc2, $method, $key, OPENSSL_RAW_DATA, $iv);

//Try to decrypt CPP 
$dec3 = openssl_decrypt(base64_decode($fromCPP), $method, $key, OPENSSL_RAW_DATA, $iv);

echo "AES Test Encrypt: ". base64_encode($enc)."\n\n<br>";
echo "AES Test Decrypt: ". $dec."\n\n<br>";
echo "AES Test Encrypt Padded: ". base64_encode($enc2)."\n\n<br>";
echo "AES Test Decrypt Padded: ". $dec2."\n\n<br>";
echo "AES Test Decrypt CPP: ". $dec3."\n\n<br>";

?>

Aucun commentaire:

Enregistrer un commentaire