http://qs321.pair.com?node_id=1066447

reqnode has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i'm trying to port a function from PHP to Perl, a following function: in PHP:
function md5_decrypt($enc_text, $password, $iv_len = 16) { $enc_text = base64_decode($enc_text); $n = strlen($enc_text); $i = $iv_len; $plain_text = ''; $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512); while ($i < $n) { $block = substr($enc_text, $i, 16); $plain_text .= $block ^ pack('H*', md5($iv)); $iv = substr($block . $iv, 0, 512) ^ $password; $i += 16; } return preg_replace('/\\x13\\x00*$/', '', $plain_text); }
i wrote in perl:
use MIME::Base64; use Digest::MD5 qw(md5 md5_hex md5_base64); use strict; sub md5_decrypt { my $iv_len = 16; my $enc_text = decode_base64(shift); my $password = shift; my $n = length($enc_text); my $i = $iv_len; my $plain_text; my $iv = substr(($password ^ substr($enc_text, 0, $iv_len)), 0, 51 +2); while ($i < $n) { my $block = substr($enc_text, $i, 16); $plain_text .= $block ^ pack('H*', md5_hex($iv)); $iv = substr($block . $iv, 0, 512) ^ $password; $i += 16; } #$plain_text =~ s/\x13\x00*$//; return $plain_text; }
perl function gives me a different and wrong result decrypting this hash, for example md5_decrypt('XHnhcRzMuo63RqqZmbxW1nQKXqVMm48eXMS6oh9wmBdg9pfXhAmNQEvR+luaVVzk', 'test'); should return 'very secret string', but it doesn't