Question

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?

Was it helpful?

Solution

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);
    //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top