C#实现注册码的方法


本文实例讲述了C#实现注册码的方法。分享给大家供大家参考。具体如下:

开发软件时,当用到商业用途时,注册码与激活码就显得很重要了。现在的软件破解技术实在在强了,各种国内外大型软件都有注册机制,但同时也不断地被破解。下面发的只是一个常用版本,发出源码被破就更容易了,但我们学习的是技术。当然也为以后自己的软件不会被轻易破解。

第一步。根据卷标,CPU序列号,生成机器码

// 取得设备硬盘的卷标号
public static string GetDiskVolumeSerialNumber()
{
 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
 ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid="d:"");
 disk.Get();
 return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
//获得CPU的序列号
public static 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 static string getMNum()
{
 string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
 string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码
 return strMNum;
}
public static int[] intCode = new int[127];//存储密钥
public static int[] intNumber = new int[25];//存机器码的Ascii值
public static char[] Charcode = new char[25];//存储机器码字
public static void setIntCode()//给数组赋值小于10的数
{
 for (int i = 1; i < intCode.Length; i++)
 {
  intCode[i] = i % 9;
 }
}

第二步。根据机器码 生成注册码

//生成注册码  
public static string getRNum()
{
 setIntCode();//初始化127位数组
 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
 {
  Charcode[i] = Convert.ToChar(getMNum().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 = "";//用于存储注册码
 for (int j = 1; j < intNumber.Length; j++)
 {
  if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
  {
   strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  }
  else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
  {
   strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  }
  else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
  {
   strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  }
  else//判断字符ASCII值不在以上范围内
  {
   if (intNumber[j] > 122)//判断字符ASCII值是否大于z
   {
    strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
   }
   else
   {
    strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
   }
  }
 }
 return strAsciiName;
}

第三步。检查注册状况,若没有注册,可自定义试用

/// <summary>
/// 检查注册
/// </summary>
private void CheckRegist()
{
  this.btn_reg.Enabled = true;
   RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software", true).CreateSubKey("wxk").CreateSubKey("wxk.INI");
  foreach (string strRNum in retkey.GetSubKeyNames())//判断是否注册
  {
   if (strRNum == clsTools.getRNum())
   {
    thControl(true);
    return;
   }
  }
  thControl(false);
  Thread th2 = new Thread(new ThreadStart(thCheckRegist2));
  th2.Start();
 }
}
/// <summary>
/// 验证试用次数
/// </summary>
private static void thCheckRegist2()
{
 MessageBox.Show("您现在使用的是试用版,该软件可以免费试用3000000次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 Int32 tLong;
 try
 {
  tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0);
  MessageBox.Show("感谢您已使用了" + tLong + "次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch
 {
  Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0, RegistryValueKind.DWord);
  MessageBox.Show("欢迎新用户使用本软件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0);
 if (tLong < 3000000)
 {
  int Times = tLong + 1;
  Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", Times);
 }
 else
 {
  MessageBox.Show("试用次数已到", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  Application.Exit();
 }
}

希望本文所述对大家的C#程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3