Reputation: 553
I am using the following function to encrypt and decrypt
define('KEYVAL',"hgfzhjh");
function encryption($plain_text)
{
$key_value = KEYVAL;
$encrypted_text = mcrypt_ecb(MCRYPT_DES, $key_value, $plain_text, MCRYPT_ENCRYPT);
return $encrypted_text;
}
function decryption($encrypted_text)
{
$key_value = KEYVAL;
$decrypted_text = mcrypt_ecb(MCRYPT_DES, $key_value, $encrypted_text, MCRYPT_DECRYPT);
return $decrypted_text;
}
echo $id = 'abc';
echo "<br />";
$enc = encryption('abc');
echo $dec = decryption($enc);
echo "<br />";
echo $dec;
echo "<br />";
echo strcmp($id,$dec);
Although the echo of $enc
and $dec
is same but
the result of strcmp
is -5. Why..?
I am fetching data from database using the decrypted result but it is unable to fetch data because both strings are not same. please let me know if I am not using it correctly..
Upvotes: 1
Views: 914
Reputation: 68536
If you have doubts, just var_dump
your string. var_dump($dec)
gives the length of 8
, that is why your strcmp
gave you -5
Decryption here gives you extra spaces on the end of the string, so make use of a rtrim
function decryption($encrypted_text)
{
$key_value = KEYVAL;
$decrypted_text = mcrypt_ecb(MCRYPT_DES, $key_value, $encrypted_text, MCRYPT_DECRYPT);
return rtrim($decrypted_text); //Trimming here
}
Btw .. why spaces are getting added ? Have a look at this thread
Upvotes: 1