软件注册
以下简单介绍一下,软件的注册的实现过程。
SoftReg类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management; namespace CommonClass { public 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> /// 获取网卡硬件地址 /// </summary> /// <returns></returns> public static string GetMacAddress() { string mac = string.Empty; using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration")) { ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { mac = mo["MacAddress"].ToString(); break; } } } return mac.Replace(":", ""); } /// <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 strNum = GetMacAddress() + GetDiskVolumeSerialNumber(); string strMNum = strNum.Substring(0, 20); //截取前20位作为机器码 return strMNum; } public int[] intCode = new int[127]; //存储密钥 public char[] charCode = new char[21]; //存储ASCII码 public int[] intNumber = new int[21]; //存储ASCII码值 //初始化密钥 public void SetIntCode() { for (int i = 1; i < intCode.Length; i++) { intCode[i] = i % 9; } } /// <summary> /// 生成注册码 /// </summary> /// <returns></returns> public string GetRNum(string xStr) { SetIntCode(); string strMNum = xStr; 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 ImClient.Comm.UI; using ImClient.Comm; using CommonClass; using Microsoft.Win32; using ImCommon.Util; using ImCommon.FrameWork; namespace ImClient.UI { public partial class SoftRegForm : BaseForm { public SoftRegForm() { InitializeComponent(); } SoftReg softreg = new SoftReg(); #region * FormLoad private void SoftRegForm_Load(object sender, EventArgs e) { this.txtMNum.Text = softreg.GetMNum();//获取机器码 string RNum = softreg.GetRNum(softreg.GetMNum());//获取生成的注册码 bool xFlag = false; RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).OpenSubKey(Encrypt.EncryptDES("jara", Const.EncryptKey), true).OpenSubKey(Encrypt.EncryptDES("jara.ini",Const.EncryptKey),true);//读取注册表中的注册码 foreach (string keyName in retkey.GetSubKeyNames()) { if (Encrypt.DecryptDES(keyName, Const.EncryptKey) == RNum)//比较两注册码是否相等 { xFlag = true; break; } } if (xFlag == true) { this.txtRNum.Text = RNum; this.txtRNum.Properties.ReadOnly = true; this.btnReg.Enabled = false; } else { this.txtRNum.Text = string.Empty; this.txtRNum.Properties.ReadOnly = false; this.btnReg.Enabled = true; } } private void SoftRegForm_Shown(object sender, EventArgs e) { this.txtRNum.Focus(); } #endregion #region * 注册 private void btnReg_Click(object sender, EventArgs e) { if (this.txtRNum.Text.ToString() == string.Empty) { ImClient.Comm.MessageBox.Info("注册码不能为空!"); return; } try { if (this.txtRNum.Text == softreg.GetRNum(this.txtMNum.Text.ToString().Trim())) { RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey(Encrypt.EncryptDES("jara",Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("jara.ini",Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES(this.txtRNum.Text.ToString().Trim(), Const.EncryptKey)); ImClient.Comm.MessageBox.Info("注册成功!重启软件后生效!"); this.Close(); } else { ImClient.Comm.MessageBox.Warn("注册码错误!"); } } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion #region * 关闭 private void btnClose_Click(object sender, EventArgs e) { this.Close(); } #endregion } }
主窗体:
public static void RegMetod() { SoftReg softreg = new SoftReg(); //判断软件是否注册 RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey(Encrypt.EncryptDES("jara", Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("jara.ini", Const.EncryptKey)); foreach (string strRNum in retkey.GetSubKeyNames()) { if (Encrypt.DecryptDES(strRNum, Const.EncryptKey) == softreg.GetRNum(softreg.GetMNum())) { Application.Run(new LoginForm("正式版",""));//启动主界面 return; } } //System.Windows.Forms.MessageBox.Show("您现在使用的是试用版,可以免费试用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); Int32 tLong; try { tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0); //System.Windows.Forms.MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { //System.Windows.Forms.MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0, RegistryValueKind.DWord); } tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0); if (tLong < 30) { int tTimes = tLong + 1; Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", tTimes); int xint = 30 - tTimes; Application.Run(new LoginForm("试用版",xint.ToString()));//启动主界面 } else { DialogResult result = System.Windows.Forms.MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { //frmRegisterForm.state = false; //btnReg_Click(sender, e); } else { Application.Exit(); } } }
个人主要研究:金融系统、MIS系统、人力资源管理系统、数据采集系统、权限管理系统等等系统。主攻C#开发语言,Oracle、Sql Server,WCF和Remoting通信。
如需联系可加QQ:442389681 Email:lxc880615@163.com 手机:18922735098
QQ群交流:186841119 (请注明来自博客园)
博客园地址:http://www.cnblogs.com/jara/ http://www.cnblogs.com/luoyuhao/
提示:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。
如果对文章有任何问题,都可以在评论中留言,我会尽可能的答复您,谢谢您的阅读
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!