码迷,mamicode.com
首页 > 其他好文 > 详细

注册机的使用

时间:2015-07-04 18:13:43      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:

目的:对于一些软件,如果不加注册码的话,所有人都可以从网上下载使用,而且没有使用限制。为了阻止这种情况,就必须加一个注册码,注册码是根据每台电脑的机器码生成的,每台电脑的机器码都不一样,所以每一台电脑装这个软件都需要注册使用。

一、造一个软件,然后给软件加上机器码,此时显示此软件尚未注册

技术分享

技术分享

二、一旦点击次数超过5次,便会提示:“访问次数一到,是否需要注册”

技术分享

 

下面开始获取本台电脑的机器码:

①、新建了一个SoftReg类,用来获取本台电脑的配置
namespace QQ邮件发送
{
    class SoftReg 
    {
        //获取硬盘卷标号
        public string GetDiskVolumeSerialNumber()
        {
            ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        // 获取CPU序列号
        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;
        }

        // 生成机器码
        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;
            }
        }

        // 生成注册码
        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;
        }

    }
}
二、给软件加上注册码
namespace QQ邮件发送
{
    public partial class Form1 : Form
    {
        public Thread thread;//定义一个线程
        public Form1()
        {
            InitializeComponent();
            
            this.im = Image.FromFile("1.jpg");
            this.BackgroundImage = im;
        }
        string file;
        //string aa;
        ArrayList Lujing=new ArrayList();
     
        private void button6_Click(object sender, EventArgs e) //添加附件
        {
            openFileDialog1.Filter = "Word文件.doc|*.doc|表格文件.xls|*.xls|所有文件|*.*"; //筛选文件类型
            DialogResult dk = openFileDialog1.ShowDialog();  //创建一个枚举类型的变量dk来接收打开这个对话框
            if (dk == DialogResult.OK) //如果点的是确定,才会执行下面的代码
            {
             
                string[] files = openFileDialog1.FileNames;//将获取到的所有路径都存到一个string类型的数组里面
                foreach (string file in files)//遍历所有路径,一条条的加到Lujing这个ArrayList里面去,然后用的时候再从集合里面一条条遍历出来
                {
                    Lujing.Add(file);
                    txtfujianname.AppendText(file + "\r\n");//AppendText追加文本
                }
                MessageBox.Show("添加附件成功");
                //获取附件的名字添加到文本框当中
            }


        }
     
        //下面开始写SendEmail函数
        MailMessage msg;
      
        public void SendEmail(string Emailshoujian, string Emailbiaoti, string Emailzhengwen, MailAddress EmailFrom)
        {
            try
            {
                //创建发送邮箱,并获取发件人地址,收件人地址,以及邮件标题与正文
                msg = new MailMessage();  //创建一个MailMessage的类,用来发送邮件
                msg.To.Add(Emailshoujian); //将收件人的邮箱地址添加进来  
                msg.Subject = Emailbiaoti; //获取一下发送邮件的标题
                msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题改成国际编码方式
                msg.From = EmailFrom;//获取一下发件人的邮箱地址
                msg.Body = Emailzhengwen;//邮件的正文内容
                msg.BodyEncoding = System.Text.Encoding.UTF8; //将邮件的正文内容改一下编码方式
                msg.IsBodyHtml = false; //确认正文内容是不是以网页格式发送的
                msg.Priority = MailPriority.High;//邮件发送的优先等级为最高

              
                //添加附件
                if(txtfujianname.Text!="")
                {
                        foreach (string path in Lujing) //路径都存到Lujing里面去了,全部遍历出来
                        {
                            Attachment data = new Attachment(path); //Attachment:附件
                            msg.Attachments.Add(data);
                        }       
                }

                // //设置用于验证发件人身份的凭据
                SmtpClient client = new SmtpClient(); //允许应用程序使用简单邮件传输协议 (SMTP) 来发送电子邮件。 
                client.Host = "smtp.qq.com"; //设置一下应用程序的服务器名称
                client.Credentials = new System.Net.NetworkCredential("527384244@qq.com", "lk19920619"); //输入发件人邮箱的用户名密码来发送邮件
                //注意!!必须在发送的时候将发件人的邮箱账户POP3/IMAP协议开启,然后输入的密码是QQ邮箱独立密码,而不是QQ密码!!!!
                client.Send(msg);//发送
                //mail from address must be same as authorization user 若出现这个错误,证明没有将发件人邮箱的POP3/IMAP协议打开,并且密码是QQ邮箱独立密码,而不是QQ密码
                //打开方式在最有一张截图
                MessageBox.Show("发送成功");
            }
            catch (Exception ex  )
            {

                throw ex;
            }
           
        }

