Need help with perl and php package

I have the task of converting a crypt function that someone made in perl to php code. Everything is working fine, except for this:

Perl:

$wert = Encode::encode( "utf8", $wert );
$len=length $wert;
$pad = ($len % 16)?"0".chr(16 - ($len % 16)):"10";
$fuell = pack( "H*", $pad x (16 - $len % 16));

PHP:

$wert = utf8_encode($wert);
$len = mb_strlen($wert);
$pad = ( $len%16 ) ? '0'.chr(16 - ($len%16)) : '10';
$fuell = pack("H*", str_repeat($pad, (16 - $len % 16)));

The php version works fine for some lines. But when I have something like "2010-01-01T00: 00: 00.000", the perl version works without any errors, and the php version prints "PHP Warning: pack (): Type H: illegal hex digit".

I am very grateful if someone could detect an error in the php version.

Edit:

This is a complete function that I have to convert to php. This was done by the programmer of a company that no longer works for us, so I can’t say what the original intention was.

sub crypt
{
    my $self = shift;
    my ($wert,$pw)= @_;
    $wert = Encode::encode( "utf8", $wert );
    $pw = Encode::encode( "utf8", $pw );
    $len=length $wert;
    $pad = ($len % 16)?"0".chr(16 - ($len % 16)):"10";
    $fuell = pack( "H*", $pad  x (16 - $len % 16));
    $wert=$wert.$fuell;
    $lenpw=length $pw;
    $fuell = ($lenpw % 16)? pack ("H*", "00" x (16 - $lenpw % 16)):"";
    $pw=$pw.$fuell;
    $cipher = new Crypt::Rijndael $pw, Crypt::Rijndael::MODE_CBC;
    $cipher->set_iv($pw);
    $crypted = encode_base64($cipher->encrypt($wert),"");

    return $crypted;
}
+3
2

, . H , , PHP, (). , :

chr(16 - ($len % 16))

Perl , Perl , ( , ). , .

, :

sprintf('%x', 16 - ($len % 16))

.. , , , , Perl.

+7

, Perl pack() , PHP .

:

print pack("H*", "ZZ");

3 Perl ( - ), , PHP.

, Perl "", , PHP.

: , Perl "" . :

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ  #-- Give this to Perl...
0123456789ABCDEF0123456789ABCDEF0123  #-- .. and it treated as this hex digit

, "ZZ" "33", 3. , . , Perl , .

+6
source

Source: https://habr.com/ru/post/1723679/


All Articles