Jump to content

STrange error (bitwise operation)


gorros

Recommended Posts

I have implemented a encrypt/decrypt algorithm (original in javascript), and when looping I got strange result.

As a test I just made a quick printout at a known value of one of the parameters.

 

This is the local code

 

$z = $v[$p-1];

$ii= 4071400299;

if ($ii==$z){

$mxt0= ($z >>5)&0x07ffffff;

$mxt1= ($ii >>5)&0x07ffffff;

echo 0-1 : ".$mxt0."=".$mxt1;

}

 

And I get this output :    0-1 : 67108863=127231259

 

Can anyone give me a clue of what is going on???????

Please, need help

Link to comment
Share on other sites

This code works for me:

 

<?php
$z = 4071400299;
$ii= 4071400299;
if ($ii==$z){
   $mxt0= ($z >>5)&0x07ffffff;
   $mxt1= ($ii >>5)&0x07ffffff;
   echo "0-1 : " .$mxt0."=".$mxt1;
}
?>

 

It is not possible for the values of $mxt0 and $mxt1 to be different provided that $z and $ii are equal.  Maybe you could post more code?

 

Patrick

Link to comment
Share on other sites

I know it shouldn't happen, but it does!!!!!

 

Here are some more of my code, and input values

 

$ciptxt="-2982644431.4071400299.2046465665.-1438448000.1276185794";

$key="570526-1039";

 

function x($ciptxt,$key){

 

$v=explode(".",$ciptxt);

$k = str2long($key,false);

if (count($k) < 4)

for ($i = count($k); $i < 4; $i++)$k[$i] = 0;

 

$n = count($v);

$z = $v[$n-1];

$y = $v[0];

$delta = 0x9E3779B9;

$mx=0;

$e=0;

$q = 3;

$sum= $q*$delta;

 

$ii= 4071400299;

 

 

while ($sum!=0){

$e = $sum>>2 & 3;

for ($p = $n-1; $p > 0; $p--){

$z = $v[$p-1];

if ($ii==$z){

$mxt0= ($z >>5)&0x07ffffff;

$mxt1= ($ii >>5)&0x07ffffff;

echo "0-1 : ".$mxt0."=".$mxt1;

}else

$mxt1= ($v[$p-1] >>5)&0x07ffffff;

 

$mx = (($z >>5)&0x07ffffff^$y<<2) + (($y >>3)&0x1fffffff^$z<<4) ^ ($sum^$y) + ($k[$p&3^$e]^$z);

$v[$p] -= $mx;

$y = $v[$p];

}

$z = $v[$n-1];

$mx = (($z >>5)&0x07ffffff^$y<<2) + (($y >>3)&0x1fffffff^$z<<4) ^ ($sum^$y) + ($k[$p&3^$e]^$z);

$v[0] -= $mx;

$y=$v[0];

$sum -= $delta;

}

Link to comment
Share on other sites

Here is the my str2long function. Try it and see what you get

 

 

function str2long($s, $w){

$v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));

$v = array_values($v);

if ($w)

$v[count($v)] = strlen($s);

return $v;

}

Link to comment
Share on other sites

hmmm... I don't really know what to say... If you're sure that $z and $ii are the equal then your code is analogous to:

 

<?php
$a = 1;
$b = 1;
$a = $a + 1;
$b = $b + 1;
?>

 

I've come across weirdness with the shift operator before (usually problems with 32 => 64 bit architecture) but It makes no sense at all that running the same SHIFT and AND would result in two different values.  Sorry, I couldn't help.

 

Patrick

Link to comment
Share on other sites

<?
echo '<pre>' . print_r (str2long("foo", "bar"), true) . '</pre>';
function str2long($s, $w){   
   $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3));
   $v = array_values($v);
   if ($w)
      $v[count($v)] = strlen($s);
   return $v;
}
?>

 

Array

(

    [0] => 7303014

    [1] => 3

)

Link to comment
Share on other sites

I don't have a cause yet, but I have a solution:

 

<?php
         if ($ii==$z){
            $z = 4071400299;
            $mxt0= ($z >>5)&0x07ffffff;
            $mxt1= ($ii >>5)&0x07ffffff;
            echo "0-1 : ".$mxt0."=".$mxt1;
         }
?>

 

or

 

<?php
         if ($ii==$z){
            $mxt1= ($ii >>5)&0x07ffffff;
            $mxt0 = $mxt1;
            echo "0-1 : ".$mxt0."=".$mxt1;
         }
?>

 

For some reason the variable $z doesn't react well to the logical shift right.  By assigning $z to the scalar 4071400299, which is tautologically equivalent to the original the orginal code, the SHIFT and AND work as expected.  I'm curious as to why this happening...

 

Best,

 

Patrick

Link to comment
Share on other sites

Problem is, the array will never be the same, and this error occur at random (to me at least) position, regardles of value or array-order

 

I don't see what the array has anything to do with the problem.  You will only run this code when $z == $ii and $ii is statically set to 4071400299;

Link to comment
Share on other sites

No, it happen with any value.

But, I found a way to make it work.

 

After assigning the value, I make a call to settype($z,"double");

 

Maybe I should have known it, but it seem that this "auto" type assignement is a so-so

Anyway, thank's a lot for making me think in different ways

Link to comment
Share on other sites

As an aside, the === operator will check both value and type.  Using that instead of == will show you when there are typing differences.  There's also a corresponding !== operator.

 

I use === by default, unless I am absolutely sure I want == for a specific case.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.