Cancelado

Port PHP Class to 64 bit

We’ve brought up our latest server in 64 bit but run into a nasty bug. We use an encryption algorithm called TEA which allows two way encryption. Its worked great for years but the PHP code that implements it produces different results in 64 bits. It no longer matches our 32 bit machines so they cannot trade parameters.

I've put the conversion task on a job board but have no takers as yet. Here's the code if you're interested in a real challenge and have 64 bit linux machine handy. I'll gladly pay for a completed conversion.

The code does bit shifting but it does not work when executed on 64 linux. Here’s the class followed by a couple of test lines:

When run on 32 bit it produces the following output:

1354ad6de2a1ae14b830a5ae5af3a49361b1f1ec = 32 bit output

1354ad6de2a1ae14b830a5ae5af3a49361b1f1ec

Secret String 2009

Successful conversion will be when the class is modified to produce the same target string on 64 bit that it does on 32 bit and then it successfully converts the encrypted string back to the original 'Secret String 2009'. Let me know if you’ve got time to fool with it. Its too nasty for me.

## Deliverables

<?php

class TEA {

function TEA(){}

function encrypt($val, $key)

{

$v = $this->strToLongs($val);

$k = $this->keyToLongs($key);

$n = count($v);

if($n == 0) return "";

if($n == 1) $v[$n++] = 0;

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

$y = $v[0];

$delta = 0x9E3779B9;

$q = floor(6+52/$n);

$sum = 0;

while ($q-- > 0)

{

$sum += $delta;

$e = ($sum>>2) & 3;

for ($p=0; $p<$n-1; $p++)

$z = $v[$p] += $this->mx($z, $v[$p+1], $sum, $k[$p&3^$e]);

$z = $v[$n-1] += $this->mx($z, $v[0], $sum, $k[$p&3^$e]);

}

return bin2hex($this->longsToStr($v));

}

function decrypt($crypt, $key) {

$v = $this->strToLongs($this->hex2str($crypt));

$k = $this->keyToLongs($key);

$n = count($v);

if ($n==0) return "";

$y = $v[0];

$delta = 0x9E3779B9;

$q = floor(6 + 52/$n);

$sum = $this->chk($q * $delta);

while ($sum != 0)

{

$e = ($sum>>2) & 3;

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

{

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

$y = $v[$p] -= $this->mx($z, $y, $sum, $k[$p&3^$e]) - 0x80000000 - 0x80000000; //0x100000000 is too large

}

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

$y = $v[0] -= $this->mx($z, $y, $sum, $k[$p&3^$e]) - 0x80000000 - 0x80000000;

$sum -= $delta;

}

return $this->unpad($this->longsToStr($v));

}

function unpad($s) {

$i = strlen($s);

if (ord($s[$i-1])==0) $i--;

if (ord($s[$i-1])==0) $i--;

if (ord($s[$i-1])==0) $i--;

if (ord($s[$i-1])==0) $i--;

return substr($s,0,$i);

}

function mx($z, $y, $sum, $k) {

$s1 = $this->xor32($this->shl5($z), $this->shr2($y));

$s2 = $this->xor32($this->shl3($y), $this->shr4($z));

$s3 = $this->xor32($sum, $y);

$s4 = $this->xor32($k, $z);

$mx = $this->xor32(($s1 + $s2), ($s3 + $s4));

return $mx;

}

function chk($d) {

return ($d<0) ? $d + (0x80000000 + 0x80000000) : $d;

}

function xor32($a, $b) {

return $this->chk($a^$b);

}

function shl5($d) {

return $this->chk(($d>>5) & 0x07ffffff);

}

function shl3($d) {

return $this->chk(($d>>3) & 0x1fffffff);

}

function shr2($d) {

return $this->chk(($d<<2) & 0xffffffff);

}

function shr4($d) {

return $this->chk(($d<<4) & 0xffffffff);

}

function strToLongs($s) {

$n = ceil(strlen($s)/4);

$j = 0;

for ($i=0; $i<$n; $i++)

$l[$i] = ord($s[$j++]) +

(ord($s[$j++])<<8) +

(ord($s[$j++])<<16) +

(ord($s[$j++])<<24);

return $l;

}

function keyToLongs($k){

$j = 0;

for ($i=0; $i<4; $i++)

$l[$i] = ord($k[$j++]) +

(ord($k[$j++])<<8) +

(ord($k[$j++])<<16) +

(ord($k[$j++])<<24);

return $l;

}

function longsToStr($l){

$s = "";

for($i=0; $i<count($l); $i++)

$s .= pack("L", $l[$i]);

return $s;

}

function hex2str($hex){

return pack("H*", $hex);

}

}

$t = new TEA;

$key = "xxKEYxx";

$es = $t->encrypt("Secret String 2009", $key);

$de = $t->decrypt($es, $key);

print "1354ad6de2a1ae14b830a5ae5af3a49361b1f1ec = Target<br>";

print "$es<br>";

print "$de<br>";

?>

Habilidades: Engenharia, MySQL, PHP, Arquitetura de software, Teste de Software, Hospedagem Web, Gestão de Site , Teste de Website

Ver mais: string j, string algorithm, shifting bits, print pack, n. b. handy, k to the i, count bits, conversion challenge, challenge machines, challenge machine, bug board, board bug, bit bits, b.c. job board, algorithm string, algorithm of encryption, algorithm for encryption, algorithm challenge, 2 sum algorithm, q-bit, encrypt php code, val , trade algorithm, s4, q-v

Acerca do Empregador:
( 1 comentário ) United States

ID do Projeto: #3022854

2 freelancers estão ofertando em média $148 para este trabalho

Flowzone

See private message.

$212.5 USD in 14 dias
(12 Comentários)
3.9
kanhashriinc

See private message.

$84.15 USD in 14 dias
(9 Comentários)
2.9