NancyJ

02-14-2006, 12:48 PM

I have an encryption function and a decryption function as part of my credit card class.

The decryption function is passed a value and a key.

If the decryption function is called from the encryption function, it works perfectly.

If the result of the encryption is converted into a string ($v[0].$v[1]) and stored as $this->encryptedNumber then later split in 2 and passed to the decryption function with the same key, it doesnt work.

I printed out both arrays that were passed in and they are both identical.

I'm absolutely baffled as to what the problem is.

function encrypt()

{

$k = $this->getKey();

$v = explode("\r\n", chunk_split($this->number, ceil(strlen($this->number)/2)));

$this->xteaEncrypt($v, $k);

}

function decrypt()

{

$k = $this->getKey();

$v = explode("\r\n", chunk_split($this->encryptedNumber, ceil(strlen($this->encryptedNumber)/2)));

$this->xteaDecrypt($v, $k);

}

function getKey()

{

$k[0] = "123456789012345";

$k[1] = "678901234567890";

$k[2] = "987654321098765";

$k[3] = "432109876543210";

return $k;

}

function xteaEncrypt($v, $k) {

$sum = 0;

$delta = 0x9E3779B9;

for($i=0; $i<32; $i++) {

$v[0] += unsigned(($v[1] << 4 ^ $v[1] >> 5) + $v[1] ^ $sum + $k[$sum & 3]);

$sum += $delta;

$v[1] += unsigned(($v[0] << 4 ^ $v[0] >> 5) + $v[0] ^ $sum + $k[$sum>>11 & 3]);

}

$this->encryptedNumber = $v[0].$v[1];

$this->xteaDecrypt($v, $k);

}

function xteaDecrypt($v, $k) {

print_r($v)."<br />";

print_r($k)."<br />";

$sum = 0xC6EF3720;

$delta = 0x9E3779B9;

for($i=0; $i<32; $i++) {

$v[1] -= unsigned(($v[0] << 4 ^ $v[0] >> 5) + $v[0] ^ $sum + $k[$sum>>11 & 3]);

$sum -= $delta;

$v[0] -= unsigned(($v[1] << 4 ^ $v[1] >> 5) + $v[1] ^ $sum + $k[$sum&3]);

}

echo "<br />Funct Decrypted:".$v[0].$v[1]."<br />";

$this->number = $v[0].$v[1];

}

function unsigned($val)

{

$val = $val*$val;

$val = sqrt($val);

return $val;

}

The decryption function is passed a value and a key.

If the decryption function is called from the encryption function, it works perfectly.

If the result of the encryption is converted into a string ($v[0].$v[1]) and stored as $this->encryptedNumber then later split in 2 and passed to the decryption function with the same key, it doesnt work.

I printed out both arrays that were passed in and they are both identical.

I'm absolutely baffled as to what the problem is.

function encrypt()

{

$k = $this->getKey();

$v = explode("\r\n", chunk_split($this->number, ceil(strlen($this->number)/2)));

$this->xteaEncrypt($v, $k);

}

function decrypt()

{

$k = $this->getKey();

$v = explode("\r\n", chunk_split($this->encryptedNumber, ceil(strlen($this->encryptedNumber)/2)));

$this->xteaDecrypt($v, $k);

}

function getKey()

{

$k[0] = "123456789012345";

$k[1] = "678901234567890";

$k[2] = "987654321098765";

$k[3] = "432109876543210";

return $k;

}

function xteaEncrypt($v, $k) {

$sum = 0;

$delta = 0x9E3779B9;

for($i=0; $i<32; $i++) {

$v[0] += unsigned(($v[1] << 4 ^ $v[1] >> 5) + $v[1] ^ $sum + $k[$sum & 3]);

$sum += $delta;

$v[1] += unsigned(($v[0] << 4 ^ $v[0] >> 5) + $v[0] ^ $sum + $k[$sum>>11 & 3]);

}

$this->encryptedNumber = $v[0].$v[1];

$this->xteaDecrypt($v, $k);

}

function xteaDecrypt($v, $k) {

print_r($v)."<br />";

print_r($k)."<br />";

$sum = 0xC6EF3720;

$delta = 0x9E3779B9;

for($i=0; $i<32; $i++) {

$v[1] -= unsigned(($v[0] << 4 ^ $v[0] >> 5) + $v[0] ^ $sum + $k[$sum>>11 & 3]);

$sum -= $delta;

$v[0] -= unsigned(($v[1] << 4 ^ $v[1] >> 5) + $v[1] ^ $sum + $k[$sum&3]);

}

echo "<br />Funct Decrypted:".$v[0].$v[1]."<br />";

$this->number = $v[0].$v[1];

}

function unsigned($val)

{

$val = $val*$val;

$val = sqrt($val);

return $val;

}