【问题标题】:try catch is not giving output as expected [duplicate]尝试捕获未按预期提供输出[重复]
【发布时间】:2014-03-13 07:59:54
【问题描述】:

你好,我正在使用 2 个函数进行加密和解密

我的解密函数如下

  function decrypt($string, $key = NULL) {
   // $key = 'this is new thign gor security';
    $string = base64_decode(base64_decode($string));
    $key = md5($key); //to improve variance
    /* Open module, and create IV */
    $td = mcrypt_module_open('des', '', 'cfb', '');

    $key = substr($key, 0, mcrypt_enc_get_key_size($td));
    $iv_size = mcrypt_enc_get_iv_size($td);
    $iv = substr($string, 0, $iv_size);
    $string = substr($string, $iv_size);
    /* Initialize encryption handle */
    if (mcrypt_generic_init($td, $key, $iv) != -1) {
      /* Encrypt data */
      $c_t = mdecrypt_generic($td, $string);
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
      return $c_t;
    } //end if
  }

但是在这个函数中,如果我传递实际上不是加密的字符串,那么它会给我注意未定义的变量并且不给出任何输出,所以这就是为什么我将 try catch 博客添加到它的下面

function decrypt($string, $key = NULL)
{


    try
    {
        $string = base64_decode(base64_decode($string));
        $key = md5($key); //to improve variance
        /* Open module, and create IV */
        $td = mcrypt_module_open('des', '', 'cfb', '');

        $key = substr($key, 0, mcrypt_enc_get_key_size($td));
        $iv_size = mcrypt_enc_get_iv_size($td);
        $iv = substr($string, 0, $iv_size);
        $string = substr($string, $iv_size);
        /* Initialize encryption handle */
        if (mcrypt_generic_init($td, $key, $iv) != -1)
        {
            /* Encrypt data */
            $c_t = mdecrypt_generic($td, $string);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return $c_t;
        } //end if
    } catch (Exception $exc)
    {
        echo $exc->getTraceAsString();
    }


    // $key = 'this is new thign gor security';
}  

但它又在同一点构造并且不去捕捉

这里我放了一些字符串和键

encrypted string: dExXZStvRmV6WFR5NkE9PQ==

and KEY is : !1@2#3$4%5^6&7*8(9)0_-+=

你可以像这样使用这个功能

$xyz = decrypt('dExXZStvRmV6WFR5NkE9PQ==','!1@2#3$4%5^6&7*8(9)0_-+=');

【问题讨论】:

  • 它为未定义变量错误给出了什么行,还请注意默认情况下您无法捕获需要使用自定义处理程序的通知/警告:stackoverflow.com/questions/1241728/can-i-try-catch-a-warning
  • 我上传我的通知图片
  • 我没有看到 Notice undefined variable 在那里...但我看到一个警告,告诉您您正在传递一个空字符串,因此您应该在将它们传递给函数之前检查空字符串。跨度>
  • @PatrickEvans 当我运行这个函数并且当我传递没有被我的函数加密的字符串时,它会给我错误 $c_t = mdecrypt_generic($td, $string);在这种情况下,因为在这种情况下 $string 将为空
  • 因为正如我之前提到的警告/通知不会触发捕获,您必须使用自定义处理程序。

标签: php encryption try-catch


【解决方案1】:

尝试抛出异常,看看你的代码有什么问题,然后你就可以解决你的问题了。

在你的条件下试试这个:print_r(error_get_last());

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 2020-12-16
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多