【问题标题】:Livecode mergAESEncryptWithKey functionLivecode mergAESEncryptWithKey 函数
【发布时间】:2020-03-19 21:34:59
【问题描述】:

我想使用 mergAESEncryptWithKey pData,pKey,pIV,[pMode],[pKeySize],[pPadding] 加密实时代码中的数据。然后将加密的数据发布到 php。 PhP 使用相同的函数解密数据,对数据进行处理,然后加密结果并将其发布到 livecode。 Livecode 然后从 php 解密数据

我的 PHP 代码看起来像这样(这很完美)

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

【问题讨论】:

  • 这里有问题吗?
  • 是的!我如何在 livecode 中做同样的事情?
  • 你应该先试试。只需在按钮中创建一个 mouseUp 处理程序,看看你能走多远。如果您已经认真尝试过,但几个小时后仍然无法弄清楚,请回到这里,发布您的代码,我们会尽力提供帮助。同时,你能解释一下你为什么需要这个吗?也许有更简单更好的解决方案。

标签: php livecode


【解决方案1】:

对于大多数不知道但仍想发布无用 cmets 的人来说,我想通了。 @Sammitch 和 @Mark 提出问题并不意味着某人很愚蠢。

    /*
* 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;
}

哦,这是用于实时代码的

//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

【讨论】:

  • 您想使用 mergAESEncryptWithKey 函数。你还没有回答你自己的问题:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多