C#软件注册与注册机

(一)软件的实现:

SoftReg类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;


namespace WindowsFormsApp16
{
    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>
        /// 获取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 strMNum = strNum.Substring(0, 24);    //截取前24位作为机器码
            return strMNum;
        }


        public int[] intCode = new int[127];    //存储密钥
        public char[] charCode = new char[25];  //存储ASCII码
        public int[] intNumber = new int[25];   //存储ASCII码值


        //初始化密钥
        public void SetIntCode()
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }


        ///<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;
        }
    }
}

b4e28a3da4239ca926271f93760a7978.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


 using System.Management;    //需要引用System.Management.dll
using Microsoft.Win32;


namespace WindowsFormsApp16
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }


 


        private void button1_Click(object sender, EventArgs e)
        {


        }


        private void mainForm_Load(object sender, EventArgs e)
        {
            //判断软件是否注册
            RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");
            foreach (string strRNum in retkey.GetSubKeyNames())
            {
                if (strRNum == softReg.GetRNum())
                {
                    this.labRegInfo.Text = "此软件已注册!";
                    this.btnReg.Enabled = false;
                    return;
                }
            }
            this.labRegInfo.Text = "此软件尚未注册!";
            this.btnReg.Enabled = true;
            MessageBox.Show("您现在使用的是试用版,可以免费试用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);


            Int32 tLong;    //已使用次数
            try
            {
                tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);
                MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord);
            }


            //判断是否可以继续试用
            tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);
            if (tLong < 30)
            {
                int tTimes = tLong + 1;
                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", tTimes);
            }
            else
            {
                DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    FormRegister.state = false; //设置软件状态为不可用
                    btnReg_Click(sender, e);    //打开注册窗口
                }
                else
                {
                    Application.Exit();
                }
            }
        }


        SoftReg softReg = new SoftReg();






   private void btnClose_Click(object sender, EventArgs e)
   {
       Application.Exit();
   }


   private void btnReg_Click(object sender, EventArgs e)
   {
       FormRegister frmRegister = new FormRegister();
       frmRegister.ShowDialog();
   }
    }
}

注册窗体:

881fb101f53ba7d1fa536e2af8ec4f57.png

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp16
{
    public partial class FormRegister : Form
    {
        public FormRegister()
        {
            InitializeComponent();
        }
        public static bool state = true;  //软件是否为可用状态
     SoftReg softReg = new SoftReg();


     private void btnReg_Click(object sender, EventArgs e)
     {
         try
         {
             if (txtLicence.Text == softReg.GetRNum())
             {
                 MessageBox.Show("注册成功!重启软件后生效!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txtLicence.Text);
                 retkey.SetValue("UserName", "Rsoft");
                 this.Close();
             }
             else
             {
                 MessageBox.Show("注册码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtLicence.SelectAll();
             }
         }
         catch (Exception ex)
         {
             throw new Exception(ex.Message);
         }
     }


     private void btnClose_Click(object sender, EventArgs e)
     {
         if (state == true)
         {
             this.Close();
         }
         else
         {
             Application.Exit();
         }
     }


     private void FormRegister_Load(object sender, EventArgs e)
     {
         this.txtHardware.Text = softReg.GetMNum();
     }
    }
}

(二)注册机的实现:

SoftReg类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace WindowsFormsApp17
{
    class SoftReg
    {
        public int[] intCode = new int[127];    //存储密钥
        public char[] charCode = new char[25];  //存储ASCII码
        public int[] intNumber = new int[25];   //存储ASCII码值


 
        //初始化密钥
        public void SetIntCode()
        {
            for (int i = 1; i<intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }


        }


 


        ///<summary>


        /// 生成注册码


        ///</summary>


        ///<returns></returns>


        public string GetRNum(string strMNum)


        {


            SetIntCode();


            


            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;


        }
    }
}

6aa85eeb8ed23a689d88d57430e24a7f.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp17
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }


        private void mainForm_Load(object sender, EventArgs e)
        {


        }
        SoftReg softReg = new SoftReg();
  
         private void btnCreate_Click(object sender, EventArgs e)
         {
             try
             {
                 string strHardware = this.txtHardware.Text;
                 string strLicence = softReg.GetRNum(strHardware);
                 this.txtLicence.Text = strLicence;
             }
             catch (System.Exception ex)
             {
                 MessageBox.Show("输入的机器码格式错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }  
         }
  
         private void btnExit_Click(object sender, EventArgs e)
         {
             Application.Exit();
         }
    }
}

参考链接:

https://www.cnblogs.com/hanzhaoxin/archive/2013/01/04/2844191.html

源码百度网盘下载地址:

链接:https://pan.baidu.com/s/19ln2p3XuV_pAOyjTnY5Tyw 

提取码:lmy1 

技术群: 需要进技术群学习交流的请添加小编微信,切记备注:加群,对以上内容有什么疑问也可以直接和小编直接沟通交流!     

小编微信:mm1552923   

公众号:dotNet编程大全   

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zls365365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值