The principle is that the software and the registration machine use the same algorithm to match the data. After registration, the data is written to the registry. A simple way to register a machine.
1, the realization of software
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Management;
Using System.Text;
Using System.Threading.Tasks; Namespace Softwareregister {class Softreg {///<summary>///get Hard drive serial number///</summ ary>///<returns></returns> public string Getdiskserialnum () {Managem
Entclass Mydisk = new ManagementClass ("Win32_NetworkAdapterConfiguration");
ManagementObject disk = new ManagementObject ("win32_logicaldisk.deviceid=\" C:\ "); Disk.
Get (); return disk. GetPropertyValue ("VolumeSerialNumber").
ToString (); ///<summary>///Get CPU serial number///</summary>///<returns></returns>
;
public string Getcpuserialnum () {String cpustr = "";
ManagementClass mycpu = new ManagementClass ("Win32_Processor"); Managementobjectcollection mycpucollention = Mycpu.
GetInstances (); foreach (managementobject var in mycpucollention) {cpustr = var. properties["Processorid"].
Value.tostring ();
return cpustr; ///<summary>///The first 24 digits of the CPU serial number and HDD serial number to do the machine code///</summary>///<returns> </returns> public String Getmachinenum () {String Num = Getcpuserialnum () + Getdiskseria
Lnum ();
String machinenum = Num.substring (0,24);
return machinenum; } public int[] M_intcode = new int[127]; Storage key public char[] M_charascii = new CHAR[25]; Storage ASCII public int[] M_intascii = new INT[25]; Store ASCII value///<summary>///initialization key (generated by modulo 9)///</summary> public void Intiin Tcode () {for (int i = 0; i < m_intcode.length; i++) {M_intcode[i] = I
% 9;
} ///<summary>///Get device registration code///</summary>///<returns></retur
Ns> public String Getregisternum () {intiintcode ();
String machinenume = Getmachinenum (); Get ASCII code for (int i = 1; i < m_charascii.length i++) {M_charascii[i] by machine code
Convert.tochar (Machinenume.substring (i-1, 1));
///Through a simple algorithm, change the value of ASCII, the value of ASCII, plus the value of the initialization key for (int j = 1; j < M_intascii.length; J + +)
{M_intascii[j] = Convert.ToInt32 (m_charascii[j]) + m_intcode[convert.toint32 (m_charascii[j))];
} String machineascii = ""; for (int k = 1; k < m_intascii.length; k++) {if (m_intascii[k) >=-&& M_inta Scii[k] <= 57) | | (M_intascii[k] >= && m_intascii[k] <= 90) | | (M_intascii[k] >= && M_intasciI[k] <= 122)) {Machineascii + = Convert.tochar (M_intascii[k]). ToString ();
Between 0-9,a-z,a-z} else if (M_intascii[k] > 122) { Machineascii + + Convert.tochar (M_intascii[k]-10). ToString (); Greater than Z} else {machineascii + = Convert.tochar (m_intasc II[K]-9).
ToString ();
} return machineascii; }
}
}
2, check whether registration is available
Using Microsoft.Win32;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Softwareregister {public class Checkreg {Softreg Softreg = new Softreg ();
<summary>///Check has been registered///</summary>///<returns></returns>
public bool Getisreg () {bool Ischeck = false; RegistryKey RegKey = Registry.CurrentUser.OpenSubKey ("SOFTWARE", true). CreateSubKey ("MySoftware").
CreateSubKey ("Register.ini");
foreach (var item in Regkey.getsubkeynames ()) {if (item = = Softreg.getregisternum ())
{Ischeck = true;
} return Ischeck; ///<summary>///To determine whether the software is available, has 20 trial periods, can also be replaced by days, and then written to the registry information///</summary>///& Lt;returns></returns> public bool Getuseinfo (ref int m_intuse) {m_intuse = 0;
BOOL Is_canuse = false; try {m_intuse = (int) registry.getvalue ("Hkey_local_machine\\software\\mysoftware", "Usetimes"
, 0); catch (Exception) {registry.setvalue ("Hkey_local_machine\\software\\mysoftware")
, "Usetimes", 0, Registryvaluekind.dword);
} m_intuse = (int) registry.getvalue ("Hkey_local_machine\\software\\mysoftware", "Usetimes", 0);
if (M_intuse < 5) {int int_count = m_intuse + 1;
Registry.setvalue ("Hkey_local_machine\\software\\mysoftware", "Usetimes", Int_count);
Is_canuse = true;
else {is_canuse = false;
return is_canuse;
}
}
}
</pre><p></p><pre>
Main form::
Using Microsoft.Win32;
Using Softwareregister;
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms; namespace Registermachine {public partial class Form1:form {public Form1 () {Initi
Alizecomponent ();
Inti ();
} softreg Softreg = new Softreg ();
Checkreg Checkreg = new Checkreg ();
private void Inti () {txtb_machinenum.text=softreg.getmachinenum (); if (Checkreg.getisreg ()) Label1.
Text = "Software has been registered"; Else Label1.
Text = "Software not registered";
int count = 0;
if (Checkreg.getuseinfo (ref count)) {int k = 5-count; MessageBox.Show ("not registered.") The trial count is left with "+ K". ToString () + "times.
"," Information ", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void Button1_Click (object sender, EventArgs e) {
try {if (Txtb_regnum.text = = Softreg.getregisternum ()) { MessageBox.Show ("registered successfully.") When the software is restarted, it takes effect.
"," Information ", MessageBoxButtons.OK, MessageBoxIcon.Information); RegistryKey Retkey = Registry.CurrentUser.OpenSubKey ("Software", true). CreateSubKey ("MySoftware"). CreateSubKey ("Register.ini").
CreateSubKey (Txtb_regnum.text); Retkey.
SetValue ("UserName", "Rsoft"); This.
Close (); } else {MessageBox.Show ("Registration code error.)
"," warns ", MessageBoxButtons.OK, messageboxicon.warning);
Txtb_regnum.selectall (); } catch (Exception ex) {throw new Exception (ex.
message); }} private void But_cancel_click (ObjeCT sender, EventArgs e) {this.
Close ();
}
}
}
Second: Registration machine
In the same way as above, the data is matched by the same algorithm.
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms; namespace Register {public partial class Form1:form {public Form1 () {Initializeco
Mponent (); } public int[] M_intcode = new int[127]; Storage key public char[] M_charascii = new CHAR[25]; Storage ASCII public int[] M_intascii = new INT[25]; Store ASCII value///<summary>///initialization key (generated by modulo 9)///</summary> public void intii Ntcode () {for (int i = 0; i < m_intcode.length; i++) {M_intcode[i] =
I% 9;
} public string Getregisternum (string machinenume) {Intiintcode ();
if (machinenume.length!=) return "machine code is wrong, please re-enter!"; Get ASCII via machine codecode for (int i = 1; i < m_charascii.length i++) {M_charascii[i] = Convert.tocha
R (Machinenume.substring (i-1, 1));
///Through a simple algorithm, change the value of ASCII, the value of ASCII, plus the value of the initialization key for (int j = 1; j < M_intascii.length; J + +)
{M_intascii[j] = Convert.ToInt32 (m_charascii[j]) + m_intcode[convert.toint32 (m_charascii[j))];
} String machineascii = ""; for (int k = 1; k < m_intascii.length; k++) {if (m_intascii[k) >=-&& M_inta Scii[k] <= 57) | | (M_intascii[k] >= && m_intascii[k] <= 90) | |
(M_intascii[k] >= && m_intascii[k] <= 122)) {Machineascii + = Convert.tochar (M_intascii[k]). ToString ();
Between 0-9,a-z,a-z} else if (M_intascii[k] > 122) { Machineascii + + Convert.tochar (m_intascii[k)-10). ToString (); Greater than Z} else {machineascii + = Convert.tochar (m_intasc II[K]-9).
ToString ();
} return machineascii;
} private void But_reg_click (object sender, EventArgs e) {TextBox2.Text = "";
TextBox2.Text = Getregisternum (TextBox1.Text); } private void Btn_cancel_click (object sender, EventArgs e) {this.
Close ();
}
}
}
3, return to the main interface for registration