dsfh40613182 2019-02-27 22:21
浏览 357

为什么AES无法解密,PHP。 OpenSSL的

I'm having problem with decrypting I have

 $key="Gwu078980";
 $cipher="aes-128-gcm";
 $iv=md5($cipher);
 $text="yaw0";
 $tag="";
 echo $encrypted=openssl_encrypt($text, $cipher, $key, 0, $iv, $tag);

 echo $de_ciphertext=openssl_decrypt($encrypted, $cipher, $key, 0, $iv, $tag);

Output

ELRmWQ==
yaw0

So the raw text is yaw0 and the encrypted is ELRmWQ== and the decrypted is yaw0 so perfect.

But when I manually copy the encrypted text and use it as

$encrypted ="ELRmWQ==";

And I run the decryption the decryption returns null. Thanks in advance for anyone to help me out.

  • 写回答

1条回答 默认 最新

  • duanlipeng4136 2019-02-27 22:47
    关注

    Your openssl_encrypt message modifies $tag by reference since you're using aes-128-gcm.

    That parameter is required for openssl_decrypt aswell (when using AEAD - Authenticated Encryption and Decryption) and is probably an empty string in your case when you omit the openssl_encrypt call.

    See Example 1 in the docs:

    The comment about storing $cipher, $iv, and $tag is the important part:

    <?php
    //$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes
    $plaintext = "message to be encrypted";
    $cipher = "aes-128-gcm";
    if (in_array($cipher, openssl_get_cipher_methods()))
    {
        $ivlen = openssl_cipher_iv_length($cipher);
        $iv = openssl_random_pseudo_bytes($ivlen);
        $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
        //store $cipher, $iv, and $tag for decryption later
        $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
        echo $original_plaintext."
    ";
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果