Method of Realizing Registration Code by C

  • 2021-07-24 11:39:29
  • OfStack

In this paper, an example is given to describe the method of implementing registration code in C #. Share it for your reference. The details are as follows:

When developing software, when it is used for commercial purposes, registration codes and activation codes are very important. Nowadays, the software cracking technology is really strong. All kinds of large-scale software at home and abroad have registration mechanisms, but at the same time, they are constantly cracked. The following is only a commonly used version, send out the source code is more easily broken, but we are learning technology. Of course, my own software will not be easily cracked in the future.

Step 1. Generate machine code according to volume label and CPU serial number


//  Get the volume label of the device hard disk 
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();
}
// Obtain CPU Serial number of 
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;
}
// Generate machine code 
public static string getMNum()
{
 string strNum = getCpu() + GetDiskVolumeSerialNumber();// Obtain 24 Bit Cpu And hard disk serial number 
 string strMNum = strNum.Substring(0, 24);// From the generated string 24 Characters as machine code 
 return strMNum;
}
public static int[] intCode = new int[127];// Storage key 
public static int[] intNumber = new int[25];// Machine code storing Ascii Value 
public static char[] Charcode = new char[25];// Store machine code words 
public static void setIntCode()// Assign an array a value less than 10 Number of 
{
 for (int i = 1; i < intCode.Length; i++)
 {
  intCode[i] = i % 9;
 }
}

Step 2. Generate a registration code according to the machine code


// Generate registration code   
public static string getRNum()
{
 setIntCode();// Initialization 127 Bit group 
 for (int i = 1; i < Charcode.Length; i++)// Store machine code in an array 
 {
  Charcode[i] = Convert.ToChar(getMNum().Substring(i - 1, 1));
 }
 for (int j = 1; j < intNumber.Length; j++)// Put the character's ASCII Value deposit 1 In a group of integers. 
 {
  intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
 }
 string strAsciiName = "";// Used to store registration codes 
 for (int j = 1; j < intNumber.Length; j++)
 {
  if (intNumber[j] >= 48 && intNumber[j] <= 57)// Judgment character ASCII Value whether 0 - 9 Between 
  {
   strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  }
  else if (intNumber[j] >= 65 && intNumber[j] <= 90)// Judgment character ASCII Value whether A - Z Between 
  {
   strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  }
  else if (intNumber[j] >= 97 && intNumber[j] <= 122)// Judgment character ASCII Value whether a - z Between 
  {
   strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  }
  else// Judgment character ASCII Value is not in the above range 
  {
   if (intNumber[j] > 122)// Judgment character ASCII Is the value greater than z
   {
    strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
   }
   else
   {
    strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
   }
  }
 }
 return strAsciiName;
}

Step 3. Check the registration status. If there is no registration, you can customize the trial


/// <summary>
///  Check registration 
/// </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())// Determine whether to register or not 
  {
   if (strRNum == clsTools.getRNum())
   {
    thControl(true);
    return;
   }
  }
  thControl(false);
  Thread th2 = new Thread(new ThreadStart(thCheckRegist2));
  th2.Start();
 }
}
/// <summary>
///  Number of verification trials 
/// </summary>
private static void thCheckRegist2()
{
 MessageBox.Show(" You are now using a trial version, which can be tried for free 3000000 Times! ", " Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Information);
 Int32 tLong;
 try
 {
  tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0);
  MessageBox.Show(" Thank you for using the " + tLong + " Times ", " Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch
 {
  Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\angel", "UseTimes", 0, RegistryValueKind.DWord);
  MessageBox.Show(" Welcome new users to use this software ", " Prompt ", 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(" The number of trials has reached ", " Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  Application.Exit();
 }
}

I hope this article is helpful to everyone's C # programming.


Related articles: