以下简单介绍一下,软件的注册的实现过程。
SoftReg类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management; namespace CommonClass { public 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> /// 获取网卡硬件地址 /// </summary> /// <returns></returns> public static string GetMacAddress() { string mac = string.Empty; using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration")) { ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { mac = mo["MacAddress"].ToString(); break; } } } return mac.Replace(":", ""); } /// <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(); string strNum = GetMacAddress() + GetDiskVolumeSerialNumber(); string strMNum = strNum.Substring(0, 20); //截取前20位作为机器码 return strMNum; } public int[] intCode = new int[127]; //存储密钥 public char[] charCode = new char[21]; //存储ASCII码 public int[] intNumber = new int[21]; //存储ASCII码值 //初始化密钥 public void SetIntCode() { for (int i = 1; i < intCode.Length; i++) { intCode[i] = i % 9; } } /// <summary> /// 生成注册码 /// </summary> /// <returns></returns> public string GetRNum(string xStr) { SetIntCode(); string strMNum = xStr; for (int i = 1; i < charCode.Length; i++) //存储机器码 { charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1)); } for (int j = 1; j < intNumber.Length; j++) //改变ASCII码值 { intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])]; } string strAsciiName = ""; //注册码 for (int k = 1; k < intNumber.Length; k++) //生成注册码 { if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k] <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判断如果在0-9、A-Z、a-z之间 { strAsciiName += Convert.ToChar(intNumber[k]).ToString(); } else if (intNumber[k] > 122) //判断如果大于z { strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString(); } else { strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString(); } } return strAsciiName; } } }
注册窗体:
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 ImClient.Comm.UI; using ImClient.Comm; using CommonClass; using Microsoft.Win32; using ImCommon.Util; using ImCommon.FrameWork; namespace ImClient.UI { public partial class SoftRegForm : BaseForm { public SoftRegForm() { InitializeComponent(); } SoftReg softreg = new SoftReg(); #region * FormLoad private void SoftRegForm_Load(object sender, EventArgs e) { this.txtMNum.Text = softreg.GetMNum();//获取机器码 string RNum = softreg.GetRNum(softreg.GetMNum());//获取生成的注册码 bool xFlag = false; RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).OpenSubKey(Encrypt.EncryptDES("jara", Const.EncryptKey), true).OpenSubKey(Encrypt.EncryptDES("jara.ini",Const.EncryptKey),true);//读取注册表中的注册码 foreach (string keyName in retkey.GetSubKeyNames()) { if (Encrypt.DecryptDES(keyName, Const.EncryptKey) == RNum)//比较两注册码是否相等 { xFlag = true; break; } } if (xFlag == true) { this.txtRNum.Text = RNum; this.txtRNum.Properties.ReadOnly = true; this.btnReg.Enabled = false; } else { this.txtRNum.Text = string.Empty; this.txtRNum.Properties.ReadOnly = false; this.btnReg.Enabled = true; } } private void SoftRegForm_Shown(object sender, EventArgs e) { this.txtRNum.Focus(); } #endregion #region * 注册 private void btnReg_Click(object sender, EventArgs e) { if (this.txtRNum.Text.ToString() == string.Empty) { ImClient.Comm.MessageBox.Info("注册码不能为空!"); return; } try { if (this.txtRNum.Text == softreg.GetRNum(this.txtMNum.Text.ToString().Trim())) { RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey(Encrypt.EncryptDES("jara",Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("jara.ini",Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES(this.txtRNum.Text.ToString().Trim(), Const.EncryptKey)); ImClient.Comm.MessageBox.Info("注册成功!重启软件后生效!"); this.Close(); } else { ImClient.Comm.MessageBox.Warn("注册码错误!"); } } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion #region * 关闭 private void btnClose_Click(object sender, EventArgs e) { this.Close(); } #endregion } }
主窗体:
public static void RegMetod() { SoftReg softreg = new SoftReg(); //判断软件是否注册 RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey(Encrypt.EncryptDES("jara", Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("jara.ini", Const.EncryptKey)); foreach (string strRNum in retkey.GetSubKeyNames()) { if (Encrypt.DecryptDES(strRNum, Const.EncryptKey) == softreg.GetRNum(softreg.GetMNum())) { Application.Run(new LoginForm("正式版",""));//启动主界面 return; } } //System.Windows.Forms.MessageBox.Show("您现在使用的是试用版,可以免费试用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); Int32 tLong; try { tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0); //System.Windows.Forms.MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { //System.Windows.Forms.MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0, RegistryValueKind.DWord); } tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0); if (tLong < 30) { int tTimes = tLong + 1; Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", tTimes); int xint = 30 - tTimes; Application.Run(new LoginForm("试用版",xint.ToString()));//启动主界面 } else { DialogResult result = System.Windows.Forms.MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { //frmRegisterForm.state = false; //btnReg_Click(sender, e); } else { Application.Exit(); } } }