C# 实现软件授权码的功能

您所在的位置:网站首页 ilo授权码 C# 实现软件授权码的功能

C# 实现软件授权码的功能

2023-09-08 13:15| 来源: 网络整理| 查看: 265

对于一个商业软件来说,授权码这个功能必不可少。我这里采用CPU序列号加硬盘标识来判断是否授权。完整代码如下:

using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Management; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace KeyManager { public partial class FrmShouQuan : Form { public FrmShouQuan() { InitializeComponent(); } /// /// 获取CPU的参数 /// /// public string getCpu() { string strCpu = null; ManagementClass myCpu = new ManagementClass("win32_Processor"); ManagementObjectCollection myCpuConnection = myCpu.GetInstances(); foreach (ManagementObject myObject in myCpuConnection) { strCpu = myObject.Properties["Processorid"].Value.ToString(); break; } return strCpu; } /// /// 获取硬盘的参数 /// /// 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参数加上硬盘参数 /// /// /// private void button1_Click(object sender, EventArgs e) { txtMachineInfo.Text = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号 } public int[] intCode = new int[127];//用于存密钥 public void setIntCode() { for (int i = 1; i < intCode.Length; i++) { intCode[i] = i % 10; } } public int[] intNumber = new int[25];//用于存机器码的Ascii值 public char[] Charcode = new char[25];//存储机器码字 //生成注册码 private void button2_Click(object sender, EventArgs e) { if (txtMachineInfo.Text != "") { txtRegistText.Text = GetResistText(txtMachineInfo.Text); } else { MessageBox.Show("请选生成机器码", "注册提示"); } } /// /// 根据机器码获取注册码 /// /// /// private string GetResistText(string machineText) { //把机器码存入数组中 setIntCode();//初始化127位数组 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中 { Charcode[i] = Convert.ToChar(machineText.Substring(i - 1, 1)); } for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。 { intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]); } string strAsciiName = null;//用于存储机器码 for (int j = 1; j < intNumber.Length; j++) { if (intNumber[j] >= 48 && intNumber[j] = 65 && intNumber[j] = 97 && intNumber[j] 122)//判断字符ASCII值是否大于z { strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); } else { strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString(); } } } return strAsciiName; } private void btnRegist_Click(object sender, EventArgs e) { if (txtRegistText.Text != "") { if (textBox1.Text.TrimEnd().Equals(txtRegistText.Text.TrimEnd())) { //这里需要将机器码和注册码保存到数据库或注册表中,以便以后校验(推荐保存到数据库中,这样不怕重装系统) MessageBox.Show("注册成功"); } else { MessageBox.Show("注册码输入错误"); } } else { MessageBox.Show("请生成注册码", "注册提示"); } } /// /// 验证是否已经激活软件 /// /// /// private void button4_Click(object sender, EventArgs e) { //这里重新生成机器码和注册码,与数据库中进行对比。只要有一个参数不一致,就是未激活(此处省略两万行代码) } } }

设计文件的代码:

namespace KeyManager { partial class FrmShouQuan { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.txtRegistText = new System.Windows.Forms.TextBox(); this.txtMachineInfo = new System.Windows.Forms.TextBox(); this.button4 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(238, 126); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(278, 25); this.textBox1.TabIndex = 1; // // button1 // this.button1.Location = new System.Drawing.Point(72, 272); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(114, 32); this.button1.TabIndex = 2; this.button1.Text = "获取硬件信息"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(228, 272); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(98, 32); this.button2.TabIndex = 2; this.button2.Text = "生成注册码"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(368, 272); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 32); this.button3.TabIndex = 2; this.button3.Text = "注册"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.btnRegist_Click); // // txtRegistText // this.txtRegistText.Location = new System.Drawing.Point(238, 89); this.txtRegistText.Name = "txtRegistText"; this.txtRegistText.Size = new System.Drawing.Size(278, 25); this.txtRegistText.TabIndex = 1; // // txtMachineInfo // this.txtMachineInfo.Location = new System.Drawing.Point(238, 52); this.txtMachineInfo.Name = "txtMachineInfo"; this.txtMachineInfo.Size = new System.Drawing.Size(278, 25); this.txtMachineInfo.TabIndex = 1; // // button4 // this.button4.Location = new System.Drawing.Point(485, 272); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(118, 32); this.button4.TabIndex = 2; this.button4.Text = "检查是否注册"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(159, 52); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(67, 15); this.label1.TabIndex = 3; this.label1.Text = "机器码:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(159, 90); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(67, 15); this.label2.TabIndex = 3; this.label2.Text = "授权码:"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(114, 128); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(112, 15); this.label3.TabIndex = 3; this.label3.Text = "请输入授权码:"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(238, 163); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(278, 25); this.textBox2.TabIndex = 1; this.textBox2.Visible = false; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(99, 166); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(127, 15); this.label4.TabIndex = 3; this.label4.Text = "请输入起始日期:"; this.label4.Visible = false; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(238, 200); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(278, 25); this.textBox3.TabIndex = 1; this.textBox3.Visible = false; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(99, 203); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(127, 15); this.label5.TabIndex = 3; this.label5.Text = "请输入有效天数:"; this.label5.Visible = false; // // FrmShouQuan // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(688, 339); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.txtMachineInfo); this.Controls.Add(this.txtRegistText); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "FrmShouQuan"; this.Text = "FrmShouQuan"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.TextBox txtRegistText; private System.Windows.Forms.TextBox txtMachineInfo; private System.Windows.Forms.Button button4; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label5; } }

运行效果:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3