VB icon

Get XP Key the .NET way

Email
Submitted on: 1/4/2015 8:34:00 AM
By: Terry Olsen (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VB.NET
Views: 1126
 
     To get the CD-Key used to install Windows XP, completely in VB .NET, instead of using the API.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Get XP Key the .NET way
//**************************************
None that I am aware of.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Get XP Key the .NET way
// Description:To get the CD-Key used to install Windows XP, completely in VB .NET, instead of using the API.
// By: Terry Olsen (from psc cd)
//
// Inputs:None.
//
// Returns:The CD-Key that was used to install Windows XP.
//
// Assumes:This is an update to an update. The original code done in VB6 is here: 
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=57164&lngWId=1 
It was converted to VB .NET here: 
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4578&lngWId=10
I have removed all API calls in favor of .NET functions.
There is no error catching, so you'll have to add it yourself if you decide to use the code in your app.
//
// Side Effects:None.
//**************************************

Imports Microsoft.Win32
Module modXPKey
Public Function sGetXPKey() As String
'Open the Registry Key and then get the value (byte array) from the SubKey
Dim RegKey As RegistryKey = _
Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion", False)
Dim bytDPID() As Byte = RegKey.GetValue("DigitalProductID")
'Transfer only the needed bytes into our Key Array
' Key starts at byte 52 and is 15 bytes long.
Dim bytKey(14) As Byte '0-14 = 15 bytes
Array.Copy(bytDPID, 52, bytKey, 0, 15)
'Our "Array" of valid CD-Key characters
Dim strChar As String = "BCDFGHJKMPQRTVWXY2346789"
'Finally, our decoded CD-Key to be returned
Dim strKey As String = ""
'How Microsoft encodes this to begin with, I'd love to know...
'but here's how we decode the byte array into a string
'containing the CD-KEY.
For j As Integer = 0 To 24
Dim nCur As Short = 0
For i As Integer = 14 To 0 Step -1
nCur = CShort(nCur * 256 Xor bytKey(i))
bytKey(i) = CByte(Int(nCur / 24))
nCur = CShort(nCur Mod 24)
Next
strKey = strChar.Substring(nCur, 1) & strKey
Next
'Finally, insert the dashes into the string.
For i As Integer = 4 To 1 Step -1
strKey = strKey.Insert(i * 5, "-")
Next
Return strKey
End Function
End Module


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.