AESEncryptWithKey merge method in Livecode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jumpygate
Posts: 1
Joined: Tue Nov 26, 2024 11:15 am
Contact:

AESEncryptWithKey merge method in Livecode

Post by jumpygate » Tue Nov 26, 2024 11:23 am

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');

Post Reply