dongyinzhi4689 2014-01-03 12:44
浏览 18
已采纳

PHP:mcrypt_ecb在函数内部不起作用

I've got a very simple function:

$me = 45S237s53dsSerwjw53s23rjf; //Some long encrypted string.

function decrypt($user){
    $user = pack("H*" , $user); //Converting from hexadecimal to binary

    $user = mcrypt_ecb(MCRYPT_DES, $key, $user, MCRYPT_DECRYPT); //Decrypting

    return $user;
}

The problem is if I do go echo decrypt($me); it doesn't work, I don't end up with a decrypted string.

However if I do essentially the same thing without using a function it works:

    $user = $me;        

    $user = pack("H*" , $user);

    $user = mcrypt_ecb(MCRYPT_DES, $key, $user, MCRYPT_DECRYPT);

    echo $user; //Works fine...

What's going on here?

  • 写回答

1条回答 默认 最新

  • douchibu7040 2014-01-03 12:49
    关注

    Your missing the $key variable inside the function body. With the correct error level settings you'd have been given a warning, that $key is undefined.

    Either add $key as a function argument or define $key inside the function body (or, third alternative, import $key from the global scope).

    1

    function decrypt($user, $key){
        //...
    }
    

    2

    function decrypt($user){
        $key = '....whatever...';
        //...
    }
    

    3.1

    function decrypt($user){
        global $key;
        //...
    }
    

    3.2

    function decrypt($user){
        //...
        $user = mcrypt_ecb(MCRYPT_DES, $GLOBALS['key'], $user, MCRYPT_DECRYPT);
        //...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?