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

Hello Monks,

I have a question in regard to a PHP function "XORmask" which I'm not sure how it works in Perl. I suspect the Perl 'unpack' would produce the desired output, but I am not sure how to get there.

The PHP code is:

$id="Z0xcAVtKAwkDHVcKVF5TCQ";
$track = base64_decode($id); #yields "gL\[J W T^S"
$track = $track ^ XORmask; #yields "T|808|586|127800"

I have gotten as far as:

use MIME::Base64;
my $track_decode = decode_base64($id); #yields "gL\[J W T^S"

... but cannot get the Perl equivalent of "$track = $track ^ XORmask;" so I can get to "T|808|586|127800" as the output.

Any help would be greatly appreciated!

  • Comment on Perl function equivalent to PHP xormask

Replies are listed 'Best First'.
Re: Perl function equivalent to PHP xormask
by choroba (Cardinal) on Mar 20, 2016 at 00:05 UTC
    You can define it as
    sub XORmask { '30d1c6615af8cfc9' }

    How did I get the magical value? By XORing the expected output with the $track. That's how XOR works.

    #!/usr/bin/perl use warnings; use strict; use MIME::Base64; use constant XORmask => '30d1c6615af8cfc9'; my $id = 'Z0xcAVtKAwkDHVcKVF5TCQ'; my $track_decode = decode_base64($id); print $track_decode ^ XORmask, "\n";

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      If you could please share how you were able to get the constant value '30d1c6615af8cfc9', that would be the tidbit of code I'm looking for.

      For what it's worth, I tried that same constant value on other IDs (examples: Z0xcAVdKAwkAHVcKUF5VAA, Z0xcAVRKAwkAHVcKUF9bAA) and they were decoded properly.

      I hope that makes sense.

        #!/usr/bin/perl use warnings; use strict; use MIME::Base64; my $XORmask = "T|808|586|127800" ^ decode_base64('Z0xcAVtKAwkDHVcKVF5T +CQ'); print $XORmask."\n"; print $XORmask ^ decode_base64('Z0xcAVdKAwkAHVcKUF5VAA')."\n"; print $XORmask ^ decode_base64('Z0xcAVRKAwkAHVcKUF9bAA')."\n";
        poj
        If you could please share how you were able to get the constant value

        choroba already did:

        How did I get the magical value? By XORing the expected output with the $track. That's how XOR works.

        i.e.

        $ perl -MMIME::Base64 -le 'print decode_base64( "Z0xcAVtKAwkDHVcKVF5TCQ")^"T|808|586|127800"' 30d1c6615af8cfc9
Re: Perl function equivalent to PHP xormask
by graff (Chancellor) on Mar 20, 2016 at 00:15 UTC
    It's not clear to me that there is a commonly-known PHP function called "XORmask" (at least, nothing that can be found via Google). Could you point us to some documentation that describes this function? (Could it be something that is defined in the PHP code that you started from?)

    Also, unless there's a description of this "XORmask" function that explains how it arrives at the output you say it yields, I'm at a loss how to come up with similar results.

    I tried this little test script, which decodes your base64 input string to the series of bytes you suggest - but just to be clear, I'm displaying each byte as a 2-digit hex number; I'm also breaking up the character sequence that you say you expect to get as a result, and I display this as a sequence of hex numbers as well. The two sequences are displayed next to each other below, values from decode_base64 on the left, and your expected output on the right:

    perl -MMIME::Base64 -e '$_="Z0xcAVtKAwkDHVcKVF5TCQ"; @i=split //,decode_base64($_); @t=split //,"T|808|586|127800"; printf("%3d: %02x %02x\n",$_,ord($i[$_]),ord($t[$_])) for (0..$#i)' 0: 67 54 1: 4c 7c 2: 5c 38 3: 01 30 4: 5b 38 5: 4a 7c 6: 03 35 7: 09 38 8: 03 36 9: 1d 7c 10: 57 31 11: 0a 32 12: 54 37 13: 5e 38 14: 53 30 15: 09 30
    So, apart from not seeing any coherent pattern here, I'm wondering what sort of function this is supposed to be. It's clearly not any kind of bit-wise logic operation, because a given input value can 'yield' different outputs (lines "7:" and "15:" above), and different inputs can yield the same output (lines "3:", "14." and "15.").

    You need to give us a better description of the functionality you are looking for.