注册码生成
根据硬件信息生成注册码
得到硬件信息
using System.Management;(手动添加引用下dll,在.net库里)
代码
/// <summary>
/// 得到CpuId
/// </summary>
/// <returns></returns>
public string GetCpuId()
{
string cpuId = null;
ManagementClass mClass = new ManagementClass("win32_Processor");
ManagementObjectCollection mO = mClass.GetInstances();
foreach (ManagementObject myObject in mO)
{
cpuId = myObject.Properties["Processorid"].Value.ToString();
break;
}
return cpuId;
}
/// <summary>
/// 得到HD id
/// </summary>
/// <returns></returns>
public string GetHdId()
{
string hDId = null;
ManagementClass mClass = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection mOColl = mClass.GetInstances();
foreach (ManagementObject mO in mOColl)
{
hDId = (string)mO.Properties["Model"].Value;
}
return hDId;
}
/// <summary>
/// 获取网卡硬件地址 Network Card
/// </summary>
/// <returns>string </returns>
public static string GetNetworkCardAddress()
{
string nCAddress = null;
ManagementClass mClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection mOC = mClass.GetInstances();
foreach (ManagementObject mO in mOC)
{
if ((bool)mO["IPEnabled"] == true)
nCAddress = mO["MacAddress"].ToString();
mO.Dispose();
}
return nCAddress;
}
/// 得到CpuId
/// </summary>
/// <returns></returns>
public string GetCpuId()
{
string cpuId = null;
ManagementClass mClass = new ManagementClass("win32_Processor");
ManagementObjectCollection mO = mClass.GetInstances();
foreach (ManagementObject myObject in mO)
{
cpuId = myObject.Properties["Processorid"].Value.ToString();
break;
}
return cpuId;
}
/// <summary>
/// 得到HD id
/// </summary>
/// <returns></returns>
public string GetHdId()
{
string hDId = null;
ManagementClass mClass = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection mOColl = mClass.GetInstances();
foreach (ManagementObject mO in mOColl)
{
hDId = (string)mO.Properties["Model"].Value;
}
return hDId;
}
/// <summary>
/// 获取网卡硬件地址 Network Card
/// </summary>
/// <returns>string </returns>
public static string GetNetworkCardAddress()
{
string nCAddress = null;
ManagementClass mClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection mOC = mClass.GetInstances();
foreach (ManagementObject mO in mOC)
{
if ((bool)mO["IPEnabled"] == true)
nCAddress = mO["MacAddress"].ToString();
mO.Dispose();
}
return nCAddress;
}
代码
private void button1_Click(object sender, EventArgs e)
{
//硬件的信息存在的硬件Id
label1.Text = GetCpuId() + GetHdId() + GetNetworkCardAddress();
string[] hwId = new string[24];
for (int i = 0; i < 24; i++)//把字符赋给数组
{
hwId[i] = label1.Text.Substring(i, 1);
}
label2.Text = "";
//从数组随机抽取24个字符组成新的字符生成机器三
Random rd= new Random();
for (int i = 0; i < 24; i++)
{
label2.Text += hwId[rd.Next(0, 24)];
}
}
public int[] intCode = new int[127];//用于存密钥
public void setIntCode()//给数组赋值个小于10的随机数
{
Random ra = new Random();
for (int i = 1; i < intCode.Length; i++)
{
intCode[i] = ra.Next(0, 9);
}
}
public int[] intNumber = new int[25];//用于存机器码的Ascii值
public char[] Charcode = new char[25];//存储机器码字
//生成注册码
private void button2_Click(object sender, EventArgs e)
{
if (label2.Text != "")
{ //把机器码存入数组中
setIntCode();//初始化127位数组
for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
{
Charcode[i] = Convert.ToChar(label2.Text.Substring(i - 1, 1));
}//
for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
{
intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
}
string strAsciiName = null;//用于存储机器码
for (int j = 1; j < intNumber.Length; j++)
{
//MessageBox.Show((Convert.ToChar(intNumber[j])).ToString());
if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else//判断字符ASCII值不在以上范围内
{
if (intNumber[j] > 122)//判断字符ASCII值是否大于z
{
strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
}
else
{
strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
}
}
label3.Text = strAsciiName;//得到注册码
}
}
else
{
MessageBox.Show("请选生成机器码", "注册提示");
}
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = label3.Text;
if (label3.Text != "")
{
if (textBox1.Text.TrimEnd().Equals(label3.Text.TrimEnd()))
{
Microsoft.Win32.RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software", true).CreateSubKey("ZHY").CreateSubKey("ZHY.INI").CreateSubKey(textBox1.Text.TrimEnd());
retkey.SetValue("UserName", "MySoft");
MessageBox.Show("注册成功");
}
else { MessageBox.Show("注册码输入错误"); }
}
else
{
MessageBox.Show("请生成注册码", "注册提示");
}
}
{
//硬件的信息存在的硬件Id
label1.Text = GetCpuId() + GetHdId() + GetNetworkCardAddress();
string[] hwId = new string[24];
for (int i = 0; i < 24; i++)//把字符赋给数组
{
hwId[i] = label1.Text.Substring(i, 1);
}
label2.Text = "";
//从数组随机抽取24个字符组成新的字符生成机器三
Random rd= new Random();
for (int i = 0; i < 24; i++)
{
label2.Text += hwId[rd.Next(0, 24)];
}
}
public int[] intCode = new int[127];//用于存密钥
public void setIntCode()//给数组赋值个小于10的随机数
{
Random ra = new Random();
for (int i = 1; i < intCode.Length; i++)
{
intCode[i] = ra.Next(0, 9);
}
}
public int[] intNumber = new int[25];//用于存机器码的Ascii值
public char[] Charcode = new char[25];//存储机器码字
//生成注册码
private void button2_Click(object sender, EventArgs e)
{
if (label2.Text != "")
{ //把机器码存入数组中
setIntCode();//初始化127位数组
for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
{
Charcode[i] = Convert.ToChar(label2.Text.Substring(i - 1, 1));
}//
for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
{
intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
}
string strAsciiName = null;//用于存储机器码
for (int j = 1; j < intNumber.Length; j++)
{
//MessageBox.Show((Convert.ToChar(intNumber[j])).ToString());
if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else//判断字符ASCII值不在以上范围内
{
if (intNumber[j] > 122)//判断字符ASCII值是否大于z
{
strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
}
else
{
strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
}
}
label3.Text = strAsciiName;//得到注册码
}
}
else
{
MessageBox.Show("请选生成机器码", "注册提示");
}
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = label3.Text;
if (label3.Text != "")
{
if (textBox1.Text.TrimEnd().Equals(label3.Text.TrimEnd()))
{
Microsoft.Win32.RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software", true).CreateSubKey("ZHY").CreateSubKey("ZHY.INI").CreateSubKey(textBox1.Text.TrimEnd());
retkey.SetValue("UserName", "MySoft");
MessageBox.Show("注册成功");
}
else { MessageBox.Show("注册码输入错误"); }
}
else
{
MessageBox.Show("请生成注册码", "注册提示");
}
}