转自:https://www.cnblogs.com/hanzhaoxin/archive/2013/01/04/2844191.html
例子下载地址:https://download.csdn.net/download/cgs_______/11862095
注册机类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace ExportLshTool
{
class SoftReg
{
/// <summary>
/// 获取硬盘卷标号
/// </summary>
/// <returns></returns>
public string GetDiskVolumeSerialNumber()
{
ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
/// <summary>
/// 获取Cpu序列号
/// </summary>
/// <returns></returns>
public string GetCpu()
{
string strCpu = null;
ManagementClass myCpu = new ManagementClass("win32_Processor");
ManagementObjectCollection myCpuCollection = myCpu.GetInstances();
foreach (ManagementObject myObject in myCpuCollection)
{
strCpu = myObject.Properties["Processorid"].Value.ToString();
}
return strCpu;
}
/// <summary>
/// 生成机器码
/// </summary>
/// <returns></returns>
public string GetMNum()
{
string strNum = GetCpu() + GetDiskVolumeSerialNumber();
return strNum.Substring(0, 24);
}
public int[] intCode = new int[127];//存储密匙
public char[] charCode = new char[25];//存储ASCII码
public int[] intNumber = new int[25];//存储ASCII码值
/// <summary>
/// 初始化密匙
/// </summary>
public void SetIntCode()
{
for (int i = 1; i < intCode.Length; i++)
{
intCode[i] = i % 9;
}
}
/// <summary>
/// 生成注册码
/// </summary>
/// <returns></returns>
public string GetRNum()
{
SetIntCode();
string strMNum = GetMNum();
for (int i = 1; i < charCode.Length; i++)//存储机器码
{
charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));
}
for (int i = 1; i < intNumber.Length; i++)//改变ASCII码值
{
intNumber[i] = Convert.ToInt32(charCode[i]) + intCode[Convert.ToInt32(charCode[i])];
}
string strAsciiName = "";
for (int i = 1; i < intNumber.Length; i++)
{
if ((intNumber[i] >= 48 && intNumber[i] <= 57) ||
(intNumber[i] >= 65 && intNumber[i] <= 90) ||
(intNumber[i] >= 97 && intNumber[i] <= 122))
{
strAsciiName += Convert.ToChar(intNumber[i]).ToString();
}
else if (intNumber[i] > 122) //判断如果大于Z
{
strAsciiName += Convert.ToChar(intNumber[i] - 10).ToString();
}
else
{
strAsciiName += Convert.ToChar(intNumber[i] - 9).ToString();
}
}
return strAsciiName;
}
/// <summary>
/// 生成注册码
/// </summary>
/// <returns></returns>
public string GetRNum(string strMNum)
{
SetIntCode();
for (int i = 1; i < charCode.Length; i++)//存储机器码
{
charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));
}
for (int i = 1; i < intNumber.Length; i++)//改变ASCII码值
{
intNumber[i] = Convert.ToInt32(charCode[i]) + intCode[Convert.ToInt32(charCode[i])];
}
string strAsciiName = "";
for (int i = 1; i < intNumber.Length; i++)
{
if ((intNumber[i] >= 48 && intNumber[i] <= 57) ||
(intNumber[i] >= 65 && intNumber[i] <= 90) ||
(intNumber[i] >= 97 && intNumber[i] <= 122))
{
strAsciiName += Convert.ToChar(intNumber[i]).ToString();
}
else if (intNumber[i] > 122) //判断如果大于Z
{
strAsciiName += Convert.ToChar(intNumber[i] - 10).ToString();
}
else
{
strAsciiName += Convert.ToChar(intNumber[i] - 9).ToString();
}
}
return strAsciiName;
}
}
}
写入注册表操作(需要using Microsoft.Win32)
RegistryKey retKey = Registry.CurrentUser.OpenSubKey("SoftWare", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txt_rnum.Text);
retKey.SetValue("UserName", "Rsoft");
读注册表操作(需要using Microsoft.Win32)
RegistryKey retKey = Registry.CurrentUser.OpenSubKey("SoftWare", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");
foreach (string strRNum in retKey.GetSubKeyNames())
{
if (strRNum == softReg.GetRNum())
{
//逻辑
}
}