權限管理系統之軟件註冊模塊

轉自:http://www.cnblogs.com/jara/post

軟件註冊是商業軟件必不可少的模塊,在整個程序中啓到舉足輕重的做用。this

系統啓動時首先會檢測程序是否註冊,若是程序沒有註冊能夠使用30次,試用次數完成後將彈出註冊界面。程序每次登錄都會將程序使用次數寫入到註冊表中,程序註冊是也會將註冊碼已加密的方式寫入到註冊表中。加密

複製代碼
   public static void RegMetod(bool isDownLoad) { try { SoftReg softReg = new SoftReg(); //判斷軟件是否註冊 RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey(Encrypt.EncryptDES("mySoftWare", Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("Register.INI", Const.EncryptKey)); foreach (string strRNum in retkey.GetSubKeyNames()) { if (Encrypt.DecryptDES(strRNum, Const.EncryptKey) == softReg.GetRNum()) { run(isDownLoad, "正式版", ""); return; } } Int32 tLong; try { tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\" + Encrypt.EncryptDES("mySoftWare", Const.EncryptKey), "UseTimes", 0); } catch { Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\" + Encrypt.EncryptDES("mySoftWare", Const.EncryptKey), "UseTimes", 0, RegistryValueKind.DWord); } tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\" + Encrypt.EncryptDES("mySoftWare", Const.EncryptKey), "UseTimes", 0); if (tLong < 30) { int tTimes = tLong + 1; Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\" + Encrypt.EncryptDES("mySoftWare", Const.EncryptKey), "UseTimes", tTimes); int xint = 30 - tTimes; //試用版不提供更新服務 run(true, "試用版", xint.ToString()); } else { DialogResult result = Comm.MessageBox.YesNo("試用次數已到!您是否須要註冊?"); if (result == DialogResult.Yes) { RegisterForm registerForm = new RegisterForm(); registerForm.ShowDialog(); } else { Application.Exit(); } } } catch (Exception e) { Log.Error("程序註冊出錯!"); throw new Exception("程序註冊出錯:" + e.Message); } }
複製代碼

註冊界面:spa

複製代碼
        #region * 註冊
        private void btnReg_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtLicence.Text)) { Client.Win.Comm.MessageBox.Info("註冊碼不能爲空!"); return; } try { if (txtLicence.Text == softReg.GetRNum()) { Client.Win.Comm.MessageBox.Info("註冊成功!重啓軟件後生效!"); RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey(Encrypt.EncryptDES("mySoftWare", Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("Register.INI", Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES(txtLicence.Text, Const.EncryptKey)); retkey.SetValue(Encrypt.EncryptDES("UserName",Const.EncryptKey),Encrypt.EncryptDES( "Rsoft",Const.EncryptKey)); this.Close(); } else { Client.Win.Comm.MessageBox.Warn("註冊碼錯誤!"); txtLicence.SelectAll(); } } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion
複製代碼

生成機器碼(由MAC地址和硬盤券盤號截取前面20位):操作系統

複製代碼
     ///<summary> /// 生成機器碼 ///</summary> ///<returns></returns> public string GetMNum() { string strNum = Computer.Instance().MacAddress + Computer.Instance().VolumeSerialNumber; // GetCpu() + GetDiskVolumeSerialNumber(); string strMNum = string.Empty; if (strNum.Length > 20) { strMNum = strNum.Substring(0, 20); //截取前20位做爲機器碼  } else { strMNum = strNum; } return strMNum; }
複製代碼

生成註冊碼:code

複製代碼
        ///<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 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-九、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; }
複製代碼

獲取電腦的基本信息:orm

複製代碼
 /// <summary> /// Computer Information /// </summary> public class Computer { public string CpuID; public string MacAddress; public string DiskID; public string IpAddress; public string LoginUserName; public string ComputerName; public string SystemType; public string TotalPhysicalMemory; //單位:M public string VolumeSerialNumber; private static Computer _instance; public static Computer Instance() { if (_instance == null) _instance = new Computer(); return _instance; } protected Computer() { VolumeSerialNumber = GetDiskVolumeSerialNumber(); CpuID = GetCpuID(); MacAddress = GetMacAddress(); DiskID = GetDiskID(); IpAddress = GetIPAddress(); LoginUserName = GetUserName(); SystemType = GetSystemType(); TotalPhysicalMemory = GetTotalPhysicalMemory(); ComputerName = GetComputerName(); } ///<summary> /// 獲取硬盤卷標號 ///</summary> ///<returns></returns> public string GetDiskVolumeSerialNumber() { try { ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } catch { return "unknow"; } finally { } } /// <summary> /// CPU序列號 /// </summary> /// <returns></returns> string GetCpuID() { //try //{ // //獲取CPU序列號代碼 // string cpuInfo = "";//cpu序列號 // ManagementClass mc = new ManagementClass("Win32_Processor"); // ManagementObjectCollection moc = mc.GetInstances(); // foreach (ManagementObject mo in moc) // { // cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); // } // moc = null; // mc = null; // return cpuInfo; //} //catch //{ // return "unknow"; //} //finally //{ //} return ""; } /// <summary> /// MAC地址 /// </summary> /// <returns></returns> string GetMacAddress() { try { //獲取網卡Mac地址 string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { mac = mo["MacAddress"].ToString(); break; } } moc = null; mc = null; return mac.Replace(":", ""); } catch { return "unknow"; } finally { } } /// <summary> /// IP地址 /// </summary> /// <returns></returns> string GetIPAddress() { try { //獲取IP地址 string st = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { //st=mo["IpAddress"].ToString();  System.Array ar; ar = (System.Array)(mo.Properties["IpAddress"].Value); st = ar.GetValue(0).ToString(); break; } } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } /// <summary> /// 硬盤序列號 /// </summary> /// <returns></returns> string GetDiskID() { try { //獲取硬盤ID String HDid = ""; ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { HDid = (string)mo.Properties["Model"].Value; } moc = null; mc = null; return HDid; } catch { return "unknow"; } finally { } } /// <summary> /// 操做系統的登陸用戶名 /// </summary> /// <returns></returns> string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["UserName"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } /// <summary> /// PC類型 /// </summary> /// <returns></returns> string GetSystemType() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["SystemType"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } /// <summary> /// 物理內存 /// </summary> /// <returns></returns> string GetTotalPhysicalMemory() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["TotalPhysicalMemory"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } /// <summary> /// 計算機名稱 /// </summary> /// <returns></returns> string GetComputerName() { try { return System.Environment.GetEnvironmentVariable("ComputerName"); } catch { return "unknow"; } finally { } }
複製代碼

完成以上便可實現軟件註冊!blog

相關文章
相關標籤/搜索