C# 从注册表读取DigitalProductId返回空值
我目前正试图从注册表中获取windows 7安装的产品密钥。但由于某些原因,DigitalProductId返回空值。我不明白它为什么这么做 我可以很好地读取“NT\Current Version”中的其他值。就像“productName”,但说到digitalProductId,我什么也得不到 这是我的密码:C# 从注册表读取DigitalProductId返回空值,c#,null,registry,C#,Null,Registry,我目前正试图从注册表中获取windows 7安装的产品密钥。但由于某些原因,DigitalProductId返回空值。我不明白它为什么这么做 我可以很好地读取“NT\Current Version”中的其他值。就像“productName”,但说到digitalProductId,我什么也得不到 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat
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(
@"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用Microsoft.Win32;
使用系统集合;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
byte[]results=Form1.GetRegistryDigitalProductId(Form1.Key.Windows);
字符串cdKey=Form1.DecodeProductKey(结果);
Show(cdKey,“HWID()”,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
公共枚举密钥{Windows};
公共静态字节[]GetRegistryDigitalProductId(Key)
{
字节[]digitalProductId=null;
注册表项注册表=null;
开关(钥匙)
{
case Key.Windows:
登记处=
Registry.LocalMachine。
打开子键(
@“软件\Microsoft\Windows NT\CurrentVersion”,
假);
打破
}
if(注册表!=null)
{
digitalProductId=registry.GetValue(“digitalProductId”)
作为字节[];
registry.Close();
}
返回digitalProductId;
}
公共静态字符串DecodeProductKey(字节[]digitalProductId)
{
常数int keyStartIndex=52;
const int keyEndIndex=keyStartIndex+15;
字符[]位数=新字符[]
{
‘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=新字符[decodeLength];
ArrayList hexPid=新的ArrayList();
对于(int i=keyStartIndex;i=0;i--)
{
如果((i+1)%6==0)
{
decodedChars[i]='-';
}
其他的
{
int digitMapIndex=0;
对于(int j=decodeStringLength-1;j>=0;j--)
{
int byteValue=(digitMapIndex如果在构建中以64位为目标,则可以按原样使用代码
否则,您将被引导到此处,并且可能找不到您要查找的密钥:
软件\Wow6432Node\
因此,如果必须以32位为目标,请使用以下代码:
var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var reg = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
var digitalProductId = reg.GetValue("DigitalProductId") as byte[];
public static string getKey(string from=@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,string valueName=“DigitalProductId”)
{
RegistryKey配置单元=null;
RegistryKey=null;
尝试
{
var result=string.Empty;
hive=RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,Environment.MachineName);
key=hive.OpenSubKey(from,false);
var k=RegistryValueKind.Unknown;
试试{k=key.GetValueKind(valueName);}
捕获(异常){}
if(k==RegistryValueKind.Unknown)
{
key.Close();
hive.Close();
hive=RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,RegistryView.Registry64);
key=hive.OpenSubKey(from,false);
试试{k=key.GetValueKind(valueName);}
捕获(异常){}
}
if(k==RegistryValueKind.Binary)
{
var-pivot=0;
var bytes=(byte[])key.GetValue(valueName);
var ints=新的int[16];
对于(var i=52;i<67;++i)int[i-52]=字节[i];
对于(变量i=0;i<25;++i)
{
枢轴=0;
对于(var j=14;j>=0;--j)
{
pivot我明白你的意思,是的,我的版本是64位的。但是,当我在regedit中转到“SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion”时,没有列出DigitalProductId…(可能我不完全理解你的意思)如果你要重写开关(键)要正确工作,它会是什么样子?谢谢你的回复。没关系。在玩了你的代码之后,我能够理解你说的话。我让它正确工作。谢谢你的帮助。现在我明白了你对每个构建都要做些什么。
public static string getKey(string from = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", 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";