Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: port a function from php

by reqnode (Novice)
on Dec 10, 2013 at 19:36 UTC ( #1066496=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1066496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-12-05 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?