c# 實現一機一碼
爲自己寫的程序加一個註冊功能吧。生成的機器號是根據CPU和硬盤號來的,根據自己的需求改成是否是隨機生成。
代碼直接粘貼到新建類覆蓋原代碼就能直接用了。
using System;
using System.Management;
using System.Security.Cryptography;
using System.Text;
namespace RegisterClass
{
class RegisterClass
{
//步驟一: 獲得CUP序列號和硬盤序列號的實現代碼如下:
//獲得CPU的序列號
bool Stupids = true;
bool Cat = false;
public string getCpu()
{
string strCpu = null;
ManagementClass myCpu = new ManagementClass("win32_Processor");
ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
foreach( ManagementObject myObject in myCpuConnection)
{
strCpu = myObject.Properties["Processorid"].Value.ToString();
break;
}
return strCpu;
}
//取得設備硬盤的卷標號
public string GetDiskVolumeSerialNumber()
{
ManagementClass mc =
new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObject disk =
new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
//步驟二: 收集硬件信息生成機器碼, 代碼如下:
//生成機器碼
public string CreateCode()
{
string temp = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬盤序列號
string[] strid = new string[24];//
for (int i = 0; i < 24; i++)//把字符賦給數組
{
strid[i] = temp.Substring(i, 1);
}
temp = "";
//Random rdid = new Random();
for (int i = 0; i < 24; i++)//從數組隨機抽取24個字符組成新的字符生成機器三
{
//temp += strid[rdid.Next(0, 24)];
temp += strid[i+3>=24?0:i+3];
}
return GetMd5(temp);
}
//步驟三: 使用機器碼生成軟件註冊碼, 代碼如下:
//使用機器碼生成註冊碼
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);
//}
for (int i = 1; i < intCode.Length; i++)
{
intCode[i] = i + 3 > 9 ? 0 : i + 3;
}
}
public int[] intNumber = new int[25];//用於存機器碼的Ascii值
public char[] Charcode = new char[25];//存儲機器碼字
//生成註冊碼
public string GetCode(string code)
{
if (code != "")
{
//把機器碼存入數組中
setIntCode();//初始化127位數組
for (int i = 1; i < Charcode.Length; i++)//把機器碼存入數組中
{
Charcode[i] = Convert.ToChar(code.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());
//判斷字符ASCII值是否0-9之間
if (intNumber[j] >= 48 && intNumber[j] <= 57)
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
//判斷字符ASCII值是否A-Z之間
else if (intNumber[j] >= 65 && intNumber[j] <= 90)
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
//判斷字符ASCII值是否a-z之間
else if (intNumber[j] >= 97 && intNumber[j] <= 122)
{
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;//得到註冊碼
}
return strAsciiName;
}
else
{
return "";
}
}
//步驟四: 用戶輸入註冊碼註冊軟件, 演示代碼如下:
//註冊
public bool RegistIt(string currentCode,string realCode)
{
if (realCode != "")
{
if (currentCode.TrimEnd().Equals(realCode.TrimEnd()))
{
Microsoft.Win32.RegistryKey retkey =
Microsoft.Win32.Registry.CurrentUser.
OpenSubKey("software", true).CreateSubKey("StupidsCat").
CreateSubKey("StupidsCat.ini").
CreateSubKey(currentCode.TrimEnd());
retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
retkey = Microsoft.Win32.Registry.LocalMachine.
OpenSubKey("software", true).CreateSubKey("StupidsCat").
CreateSubKey("StupidsCat.ini").
CreateSubKey(currentCode.TrimEnd());
retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
return Stupids;
}
else
{
return Cat;
}
}
else { return Cat; }
}
public bool BoolRegist(string sn)
{
string[] keynames; bool flag = false;
Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser;
try
{
keynames = localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();
foreach (string name in keynames)
{
if (name == "StupidsCat")
{
if (localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")
flag = true;
}
}
keynames = userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();
foreach (string name in keynames)
{
if (name == "StupidsCat")
{
if (flag && userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")
return true;
}
}
return false;
}
catch
{
return false;
}
finally
{
localRegKey.Close();
userRegKey.Close();
}
}
public string GetMd5(object text)
{
string path = text.ToString();
MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider();
Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(text.ToString());
Byte[] byteResult = MD5Pro.ComputeHash(buffer);
string md5result = BitConverter.ToString(byteResult).Replace("-", "");
return md5result;
}
}
}
代碼直接粘貼到新建類覆蓋原代碼就能直接用了。
using System;
using System.Management;
using System.Security.Cryptography;
using System.Text;
namespace RegisterClass
{
class RegisterClass
{
//步驟一: 獲得CUP序列號和硬盤序列號的實現代碼如下:
//獲得CPU的序列號
bool Stupids = true;
bool Cat = false;
public string getCpu()
{
string strCpu = null;
ManagementClass myCpu = new ManagementClass("win32_Processor");
ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
foreach( ManagementObject myObject in myCpuConnection)
{
strCpu = myObject.Properties["Processorid"].Value.ToString();
break;
}
return strCpu;
}
//取得設備硬盤的卷標號
public string GetDiskVolumeSerialNumber()
{
ManagementClass mc =
new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObject disk =
new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
//步驟二: 收集硬件信息生成機器碼, 代碼如下:
//生成機器碼
public string CreateCode()
{
string temp = getCpu() + GetDiskVolumeSerialNumber();//獲得24位Cpu和硬盤序列號
string[] strid = new string[24];//
for (int i = 0; i < 24; i++)//把字符賦給數組
{
strid[i] = temp.Substring(i, 1);
}
temp = "";
//Random rdid = new Random();
for (int i = 0; i < 24; i++)//從數組隨機抽取24個字符組成新的字符生成機器三
{
//temp += strid[rdid.Next(0, 24)];
temp += strid[i+3>=24?0:i+3];
}
return GetMd5(temp);
}
//步驟三: 使用機器碼生成軟件註冊碼, 代碼如下:
//使用機器碼生成註冊碼
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);
//}
for (int i = 1; i < intCode.Length; i++)
{
intCode[i] = i + 3 > 9 ? 0 : i + 3;
}
}
public int[] intNumber = new int[25];//用於存機器碼的Ascii值
public char[] Charcode = new char[25];//存儲機器碼字
//生成註冊碼
public string GetCode(string code)
{
if (code != "")
{
//把機器碼存入數組中
setIntCode();//初始化127位數組
for (int i = 1; i < Charcode.Length; i++)//把機器碼存入數組中
{
Charcode[i] = Convert.ToChar(code.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());
//判斷字符ASCII值是否0-9之間
if (intNumber[j] >= 48 && intNumber[j] <= 57)
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
//判斷字符ASCII值是否A-Z之間
else if (intNumber[j] >= 65 && intNumber[j] <= 90)
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
//判斷字符ASCII值是否a-z之間
else if (intNumber[j] >= 97 && intNumber[j] <= 122)
{
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;//得到註冊碼
}
return strAsciiName;
}
else
{
return "";
}
}
//步驟四: 用戶輸入註冊碼註冊軟件, 演示代碼如下:
//註冊
public bool RegistIt(string currentCode,string realCode)
{
if (realCode != "")
{
if (currentCode.TrimEnd().Equals(realCode.TrimEnd()))
{
Microsoft.Win32.RegistryKey retkey =
Microsoft.Win32.Registry.CurrentUser.
OpenSubKey("software", true).CreateSubKey("StupidsCat").
CreateSubKey("StupidsCat.ini").
CreateSubKey(currentCode.TrimEnd());
retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
retkey = Microsoft.Win32.Registry.LocalMachine.
OpenSubKey("software", true).CreateSubKey("StupidsCat").
CreateSubKey("StupidsCat.ini").
CreateSubKey(currentCode.TrimEnd());
retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
return Stupids;
}
else
{
return Cat;
}
}
else { return Cat; }
}
public bool BoolRegist(string sn)
{
string[] keynames; bool flag = false;
Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser;
try
{
keynames = localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();
foreach (string name in keynames)
{
if (name == "StupidsCat")
{
if (localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")
flag = true;
}
}
keynames = userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();
foreach (string name in keynames)
{
if (name == "StupidsCat")
{
if (flag && userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")
return true;
}
}
return false;
}
catch
{
return false;
}
finally
{
localRegKey.Close();
userRegKey.Close();
}
}
public string GetMd5(object text)
{
string path = text.ToString();
MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider();
Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(text.ToString());
Byte[] byteResult = MD5Pro.ComputeHash(buffer);
string md5result = BitConverter.ToString(byteResult).Replace("-", "");
return md5result;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.