After encrypting a string "1234567890", I used hex2bin function to convert the encrypted string into binary format and got "ea359482e4b20603bfe9".
But my attempt to decrypt it back to 1234567890 fails (always get the wired characters).
What am I missing?
Here is a sample.
<?php
$text = "1234567890";
$key = "TestingKey";
echo "SRC: ".$text."<br/>";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv);
//$encrypted = bin2hex($encrypted);
$encrypted = "ea359482e4b20603bfe9"; //this was one of the string that came out.
echo "ENC: ".$encrypted."<br/>";
$encrypted = hex2bin($encrypted);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CFB, $iv);
echo "DEC: ".$decrypted."<br/>";
function hex2bin($text)
{
$len = strlen($text);
for($i=0;$i<$len;$i+=2)
{
$binary .= pack("C",hexdec(substr($text,$i,2)));
}
return $binary;
}
?>
Thank you!