C software registration code implementation code

  • 2020-05-12 03:05:52
  • OfStack

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


//  Gets the volume number of the device's 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();
        }
        // To obtain CPU The 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;
        }
        // Generating machine code 
        public static string getMNum()
        {
            string strNum = getCpu() + GetDiskVolumeSerialNumber();// To obtain 24 position Cpu And the hard drive serial number 
            string strMNum = strNum.Substring(0, 24);// Before pulling from the generated string 24 Four characters for machine code 
            return strMNum;
        }
        public static int[] intCode = new int[127];// Store the key 
        public static int[] intNumber = new int[25];// Storing machine code Ascii value 
        public static char[] Charcode = new char[25];// Stores machine code words 
        public static void setIntCode()// Assign a value less than 10 The number of 
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }

Step 2. Generate the registration code from the machine code

        // Generate registration code      
       public static string getRNum()
        {
            setIntCode();// Initialize the 127 An array of 
            for (int i = 1; i < Charcode.Length; i++)// Store the machine code in an array 
            {
                Charcode[i] = Convert.ToChar(getMNum().Substring(i  �  1, 1));
            }
            for (int j = 1; j < intNumber.Length; j++)// The character of ASCII The value stored in 1 In a set of integers. 
            {
                intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
            }
            string strAsciiName =  "" ;// Used to store the registration code 
            for (int j = 1; j < intNumber.Length; j++)
            {
                if (intNumber[j] >= 48 && intNumber[j] <= 57)// Judge character ASCII Values are 0 - 9 between 
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 65 && intNumber[j] <= 90)// Judge character ASCII Values are A - Z between 
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 97 && intNumber[j] <= 122)// Judge character ASCII Values are a - z between 
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else// Judge character ASCII The value is not in the above range 
                {
                    if (intNumber[j] > 122)// Judge 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 not registered, can be customized trial

/// <summary>
        ///  Check the 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 
                {
                    if (strRNum == clsTools.getRNum())
                    {
                        thControl(true);
                        return;
                    }
                }
                thControl(false);
                Thread th2 = new Thread(new ThreadStart(thCheckRegist2));
                th2.Start();
          }
        }

        /// <summary>
        ///  Verification trial times 
        /// </summary>
        private static void thCheckRegist2()
        {
            MessageBox.Show( "You are using a trial version of the software, which you can try for free 3000000 Times!" ,  "Tip" , MessageBoxButtons.OK, MessageBoxIcon.Information);
            Int32 tLong;
            try
            {
                tLong = (Int32)Registry.GetValue( " HKEY_LOCAL_MACHINE\SOFTWARE\angel " ,  " UseTimes " , 0);
                MessageBox.Show( "Thank you for using"  + tLong +  "Time" ,  "Tip" , MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                Registry.SetValue( " HKEY_LOCAL_MACHINE\SOFTWARE\angel " ,  " UseTimes " , 0, RegistryValueKind.DWord);
                MessageBox.Show( "New users are welcome to use this software" ,  "Tip" , 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( "Trial times reached" ,  "Warning" , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }
        }


Related articles: