使用PHP我試圖將由mcrypt生成的密文轉換爲二進制文件,但是當我嘗試將其轉換回密文時,它並沒有正確轉換回所以不能被解密。我假設我的編碼正在被搞亂,但我不知道從哪裏開始弄清楚。這是一個鏈接到網站http://dev.hersha.me/str2bin.php,這裏是我的代碼。PHP - mcrypt在加密字符串上使用什麼編碼?
<?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
?>
任何想法?
謝謝,剛剛幫我解決了類似的問題 – 2010-04-30 19:41:49