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 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。