1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
namespace CD_Key
{
public class Win32
{
#region Properties
/// <summary>
/// Reads the Windows CD key from the registry and returns it as string separated by '-' chars.
/// </summary>
public static string WindowsCDKey
{
get
{
RegistryKey rKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
byte[] rpk = (byte[])rKey.GetValue("DigitalProductId", new byte[0]);
string strKey = "";
const int iRPKOffset = 52;
const string strPossibleChars = "BCDFGHJKMPQRTVWXY2346789";
int i = 28;
do
{
long lAccu = 0;
int j = 14;
do
{
lAccu *= 256; lAccu += Convert.ToInt64(rpk[iRPKOffset + j]);
rpk[iRPKOffset + j] =
Convert.ToByte(
Convert.ToInt64(Math.Floor((float)lAccu / 24.0f)) & Convert.ToInt64(255)
);
lAccu %= 24;
j -= 1;
}
while (j >= 0);
i -= 1;
strKey = strPossibleChars[(int)lAccu].ToString() + strKey;
if ((0 == ((29 - i) % 6)) && (-1 != i))
{
i -= 1;
strKey = "-" + strKey;
}
}
while (i >= 0);
return strKey;
}
}
/// <summary>
/// Reads the Windows CD key from the registry and returns it as string array.
/// </summary>
public static string[] WindowsCDKeyParts
{
get
{
string[] strKeyParts = WindowsCDKey.Split('-');
return strKeyParts;
}
}
#endregion
}
}
|