使用 PHP 我正在尝试将 mcrypt 生成的密文转换为二进制文件,但是当我尝试将其转换回密文时,它没有正确转换回来,因此无法解密。我假设我的编码在某个地方搞砸了,但我不知道从哪里开始弄清楚。这是网站http://dev.hersha.me/str2bin.php的链接,这是我的代码。
<?php
class phpSteg {
function bin2bstr($input) {
if (!is_string($input)) return null;
return pack('H*', base_convert($input, 2, 16));
}
function bstr2bin($input) {
if (!is_string($input)) return null;
$value = unpack('H*', $input);
return base_convert($value[1], 16, 2);
}
};
$steg = new phpSteg();
//echo $steg->bstr2bin('OMG') . "\n <br \>";
//echo $steg->bin2bstr('010011110100110101000111') . "\n <br \>";
$hash = hash('md5',"OMGZWTF");
echo $hash . "\n <br \>";
$message = "OMG WTF BBQ";
$text = $message;
$key = $hash;
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
echo $iv . "\n <br \>";
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv);
$binenc = $steg->bstr2bin($encrypted);
$bstrenc = $steg->bin2bstr($binenc);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $bstrenc, MCRYPT_MODE_CFB, $iv);
echo "Cipher Text : " . $encrypted . "\n <br \>";
echo "Cipher Text (binary) : " . $binenc . "\n <br \>";
echo "Cipher Text (back from binary) : " . $bstrenc . "\n <br \>";
echo "Decryption : " . $decrypted; // The quick brown fox jumps over the lazy dog
?>
有任何想法吗?