-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyDecoder.cs
151 lines (140 loc) · 5.83 KB
/
KeyDecoder.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace WUAICT
{
public enum DigitalProductIdVersion
{
/// <summary>
/// All systems up to Windows 7 (Windows 7 and older versions)
/// </summary>
UpToWindows7,
/// <summary>
/// Windows 8 and up (Windows 8 and newer versions)
/// </summary>
Windows8AndUp
}
/// <summary>
/// Provides methods to decode Windows Product Key from registry or from DigitalProductId.
/// This class is static.
/// </summary>
public static class KeyDecoder
{
public static string GetWindowsProductKeyFromRegistry()
{
var localKey =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem
? RegistryView.Registry64
: RegistryView.Registry32);
var registryKeyValue = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion")?.GetValue("DigitalProductId");
if (registryKeyValue == null)
return "Failed to get DigitalProductId from registry";
var digitalProductId = (byte[])registryKeyValue;
localKey.Close();
var isWin8OrUp =
Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 2
||
Environment.OSVersion.Version.Major > 6;
return GetWindowsProductKeyFromDigitalProductId(digitalProductId,
isWin8OrUp ? DigitalProductIdVersion.Windows8AndUp : DigitalProductIdVersion.UpToWindows7);
}
/// <summary>
/// Decodes Windows Product Key from DigitalProductId with specified DigitalProductId version.
/// </summary>
/// <param name="digitalProductId"></param>
/// <param name="digitalProductIdVersion"></param>
/// <returns></returns>
public static string GetWindowsProductKeyFromDigitalProductId(byte[] digitalProductId, DigitalProductIdVersion digitalProductIdVersion)
{
var productKey = digitalProductIdVersion == DigitalProductIdVersion.Windows8AndUp
? DecodeProductKeyWin8AndUp(digitalProductId)
: DecodeProductKey(digitalProductId);
return productKey;
}
/// <summary>
/// Decodes Windows Product Key from the DigitalProductId.
/// This method applies to DigitalProductId from Windows 7 or lower versions of Windows.
/// </summary>
/// <param name="digitalProductId">DigitalProductId to decode</param>
/// <returns>Decoded Windows Product Key as a string</returns>
private static string DecodeProductKey(byte[] digitalProductId)
{
const int keyStartIndex = 52;
const int keyEndIndex = keyStartIndex + 15;
var digits = new[]
{
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R',
'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9',
};
const int decodeLength = 29;
const int decodeStringLength = 15;
var decodedChars = new char[decodeLength];
var hexPid = new ArrayList();
for (var i = keyStartIndex; i <= keyEndIndex; i++)
{
hexPid.Add(digitalProductId[i]);
}
for (var i = decodeLength - 1; i >= 0; i--)
{
// Every sixth char is a separator.
if ((i + 1) % 6 == 0)
{
decodedChars[i] = '-';
}
else
{
// Do the actual decoding.
var digitMapIndex = 0;
for (var j = decodeStringLength - 1; j >= 0; j--)
{
var byteValue = (digitMapIndex << 8) | (byte)hexPid[j];
hexPid[j] = (byte)(byteValue / 24);
digitMapIndex = byteValue % 24;
decodedChars[i] = digits[digitMapIndex];
}
}
}
return new string(decodedChars);
}
/// <summary>
/// Decodes Windows Product Key from the DigitalProductId.
/// This method applies to DigitalProductId from Windows 8 or newer versions of Windows.
/// </summary>
/// <param name="digitalProductId">DigitalProductId to decode</param>
/// <returns>Decoded Windows Product Key as a string</returns>
public static string DecodeProductKeyWin8AndUp(byte[] digitalProductId)
{
var key = String.Empty;
const int keyOffset = 52;
var isWin8 = (byte)((digitalProductId[66] / 6) & 1);
digitalProductId[66] = (byte)((digitalProductId[66] & 0xf7) | (isWin8 & 2) * 4);
const string digits = "BCDFGHJKMPQRTVWXY2346789";
var last = 0;
for (var i = 24; i >= 0; i--)
{
var current = 0;
for (var j = 14; j >= 0; j--)
{
current = current * 256;
current = digitalProductId[j + keyOffset] + current;
digitalProductId[j + keyOffset] = (byte)(current / 24);
current = current % 24;
last = current;
}
key = digits[current] + key;
}
var keypart1 = key.Substring(1, last);
var keypart2 = key.Substring(last + 1, key.Length - (last + 1));
key = keypart1 + "N" + keypart2;
for (var i = 5; i < key.Length; i += 6)
{
key = key.Insert(i, "-");
}
return key;
}
}
}