Reputation: 3549
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?
Upvotes: 0
Views: 764
Reputation: 83642
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).
function decrypt($user, $key){
//...
}
function decrypt($user){
$key = '....whatever...';
//...
}
function decrypt($user){
global $key;
//...
}
function decrypt($user){
//...
$user = mcrypt_ecb(MCRYPT_DES, $GLOBALS['key'], $user, MCRYPT_DECRYPT);
//...
}
Upvotes: 1