c# 无法检索解密密钥
注意:今天早上在我的博客上发布了此内容,但也感到字节社区可以从中受益。我已经在不同的论坛上多次问过这个问题,并认为我会开发一种用于检索Windows产品密钥的解决方案。 实际上,由于密钥在注册表中被编码为字节数组,因此实际上比我最初想的要深得多,因此我们必须对其进行解码才能获得实际的密钥。
我们需要做的第一件事是声明解码和保持解码后的产品密钥所需的所有局部变量:
//first byte offset
const int start = 52;
//last byte offset
const int end = start + 15;
//decoded key length
const int length = 29;
//decoded key in byte form
const int decoded = 15;
//char[] for holding the decoded product key
var decodedKey = new char[length];
//List<byte> to hold the key bytes
var keyHex = new List<byte>();
//list to hold possible alpha-numeric characters
//that may be in the product key
var charsInKey = new List<char>()
{
'B', 'C', 'D', 'F', 'G', 'H',
'J', 'K', 'M', 'P', 'Q', 'R',
'T', 'V', 'W', 'X', 'Y', '2',
'3', '4', '6', '7', '8', '9'
};
下一步是将所有字节添加到我们的List <byte>中:
//add all bytes to our list
for (var i = start; i <= end; i++)
keyHex.Add(id[i]);
接下来,我们进行实际的大量列出(解码字节数组)以获取产品密钥:
//now the decoding starts
for (var i = length - 1; i >= 0; i--)
switch ((i + 1) % 6)
{
//if the calculation is 0 (zero) then add the seperator
case 0:
decodedKey[i] = '-';
break;
default:
var idx = 0;
for (var j = decoded - 1; j >= 0; j--)
{
var @value = (idx << 8) | keyHex[j];
keyHex[j] = (byte) (@value/24);
idx = @value%24;
decodedKey[i] = charsInKey[idx];
}
break;
}
最后将字节数组转换为包含产品密钥的字符串:
return new string(decodedKey);
注意:我们使用字符串类的新构造函数将char数组转换为常规字符串。
好的,现在我们有了一种解码产品密钥的方法,现在介绍如何使用它。 我创建了一个带有文本框和按钮的简单WinForm。 我要做的第一件事是打开以下键“
SOFTWARE \\ Microsoft \\ Windows NT \\ CurrentVersion “(在HKEY_LOCAL_MACHINE中 )。然后,我获取DigitalProductId的值并将字节数组传递给解码函数。按钮单击事件如下所示:
private void FindProductKeyClick(object sender, EventArgs e)
{
byte[] id = null;
var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
if (regKey != null) id = regKey.GetValue("DigitalProductId") as byte[];
ProductKeyTextBox.Text = DecodeKeyByteArray(id);
}
现在运行您的应用程序并获取Windows产品密钥。
看到那样的浪费与您现在想的一样困难。
希望对您有所帮助,并感谢您的阅读。
编码愉快!
翻译自: https://bytes.com/topic/c-sharp/insights/941406-retrieve-your-windows-product-key-c
c# 无法检索解密密钥