On Jan 24, 2008 7:13 PM, Jochem Maas <[EMAIL PROTECTED]> wrote:
> Richard Lynch schreef:
> > On Thu, January 24, 2008 5:20 pm, Jochem Maas wrote:
> >> someone asked about checksum values in another thread, I believe
> >> he got his answer no thanks to me. but whilst I was trying to help
> >> I got stuck playing with pack/unpack.
> >>
> >> so now I have the broadbrush question of what would one use
> >> pack/unpack for? can anyone point at some realworld examples/code that
> >> might help me broaden my horizons?
> >
> > Reading a "binary" file with various bytes/bits that need to get
> > converted to integers for you to do something useful with them.
>
> ok. that's where my brain goes to mush - all strings in php (for now)
> are binary ... what's the difference between the binary strings
> in php and such found in these 'binary files' that they need to be
> packed/unpacked?
>
> sorry if I'm sounding retarded ... I probably am :-/
>
> >
> > Ditty for writing said file.
> >
>
> --
>
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

I took over a project that used XOR encryption on these jpeg files and
re-wrote them with custom headers.  So I had to learn all about
converting binary to hex.  I needed a way to find specific values
within the file of where the header stopped and where the data began.
Using pack() was a key part of getting my modified hex values back
into binary when I was writing values into the file.

So tools of the trade: bin2hex(), ord(), pack('H*'), sprintf('%02x').

Here is a simple way to write hex to binary:
pack('H*', sprintf('%02x', $hex));

Here is a simple XOR example:

<?php
echo '<pre>';

$string         = 'My unencrypted string';
$salt           = "Ya Harr";

$len            = strlen($string);
$saltLen        = strlen($salt);
$index          = 0;
$encrypted      = array();

echo "Encrypting:\n";

for ($x=0; $x < $len; ++$x) {
        $char = $string{$x};
        $xor = ord($salt{ $index % $saltLen });
        $enc = (ord($char) ^ $xor);
        ++$index;
        $encrypted[] = $enc;
        echo sprintf("char: %s enc: %d xor: %d \n", $char, $enc, $xor);
}

echo "Decrypting:\n";

$index = 0;
foreach ($encrypted as $enc) {
        $xor = ord($salt{$index % $saltLen});
        ++$index;
        $char = $xor ^ $enc;
        echo sprintf("char %c enc: %d xor: %d \n", $char, $char, $enc, $xor);
}
?>

Of course one should know that xor encryption really isn't encryption.
 It is basically just reversing the file so it is more of an
obfuscation method.

I'm sure there are better ways of achieving the desired output, but
this is what I found out in the time I was given.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to