        //当点击发送按钮的时候启动新的线程,从而不会因为卡住而影响到别的按钮的功能
        private void button1_Click(object sender, EventArgs e) //发送按钮
        {
            //thread = new Thread(new ThreadStart(fasong)); //为button1按钮造一个新的线程,这样点button1的话,就会运行新造的这个线程,而不会影响到窗体的主线程,button1执行的发送,点了button1如果卡住,但不会影响到别的按钮的作用
            //thread.IsBackground = true;//确认新造的线程后台运行
            //thread.Start();
            fasong();
        }
        public void fasong()
        {
            // MailAddress 表示电子邮件发件人或收件人的地址。
            MailAddress EmailFrom = new MailAddress("527384244@qq.com");  //发件人邮箱地址 //创建一个MailAddress的类来写发件人的地址
            string Emailshoujian = txtshoujian.Text;  //收件人邮箱地址
            string Emailbiaoti = txtbiaoti.Text; //邮件标题
            string Emailzhengwen = textBox1.Text; //邮件内容
            SendEmail(Emailshoujian, Emailbiaoti, Emailzhengwen, EmailFrom);  //调用发送邮件函数
        }
        private bool isok;
        private int dianxiaqux;
        private int dianxiaquy;
        private int chushix;
        private int chushiy;
        private int movex;
        private int movey;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isok = true;
            dianxiaqux = Cursor.Position.X;
            dianxiaquy = Cursor.Position.Y;
            chushix = this.Location.X;
            chushiy = this.Location.Y;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isok)
            {
                movex = Cursor.Position.X;
                movey = Cursor.Position.Y;
                this.Location = new Point(chushix + movex - dianxiaqux, chushiy + movey - dianxiaquy);
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isok = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
           
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        Image im;
        private int i = 1;
        private void button8_Click(object sender, EventArgs e)
        {
            if (i == 0)
            {
                this.im = Image.FromFile("5.jpg");
                this.BackgroundImage = im;
            }
            else if(i == 1)
            {
                this.im = Image.FromFile("2.jpg");
                this.BackgroundImage = im;
            }
             else if(i == 2)
            {
                this.im = Image.FromFile("3.jpg");
                this.BackgroundImage = im;
               
            }
            else if (i == 3)
            {
                this.im = Image.FromFile("4.jpg");
                this.BackgroundImage = im;
            }
            else if (i == 4)
            {
                this.im = Image.FromFile("5.jpg");
                this.BackgroundImage = im;
            }
            else 
            {
                this.im = Image.FromFile("1.jpg");
                this.BackgroundImage = im;
                i = 0;
             }
           
              i++;
        }

        private void button9_Click(object sender, EventArgs e)
        {
           
        }
      
        //下面的这些是给软件加注册码
        SoftReg softReg = new SoftReg();
        private void Form1_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.label1.Text = "此软件已注册!";
                    this.btnReg.Enabled = false;
                    return;
                }
            }
            this.label1.Text = "此软件尚未注册!";
            this.btnReg.Enabled = true;
            MessageBox.Show("您现在使用的是试用版,可以免费试用5次!", "信息", 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 (Exception)
            {
                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 < 5)
            {
                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();
                }
            }
        }

        private void btnReg_Click(object sender, EventArgs e)//注册按钮
        {
            FormRegister frmRegister = new FormRegister();
            frmRegister.ShowDialog();
            
        }

        //这是为了打开添加附件的窗口,从而获得选择的附件的路径
     
    }
}

当点击注册的时候,会获取到本机的机器码,然后根据机器码,打开注册机,输入注册码,点击"注册本软件",会自动生成注册码来注册软件

技术分享

↓ 打开注册机

技术分享

↓输入机器码,然后点击注册本软件,会自动生成注册码

技术分享

技术分享

 

↓获取到了注册码,然后回到注册页面,输入注册码

技术分享

↓点击注册

技术分享

↓注册成功,可以长久使用

技术分享

 

 

下面是关于注册机的代码。(通用的)

①Class类里面的
namespace 注册机
{
    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;
            }
        }

        /// 生成注册码
        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;
        }
    }
}

②Form1.cs 里面的
namespace 注册机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        SoftReg softReg = new SoftReg();
        private void button1_Click(object sender, EventArgs e) //注册本软件
        {
            try
            {
                string strHardware = this.txtHardware.Text; //获取到机器码
                string strLicence = softReg.GetRNum(strHardware);//根据机器码生成注册码
                this.txtLicence.Text = strLicence;//将注册码显示出来
            }
            catch (System.Exception)
            {
                MessageBox.Show("输入的机器码格式错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        //下面是拖动窗体的代码
        private bool isok;
        private int dianxiaqux;
        private int dianxiaquy;
        private int chushix;
        private int chushiy;
        private int movex;
        private int movey;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isok = true;
            dianxiaqux = Cursor.Position.X;
            dianxiaquy = Cursor.Position.Y;
            chushix = this.Location.X;
            chushiy = this.Location.Y;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isok)
            {
                movex = Cursor.Position.X;
                movey = Cursor.Position.Y;
                this.Location = new Point(chushix + movex - dianxiaqux, chushiy + movey - dianxiaquy);
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isok = false;
        }

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

       
    }
}

 

注册机的使用

标签:

原文地址:http://www.cnblogs.com/lk-kk/p/4620879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!