For a school project I’m developing a php based steganography class. As part of it I am encrypting the message to be hidden before encoding it into an image. So this means I must convert the resulting cipher text from mcrypt into binary. My problem is that when I try converting the binary back into the cipher text the result is different from the original cipher text. (The output binary looks strange to for that matter.) I’m assuming that the problem lies with the encoding used on the cipher text and the encoding used when converting back from binary are different but I have no idea where to start to fix that. You can see a live example at dev.hersha.me/str2bin.php and my code will be posted below.
<?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') . "\
<br \\>";
//echo $steg->bin2bstr('010011110100110101000111') . "\
<br \\>";
$hash = hash('md5',"OMGZWTF");
echo $hash . "\
<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 . "\
<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 . "\
<br \\>";
echo "Cipher Text (binary) : " . $binenc . "\
<br \\>";
echo "Cipher Text (back from binary) : " . $bstrenc . "\
<br \\>";
echo "Decryption : " . $decrypted; // The quick brown fox jumps over the lazy dog
?>