|
Posted by Jason Gerfen on 01/13/06 20:07
Duffy, Scott E wrote:
>Trying to encrypt then decrypt text with php using mcrypt. The encrypt seems to work but when I decrypt it with a different script I get most of it but some garbage. Using blowfish. So to test.
>Encrypt.php
> $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
> $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
> $key = "This is a very secret key";
> $text = "Meet me at 11 o'clock behind the monument.";
> //echo strlen($text) . "\n";
>
> $crypttext = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $text, MCRYPT_MODE_ECB, $iv);
> echo $crypttext. "\n";
>
>decrypt.php
>
> $server = $_SERVER['SERVER_NAME'];
> $url = 'http://'.$server.'/encrypt.php';
> $fh = fopen($url,'r') or die ("cant open: $php_errormsg");
> $new_string="";
>while (! feof($fh))
>{
>$new_string = $new_string.rtrim(fgets($fh,4096));
>}
>
>$enc=$newstring;
> $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
>$enc=$_POST['text'];
>$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
>$key = "This is a very secret key";
>$text = "Meet me at 11 o'clock behind the monument.";
>//echo strlen($text) . "<br>";
>
>$crypttext = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $enc, MCRYPT_MODE_ECB, $iv);
>echo "$crypttext<br>";
>
>
>I get from decrypt
>Meet me at 11 o'clock behind the monumen3ýÚ·nÃt<br>
>Is it doing some padding or something? When I encrypt/decrypt same script it works fine.
>Maybe something to do with these?
> $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
> $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
>
>
>
>Thanks,
>
>Scott Duffy
>
>
>
Look at trim(). And your right it does have to do with using ECB.
--
Jason Gerfen
"The charge that he had insulted Turkey's armed forces was dropped, but he still faces the charge that he insulted "Turkishness", lawyers said."
~ BBC News Article
Navigation:
[Reply to this message]
|