Encoding with php and Decoding in c++ problem


#1

I’m testing some code to generate user licenses on a server with the following php code that I got from here:

<?php
include ('BigInteger.php'); 
$privatePart1 = "72fa7824e0c3208d";
$privatePart2 = "8fb9162f9c73f563";

function applyToValue ($message, $key_part1, $key_part2)
{
	$result = new Math_BigInteger();
	$zero   = new Math_BigInteger();
	$value  = new Math_BigInteger (strrev ($message), 256);
	$part1  = new Math_BigInteger ($key_part1, 16);
	$part2  = new Math_BigInteger ($key_part2, 16);
	while (! $value->equals ($zero))
	{
		$result = $result->multiply ($part2);
		list ($value, $remainder) = $value->divide ($part2);
		$result = $result->add ($remainder->modPow ($part1, $part2));
	}
	return $result->toHex();
}

$input = "Joe User,joe@user.com,myProductName";

$userLicense = applyToValue ($input, $privatePart1, $privatePart2);
echo $userLicense;
?>

That gives me “0e34cc40929df005a5d0c3106069a6d46d62518f62569a7e2938574c4f8903de1c3745618c4df562” output to the browser screen (which I would email to the user) and use it inside my plugin like this:

void decode()
{
    String fromServer = "0e34cc40929df005a5d0c3106069a6d46d62518f62569a7e2938574c4f8903de1c3745618c4df562";
    
    RSAKey publicKey("5,8fb9162f9c73f563");
    BigInteger val;
    val.parseString(fromServer, 10);    
    publicKey.applyToValue(val);
    String decoded = val.toString(10);
    DBG ("decoded: " + decoded);
}

I expect to see “Joe User,joe@user.com,myProductName”, but I’m getting “557808617549894482492343375676741911471126211295016976348”, so obviously I’m either missing a step or I don’t completely understand what’s going on.

Sorry for the ugly testing code, but I’m just trying to get it working before I’m clean it up.

Anyone? Thanks.


#2

I use the same stuff… it’s been a while, so I’m not really deep in the matter right now, but I observe two differences:

  • in my PHP script I wrote strref($result->toBytes()) instead of toHex()
  • your output is obviously an integer now… try val.toMemoryBlock().toString

Hope this helps.


#3

Thanks so much for the help. Yes, both of those lines were suspect.

The C++ line should have been:

   val.toMemoryBlock().toString();`

But I tried strref($result->toBytes()) but then the above line crashed in a string assertion. Well of course…it’s not a string. So, I tried:

    return $result->toString();

…and now it all works.

Thanks again.