我目前正在尝试从注册表中获取Windows 7安装的产品密钥。但是由于某种原因,DigitalProductId恢复了无效。而且我不知道为什么这样做。
我可以在" NT 当前版本"中读取其他值。就像" productName"一样,但是当涉及到数字产品时,我什么也没得到。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
byte[] results = Form1.GetRegistryDigitalProductId(Form1.Key.Windows);
string cdKey = Form1.DecodeProductKey(results);
MessageBox.Show(cdKey, "HWID()", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public enum Key { Windows };
public static byte[] GetRegistryDigitalProductId(Key key)
{
byte[] digitalProductId = null;
RegistryKey registry = null;
switch (key)
{
case Key.Windows:
registry =
Registry.LocalMachine.
OpenSubKey(
@"SOFTWAREMicrosoftWindows NTCurrentVersion",
false);
break;
}
if (registry != null)
{
digitalProductId = registry.GetValue("DigitalProductId")
as byte[];
registry.Close();
}
return digitalProductId;
}
public static string DecodeProductKey(byte[] digitalProductId)
{
const int keyStartIndex = 52;
const int keyEndIndex = keyStartIndex + 15;
char[] digits = new 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',
};
const int decodeLength = 29;
const int decodeStringLength = 15;
char[] decodedChars = new char[decodeLength];
ArrayList hexPid = new ArrayList();
for (int i = keyStartIndex; i <= keyEndIndex; i++)
{
hexPid.Add(digitalProductId[i]);
}
for (int i = decodeLength - 1; i >= 0; i--)
{
if ((i + 1) % 6 == 0)
{
decodedChars[i] = '-';
}
else
{
int digitMapIndex = 0;
for (int j = decodeStringLength - 1; j >= 0; j--)
{
int byteValue = (digitMapIndex << 8) | (byte)hexPid[j];
hexPid[j] = (byte)(byteValue / 24);
digitMapIndex = byteValue % 24;
decodedChars[i] = digits[digitMapIndex];
}
}
}
return new string(decodedChars);
}
}
}
我是C#的新手,我正在尝试遵循本指南。http://www.ultimateprogrammingsmings.info/2013/05/how-to-to-get-windows-product-key-in-c.html
但是我很难正确工作。
谢谢
如果您在构建中靶向64位,则可以使用代码。
否则,您会在这里定向,并且您可能找不到要寻找的钥匙:
软件 WOW6432NODE ...
因此,如果您必须针对32位:
使用此代码var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var reg = baseKey.OpenSubKey(@"SOFTWAREMicrosoftWindows NTCurrentVersion", false);
var digitalProductId = reg.GetValue("DigitalProductId") as byte[];
public static string getKey(string from = @"SOFTWAREMicrosoftWindows NTCurrentVersion", string valueName = "DigitalProductId")
{
RegistryKey hive = null;
RegistryKey key = null;
try
{
var result = string.Empty;
hive = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName);
key = hive.OpenSubKey(from, false);
var k = RegistryValueKind.Unknown;
try { k = key.GetValueKind(valueName); }
catch (Exception) { }
if (k == RegistryValueKind.Unknown)
{
key.Close();
hive.Close();
hive = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
key = hive.OpenSubKey(from, false);
try { k = key.GetValueKind(valueName); }
catch (Exception) { }
}
if (k == RegistryValueKind.Binary)
{
var pivot = 0;
var bytes = (byte[])key.GetValue(valueName);
var ints = new int[16];
for (var i = 52; i < 67; ++i) ints[i - 52] = bytes[i];
for (var i = 0; i < 25; ++i)
{
pivot = 0;
for (var j = 14; j >= 0; --j)
{
pivot <<= 8;
pivot ^= ints[j];
ints[j] = ((int)Math.Truncate(pivot / 24.0));
pivot %= 24;
}
result = possible_chars[pivot] + result;
if ((i % 5 == 4) && (i != 24))
{
result = "-" + result;
}
}
}
return result;
}
catch (Exception) { return null; }
finally
{
key?.Close();
hive?.Close();
}
}
private static readonly string possible_chars = "BCDFGHJKMPQRTVWXY2346789";