Jump to content


Photo

Encryption Not Working


  • Please log in to reply
7 replies to this topic

#1 Mute

Mute
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 06 July 2006 - 03:49 PM

OK, a problem with our server migration. Our TEA encryption doesn't work anymore....

Works fine on my local computer with PHP 4.3.10 and on the old server with 4.4.2 but not on the new server with 4.4.2....

Any ideas on this one? Code is below.

<?php
//---------------------------------------------------------
// TEA Encryption Functions
// Encryption and decryption of text
// Using the Tiny Encryption Algorithm
	
function DecToHex( $x )
{
	$r = dechex( $x );
	while( strlen( $r ) < 8 )
		$r = '0'.$r;
	return $r;
}

function shiftRightOne( $n )
{
	$s = $n & 0x80000000;
	$n &= 0x7fffffff;
	$n >>= 1;
	if( $s ) $n |= 0x40000000;
	return $n;
}

function shiftRight( $n, $t )
{
	for( $i = 0; $i < $t; ++$i )
	{
		$n = shiftRightOne( $n );
	}
	return $n;
}

function tea_encipher($p1, $p2, $k)
{
	$temp = array( $p1, $p2 );
	$n = 32;
	$sum = 0;
	while ($n-- > 0)
	{
		$temp[0] += (($temp[1] << 4 ^ shiftRight( $temp[1], 5 )) + $temp[1] ^ $sum + $k[($sum & 3)]);
		$sum = $sum + 0x9E3779B9;
		$temp[1] += (($temp[0] << 4 ^ shiftRight( $temp[0], 5 )) + $temp[0] ^ $sum + $k[( shiftRight( $sum, 11 ) & 3)]);
	}
	return $temp;
}

function tea_decipher($p1, $p2, $k)
{
	$temp = array( $p1, $p2 );
	$n = 32;
	$sum = 0x9E3779B9 * $n;
	while ($n-- > 0)
	{
		$temp[1] -= (($temp[0] << 4 ^ shiftRight( $temp[0], 5 )) + $temp[0] ^ $sum + $k[(shiftRight( $sum, 11 ) & 3)]);
		$sum = $sum - 0x9E3779B9;
		$temp[0] -= (($temp[1] << 4 ^ shiftRight( $temp[1], 5 )) + $temp[1] ^ $sum + $k[($sum & 3)]);
	}
	return $temp;
}

function tea_encrypt($inString, $key)
{
	$outString = "";
	
	// pad the input so that it's a multiple of 8
	while ( strlen($inString) & 7 )
	{
		$inString .= ' ';
	}
	
	$i = 0;
	while($i < strlen($inString) )
	{
		// slam 4 bytes into a dword 
		$p1 = ord( substr( $inString, $i++, 1 ) );
		$p1 |= ord( substr( $inString, $i++, 1 ) ) << 8;
		$p1 |= ord( substr( $inString, $i++, 1 ) ) << 16;
		$p1 |= ord( substr( $inString, $i++, 1 ) ) << 24;
		// mask off 32 bits to be safe
		$p1 = $p1 & 0xFFFFFFFF;
		
		// slam 4 bytes into a dword
		$p2 = ord( substr( $inString, $i++, 1 ) );
		$p2 |= ord( substr( $inString, $i++, 1 ) ) << 8;
		$p2 |= ord( substr( $inString, $i++, 1 ) ) << 16;
		$p2 |= ord( substr( $inString, $i++, 1 ) ) << 24;
		// mask off 32 bits to be safe
		$p2 = $p2 & 0xFFFFFFFF;
		
		// send dwords to be enciphered
		$res = tea_encipher($p1, $p2, $key);
		$outString .= DecToHex($res[0]).DecToHex($res[1]);
	}
	// return encrypted string
	return $outString;
}

