...

View Full Version : Decryption Problems

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)));

}

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];
}

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;

}

NancyJ
02-14-2006, 02:37 PM
I've compared the two, I've printed out the ascii character codes for each character in the array and they still come out the same. But the decryption comes out different.

NancyJ
02-14-2006, 03:08 PM
I've narrowed down the problem to being that there is some whitespace being lost here:

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

doesnt anyone know how I can preserve that white space?

firepages
02-14-2006, 10:29 PM
total guess

\$this->number = "{\$v[0]}.{\$v[1]}";

or cast everything as (string)\$v[0] etc , otherwise not a clue :D

NancyJ
02-14-2006, 11:45 PM
tried the first one, havent tried casting it as a string. WIll give it a go in the morning.

NancyJ
02-15-2006, 09:46 AM
that didnt work either :S

Theres got to be a way to preserve that data.... hmmm maybe thats why I didnt find a php version of xTEA

firepages
02-15-2006, 01:54 PM
http://php-einfach.de/sonstiges_generator_xtea_script.php

NancyJ
02-15-2006, 03:52 PM
I wish my german was better lol :(
I know it shouldnt matter but for some reason its really off-putting

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum