Page 1 of 1

AESEncryptWithKey merge method in Livecode

Posted: Tue Nov 26, 2024 11:23 am
by jumpygate
I'd like to encrypt data in livecode with mergAESEncryptWithKey pData,pKey,pIV,[pMode],[pKeySize],[pPadding]. The encrypted data is subsequently uploaded to PHP. PhP decrypts the data with the same function, processes it, and then encrypts the results before posting them to livecode. Livecode then decrypts the data from php

My PHP code looks like this (which works perfectly).

Code: Select all

 function encrypt($plaintext, $salt) {
    $method = "AES-256-CBC";
    $key = hash('sha256', $salt, true);
    $iv = openssl_random_pseudo_bytes(32);

    $ciphertext = openssl_encrypt($plaintext, $method, $key, $iv);
    $hash = hash_hmac('sha256', $ciphertext . $iv, $key, true);

    return $iv . $hash . $ciphertext;
}

function decrypt($ivHashCiphertext, $salt) {
    $method = "AES-256-CBC";
    $iv = substr($ivHashCiphertext, 0, 32);
    $hash = substr($ivHashCiphertext, 32, 48);
    $ciphertext = substr($ivHashCiphertext, 64);
    $key = hash('sha256', $salt, true);

    //if (!hash_equals(hash_hmac('sha256', $ciphertext . $iv, $key, true), $hash)) return null;

    return openssl_decrypt($ciphertext, $method, $key, $iv);
}

echo $encrypted."</br>";
echo "----------------------------The message is:<br/>";


echo decrypt($encrypted, 'hashsalt');