http://qs1969.pair.com?node_id=1066496


in reply to Re: port a function from php
in thread port a function from php

Wow, your variant worked for 'very secret string' in perl, but when i put any other hash, for example:
md5_decrypt('2EC0KFZ1aGghEdySB+5Y9nbAfMrk9ky/89vwlA4HyTU=', '34giu34hgiu34hg'); should return '34giu34hgiu34hg'
perl returns some binaty data... really weird! Any solution?

Replies are listed 'Best First'.
Re^3: port a function from php
by Crackers2 (Parson) on Dec 10, 2013 at 20:01 UTC

    Hmm I'm not getting an issue with that particular example; it's coming out f234fgerg5g for me.

    I did say it was a quick hack though :)

    To really emulate php you'd probably want to create a new function, something like:

    sub php_xor { my ($p1,$p2) = @_; my $len = length($p1) < length($p2) ? length($p1) : length($p2); return substr($p1,0,$len) ^ substr($p2,0,$len); }

    and use that where you currently have ^, giving something like:

    use MIME::Base64; use Digest::MD5 qw(md5 md5_hex md5_base64); use strict; sub php_xor { my ($p1,$p2) = @_; my $len = length($p1) < length($p2) ? length($p1) : length($p2); return substr($p1,0,$len) ^ substr($p2,0,$len); } 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(php_xor($password,substr($enc_text,0,$iv_len)), 0, + 512); my $x; while ($i < $n) { my $block = substr($enc_text, $i, 16); $plain_text .= $block ^ pack('H*', md5_hex($iv)); $iv = php_xor(substr($block . $iv, 0, 512),$password); $i += 16; } #$plain_text =~ s/\x13\x00*$//; return $plain_text; } print md5_decrypt('2EC0KFZ1aGghEdySB+5Y9nbAfMrk9ky/89vwlA4HyTU=', '34g +iu34hgiu34hg') . "\n";
      Ahh it works now!!! THANK YOU SO MUCH , YOU ARE THE BEST