权限管理系统之软件注册模块_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 权限管理系统之软件注册模块

权限管理系统之软件注册模块

 2014/9/3 11:42:41  罗宇浩  程序员俱乐部  我要评论(0)
  • 摘要:转自:http://www.cnblogs.com/jara/软件注册是商业软件必不可少的模块,在整个程序中启到举足轻重的作用。系统启动时首先会检测程序是否注册,如果程序没有注册可以使用30次,试用次数完成后将弹出注册界面。程序每次登陆都会将程序使用次数写入到注册表中,程序注册是也会将注册码已加密的方式写入到注册表中。publicstaticvoidRegMetod(boolisDownLoad){try{SoftRegsoftReg=newSoftReg()
  • 标签:软件

转自:http://www.cnblogs.com/jara/

软件注册是商业软件必不可少的模块,在整个程序中启到举足轻重的作用。

系统启动时首先会检测程序是否注册,如果程序没有注册可以使用30次,试用次数完成后将弹出注册界面。程序每次登陆都会将程序使用次数写入到注册表中,程序注册是也会将注册码已加密的方式写入到注册表中。

class="cnblogs_code_copy">复制代码
   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);
            }
        }
复制代码

注册界面:

复制代码
        #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;
        }
复制代码

生成注册码:

复制代码
        ///<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-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;
        }
复制代码

获取电脑的基本信息:

复制代码
 /// <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
            {
            }
        }
复制代码

完成以上即可实现软件注册!

发表评论
用户名: 匿名