License控制解决方案

当我们写完一个软件以后一般都会牵扯到软件控制,那么控制版本的原理是什么呢?其实就是在程序中添加了一段经过自己编写算法(这个算法可以是简单的公式运算,也可以是复杂的结合硬件的绑定方式),将形成的序列号注册到我们的注册表中.每当程序运行的时候都去读取注册表信息并验证一把.

下面是一个简单的License生成器代码:

    public partial class LicenseCreator : Form
    {
        public LicenseCreator()
        {
            InitializeComponent();
        }

        private void btnLincenseCreator_Click(object sender, EventArgs e)
        {
            string licenseKey = GetLicenseKey(txtUsername.Text);
            txtLincense.Text = licenseKey;
        }
        //通过自定义算法返回序列号
        private string GetLicenseKey(string username)
        {
            int breakCount = 8;
            //定制算法可根据个人修改
            username =
                System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(
                Math.Abs(~System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(username, "md5").GetHashCode() ^ 0x2010 << 3)
                .ToString(), "sha1");            

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < username.Length; i++)
            {
                result.Append(username.Substring(i, 1));
                if ((i + 1) % breakCount == 0)
                {
                    result.Append("-");
                }
            }

            return result.ToString().TrimEnd('-');
        }
        //注册信息
        private void btnRegedit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text) || string.IsNullOrEmpty(txtLincense.Text))
            {
                MessageBox.Show("请生成Lincese后重新注册");
                return;
            }
                string regeditKey = @"Licenses\DFF31A6A-A5C2-48A7-B4D9-3C1151676FC8\";
            RegistryKey classesRootKey = Registry.ClassesRoot;
            RegistryKey licenseKey = classesRootKey.CreateSubKey(regeditKey);

            licenseKey.SetValue("UserName", txtUsername.Text);
            licenseKey.SetValue("Key", txtLincense.Text);

            MessageBox.Show("注册成功!");

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtUsername.Text = string.Empty;
            txtLincense.Text = string.Empty;
            txtUsername.Focus();
        }
LicenseCreator

 

  

posted on 2018-02-06 11:03  平大爷  阅读(1043)  评论(0编辑  收藏  举报

导航