-2

I want to encrypt data in livecode using mergAESEncryptWithKey pData,pKey,pIV,[pMode],[pKeySize],[pPadding]. The encrypted data is then posted to php. PhP decrypts the data using the same function, does something with the data and then encrypts the results and posts them to livecode. Livecode then decrypts the data from php

My PHP Code looks like this (This works perfect)

 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');
cfugus
  • 3
  • 4
  • Is there a question somewhere in here? – Sammitch Mar 19 '20 at 21:38
  • YES!!! how do I do the same in livecode? – cfugus Mar 19 '20 at 22:49
  • You should try it first. Just make a mouseUp handler in a button and see how far you get. If you have given it a serious attempt and still can't figure it out after several hours, come back here, post your code and we'll try to help. Meanwhile, could you explain why you need this? Perhaps there is an easier and better solution. – Mark Mar 20 '20 at 22:47

1 Answers1

-1

For most of you who just don't know but still want to post good for nothing comments, I figured it out. @Sammitch and @Mark asking a question don't mean someone is stupid.

    /*
* Encrypt or decrypt data using 
* AES-256-CBC
*/
function encrypt_decrypt($mode,$data,$key,$salt){
    //define the cipher method 
    $ciphering = "AES-256-CBC"; 
    // Use OpenSSl Encryption method. This works on PHP 7.2 and above
    $iv_length = openssl_cipher_iv_length($ciphering); 
    $options = 0; 

    if($mode=="encrypt"){
        // Use openssl_encrypt() function to encrypt the data 
        $Data = openssl_encrypt($data, $ciphering, $key, $options, $salt);  
    }else{
        // Use openssl_decrypt() function to decrypt the data 
        $Data = openssl_decrypt($data, $ciphering, $key,$options, $salt);
    }



    return $Data;
}

Oooh and this is for livecode

//encrypt
encrypt tString using "aes-256-cbc" with key theKey and IV saltHash at 256 bit

//decrypt
decrypt base64Decode(varEnc) using "aes-256-cbc" with key theKey and IV saltHash at 256 bit
cfugus
  • 3
  • 4
  • You wanted to use the mergAESEncryptWithKey function. You haven't anwered your own question :-) – Mark Mar 24 '20 at 21:17