【问题标题】:openssl_decrypt returns blankopenssl_decrypt 返回空白
【发布时间】:2020-11-05 12:25:05
【问题描述】:

我尝试了以下代码

  <?PHP
    
       $encrypt_method = "AES-128-CBC";
        $secret_key = 'iuiuiui';
        $secret_iv = '1234567891234567';
        $string="keeri";
    
        // hash
        $key = hash('sha256', $secret_key);
        
        // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
        $iv = substr(hash('sha256', $secret_iv), 0, 16);
         $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
         
          echo "encrypt----)";
          echo $output ;
         
          echo "decrypt---";
           $output1 = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
           echo $output1;
         
    ?>

得到以下输出

encrypt----)3BR54C8qvhHG3e4Lgry4uw==decrypt----)

解密字符串 ($output1) 为空。

【问题讨论】:

    标签: php openssl


    【解决方案1】:

    您的代码运行良好解密功能的输入数据有误。

    您正在向 openssl_decrypt 函数提供(原始)纯文本,而不是通过 openssl_encrypt(“输出”)加密的数据。

    只是关于代码安全性的友好信息:当您在“CBC”模式下使用 AES 算法时,有必要使用 随机生成的初始化向量 (“iv”) 或您的完全加密变得易受攻击

    这将是您的加密/解密的输出:

    encrypt----)3BR54C8qvhHG3e4Lgry4uw==decrypt---keeri
    

    完整代码:

    <?php
    $encrypt_method = "AES-128-CBC";
    $secret_key = 'iuiuiui';
    $secret_iv = '1234567891234567';
    $string="keeri";
    
    // hash
    $key = hash('sha256', $secret_key);
    
    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
    
    echo "encrypt----)";
    echo $output ;
    
    echo "decrypt---";
    //$output1 = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
    $output1 = openssl_decrypt($output, $encrypt_method, $key, 0, $iv);
    echo $output1;
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 2018-12-23
      • 2017-11-20
      • 1970-01-01
      • 2011-05-27
      • 2012-09-06
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多