Tuesday, August 22, 2006

Extract Windows Product Key !

Just came across this while searching something on "Google", looked very interesting.
Everyone will be wondering how to get this product key if not mentioned it on the CD label.
But I haven't tried this yet, cause am not a windows programmer ;)
Let me know the results about this!

BSTR GetProductKey()
/*
Window Product Key Extract

*/
{
CString strResult; //Return a Window Product Key

HKEY hRegistryKey; //Registry Handler
BYTE *DigitalProductID; //Digital Product Key Value
DWORD DataLength; //Digital Product Key Length

BYTE ProductKeyExtract [15]; //Extract Key

char sCDKey [256]; //Temp, adding a Window Product Key

long ByteCounter; //Counter
long ByteConvert; //Convert

int nCur; //XOR calculate


char *KeyChars[] = {
"B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y",
"2","3","4","6","7","8","9",NULL
};

// HKLM\\SOFTWARE\\MICROSOFT\\Windows NT\\CurrentVersion 열기
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NT_CURRENT,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, &hRegistryKey) == ERROR_SUCCESS)
{

DataLength = 164;

//Allocate Memory
DigitalProductID = (BYTE *)malloc(DataLength);

//Memory Initializationd
memset(DigitalProductID, 0, DataLength);

//Digital Product Key Open

if(RegQueryValueEx(hRegistryKey, "DigitalProductId",
NULL, NULL, DigitalProductID, &DataLength) == ERROR_SUCCESS)
{
//reading a value start position 52, by 66
for(ByteCounter=52; ByteCounter<=66; ByteCounter++)
{
ProductKeyExtract[ByteCounter - 52] = DigitalProductID[ByteCounter];
}

//Last Indexer
ProductKeyExtract[sizeof(ProductKeyExtract)] = NULL;
}
}

//Start Converting job, Next Step

memset(sCDKey, 0, sizeof(sCDKey));

for(ByteCounter=24; ByteCounter>=0; ByteCounter--)
{
nCur = 0;

for(ByteConvert=14; ByteConvert>=0; ByteConvert--)
{

nCur = (nCur * 256) ^ ProductKeyExtract[ByteConvert]; //XOR계산
ProductKeyExtract[ByteConvert] = nCur / 24;
nCur = nCur % 24;
}

strrev(sCDKey);
strcat(sCDKey, KeyChars[nCur]);
strrev(sCDKey);

//Insert "-"

if(!(ByteCounter % 5) && (ByteCounter))
{
strrev(sCDKey);

strcat(sCDKey, "-");

strrev(sCDKey);
}
}

//Insert Product Key into Return value

strResult.Format("%s", sCDKey);

//Close Registry
RegCloseKey(hRegistryKey);

//Relese Memory
if(DigitalProductID) free(DigitalProductID);

return strResult.AllocSysString();
}

Note: Please mention the results in the comments section.