function tea_decrypt( $inString, $key)
{
	$outString = '';
	// loop through input string
	$i = 0;
	while ( $i < strlen($inString) )
	{
		// 8 hex chars make a dword 
		$p3 = intval( substr( $inString, $i, 4 ), 16 ); // read high 16 bit word
		$p3 <<= 16; // shift hi word correct position
		$i += 4;
		$p3 |= intval(substr( $inString, $i, 4 ), 16 ); // read low 16 bit word 
		$i += 4;
		
		$p4 = intval( substr( $inString, $i, 4 ), 16 ); // read high 16 bit word
		$p4 <<= 16; // shift hi word correct position
		$i += 4;
		$p4 |= intval(substr( $inString, $i, 4 ), 16 ); // read low 16 bit word 
		$i += 4;

		// pass dwords to decipher routine
		$res = tea_decipher($p3, $p4, $key);
		
		// transform results back into alphanumic characters
		// unpack first dword
		$outString .= chr( ( $res[0] & 0x000000FF ) );
		$outString .= chr( ( $res[0] & 0x0000FF00 ) >> 8 );
		$outString .= chr( ( $res[0] & 0x00FF0000 ) >> 16 );
		$outString .= chr( shiftRight( ( $res[0] & 0xFF000000 ), 24 ) );
		// unpack second dword
		$outString .= chr( ( $res[1] & 0x000000FF ) );
		$outString .= chr( ( $res[1] & 0x0000FF00 ) >> 8 );
		$outString .= chr( ( $res[1] & 0x00FF0000 ) >> 16 );
		$outString .= chr( shiftRight( ( $res[1] & 0xFF000000 ), 24 ) );
	}
	return trim($outString);
}

function tea_isValid( $plain, $crypt, $keys )
{
	return ( tea_encrypt( $plain, $keys ) == $crypt );
}

?>


#2 Mute

Mute
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 07 July 2006 - 08:39 AM

Bugger, so no one has a clue about this?

#3 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 07 July 2006 - 09:16 AM

well..what's not working about it...any errors..
or is it not doing what you want it to...please explain

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 July 2006 - 09:23 AM

Could yougive an explanation to "doesn't work anymore...."

Whats it supposed to do, what it supposed to return. Whats it returns now

I ran your code and it worked fine, however when I ran your code there was a few perse errors. Parse errors shouldn't stop the script from working, these errors where due to undefined index on your arrays. I suppressed these errors by place in @ symbol at the start of lines, 40, 42, 54 and 56.

The below is what I used to run your function:
echo tea_encrypt('helloworld', 'a0b1c2');
echo '<br>';
echo tea_decrypt('7bacf8ad8397ed1b43b0f0a73eb42a63', 'a0b1c2');
And I got the following:
7bacf8ad8397ed1b43b0f0a73eb42a63
helloworld
Which to me appears to be working. I cannot see why this is not wokring on your new server as it doesnt appear to be using any functions that require extensions.


#5 Mute

Mute
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 07 July 2006 - 11:46 AM

Could you give an explanation to "doesn't work anymore...."


Sorry, that was pretty undescriptive. The code processes fine and I get no errors. It just doesn't encrypt the word (a username on the site) properly anymore and then can't decrypt the word then.

The below is what I used to run your function:

echo tea_encrypt('helloworld', 'a0b1c2');
echo '<br>';
echo tea_decrypt('7bacf8ad8397ed1b43b0f0a73eb42a63', 'a0b1c2');
And I got the following:
7bacf8ad8397ed1b43b0f0a73eb42a63
helloworld
Which to me appears to be working. I cannot see why this is not wokring on your new server as it doesnt appear to be using any functions that require extensions.


That's exactly what I'm stumped about, just seems like simple PHP code that should work anywhere. I ran the same as you above on my local machine and got the same results. When on the server though I get this as the encrypted string....

86bb954593b1edad43b0f0a73eb42a63

By the way, I didn't write this code, it was part of the site when I took over.

#6 Mute

Mute
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 07 July 2006 - 11:52 AM

As far as I can tell, everything works fine till this part of the code....

function shiftRightOne( $n )
{
	$s = $n & 0x80000000;
	$n &= 0x7fffffff;
	$n >>= 1;
	if( $s ) $n |= 0x40000000;
	return $n;
}

And after that is where the two environments start having differences.

#7 Mute

Mute
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 07 July 2006 - 12:35 PM

It actually might be the part of code that is passing to the function above.

#8 Mute

Mute
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 10 July 2006 - 01:41 PM

OK, here's some phpinfo from the old server which works and the new server which doesn't.

Old Server (works)
http://83.245.15.145/~check/test.php

New Server (doesn't work)
http://87.117.198.11...hav0ly/test.php

Are there any blatant differences in variables or some such that could be causing this problem?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users