首页 > 编程语言 >C# 软件注册和注册机

C# 软件注册和注册机

时间:2023-02-12 08:44:53浏览次数:46  
标签:注册机 C# System intNumber int using 软件 public string

(一)软件的实现:

SoftReg类:

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Management;    //需要引用System.Management.dll
   
  namespace SoftRegister
  {
      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;
           }
       }
  }

主窗体:

 

 

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;
   using Microsoft.Win32;
    
   namespace SoftRegister
   {
       public partial class FormMain : Form
       {
           public FormMain()
           {
               InitializeComponent();
           }
    
           SoftReg softReg = new SoftReg();    
           private void FormMain_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();
                   }
               }    
           }
    
           private void btnClose_Click(object sender, EventArgs e)
           {
               Application.Exit();
           }
    
           private void btnReg_Click(object sender, EventArgs e)
           {
               FormRegister frmRegister = new FormRegister();
               frmRegister.ShowDialog();
           }
       }
   }

注册窗体:

 

 

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;
   using Microsoft.Win32;
    
   namespace SoftRegister
   {
       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.Management;
    
   namespace SoftwarePassport
   {
       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;
           }
       }
   }

主窗体:

 

 

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;
    
   namespace SoftwarePassport
   {
       public partial class FormMain : Form
       {
           public FormMain()
           {
               InitializeComponent();
           }
    
           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();
           }
       }
   }

 

标签:注册机,C#,System,intNumber,int,using,软件,public,string
From: https://www.cnblogs.com/funiyi816/p/17113255.html

相关文章

  • Why is address space allocation granularity 64KB?
    Whyisaddressspaceallocationgranularity64KB?RaymondChen October8th,20030 0YoumayhavewonderedwhyVirtualAllocallocatesmemoryat64K......
  • C语言:任意10个浮点数从小到大排序
    //冒泡排序:将任意10个浮点数从小到大排序#include<stdio.h>main(){floata[10],t;inti,j,k,b;for(i=0;i<=9;i++)scanf("%f",&a[i]);for(......
  • C语言:指针 运行结果
    #include<stdio.h>//程序运行结果:【1】int*f(int*x,int*y){if(*x<*y)returnx;elsereturny;}main(){inta=7,b=8,*p=&a,*q=&b,*r;r=f......
  • ChatGPT生成式AI技术分析
    ChatGPT生成式AI技术分析ChatGPT算力:研究框架(2023)ChatGPT的“背后英雄”:芯片,国内GPU、CPU、FPGA、AI芯片及光模块产业链蓄势待发。1)GPU:支撑强大算力需求。由于具备并行......
  • CF 800D Fake Plastic Tree
    CF800DFakePlasticTree首先这道题给了一棵有根树,然后对于每个节点给了区间[l,r],要求每次选取一个节点v,那么从1-v的路径上的点需要加一个c,这个c按照depth应该是非严格......
  • How To Be Successful Sam Altman
    HowToBeSuccessfulI’veobservedthousandsoffoundersandthoughtalotaboutwhatittakestomakeahugeamountofmoneyortocreatesomethingimportant......
  • 从空项目开始建立MFC程序导致1189错误解决办法
    错误提示:严重性代码说明项目文件行源禁止显示状态工具错误(活动) E0035 #error指令:BuildingMFCapplicationwith/MD[d](CRTdllversion)requiresMFC......
  • Scrapy 框架的 pipelines 参数详解
    目录pipelines的使用一个例子pipelines常用的函数pipelines的使用在pipelines.py中创建pipelines的类。(也可用默认存在的类)classPipelinesTest:defp......
  • Spring Boot+Mybatis-1:集成 Mybatis 并实现 CRUD(1)
    读前需知:此系列属于个人学习过程中的学习记录,不保证是最优方案,请读者大佬们带点批判进行阅读。如有技术上的错误望不吝赐教~1.前言MyBatis是一款持久层框架,它支持自定义......
  • AtCoder Beginner Contest 289
    A.flipC++Code#include<bits/stdc++.h>usingi64=longlong;intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::str......