Skip to content

Instantly share code, notes, and snippets.

@schakko
Created May 28, 2011 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schakko/996898 to your computer and use it in GitHub Desktop.
Save schakko/996898 to your computer and use it in GitHub Desktop.
Decoding the WindowsXP product key / serial number from the digital product id with PHP
<?php
/**
* decodes the product key and returns it as "-"-seperated string
* @param array of bytes
* @return string
*/
function decodeProductKey($aHexSrc)
{
// base24-encoded
$arrDigits = array('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9');
// get ANSI-code
for ($i = 0, $m = sizeof($arrDigits); $i < $m; $i++)
{
$arrDigits[$i] = ord($arrDigits[$i]);
}
$arrHexDigitalPID = $aHexSrc;
$rCdKey = "";
for ($i = 24, $m = 0; $i >= 0; $i--)
{
$iCurrent = 0;
for ($j = 14, $n = 0; $j >= 0; $j--)
{
$iCurrent = ($iCurrent << 8) + $arrHexDigitalPID[$j];
$arrHexDigitalPID[$j] = ($iCurrent / 24);
$iCurrent = $iCurrent % 24;
}
$rCdKey = chr($arrDigits[$iCurrent]) . $rCdKey;
if ($i%5 == 0 && $i != 0)
{
$rCdKey = "-" . $rCdKey;
}
}
return $rCdKey;
}
/**
* retrieves the encoded product key from whereever
* @return string
*/
function loadEncodedProductKey()
{
$rData = extractEncodedProductKey("YOUR DIGITAL PRODUCTID");
return $rData;
}
/**
* extract the encoded product key from the digital product id
* @param binary string of digital product ID
* @return array
*/
function extractEncodedProductKey($aDigitalProductId)
{
$startOffset = 52;
$sLen = 15;
$data = toArray($aDigitalProductId);
$rData = array_slice($data, $startOffset, $sLen);
return $rData;
}
/**
* converts a space-seperated string to an array
* @param string
* @return array
*/
function toArray($aBinaryData)
{
$arr = explode(" ", $aBinaryData);
$rT = array();
for ($i = 0, $m = sizeof($arr); $i < $m; $i++)
{
array_push($rT, hexdec($arr[$i]));
}
return $rT;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment