C # Build software registration code
This morning, it took a morning to get a demo of machine code and registration code, which contains time information through the generated registration code, which ensures that the registration code expires after the specified time after use.
Because the math is not so write relatively simple, OK on the code to generate machine code is very simple, basically is to take the device information after encryption
<summary>
///native machine code
///</summary> public
static string Getmachinecode ()
{
// CPU Information
String cpuId = Devicehelper.getcpuid ();
Disk information
String diskId = Devicehelper.getdiskid ();
Nic information
String MacAddress = Devicehelper.getmacaddress ();
String m1 = GetMD5 (CpuId + typeof (String). ToString ());
string m2 = GetMD5 (diskId + typeof (int). ToString ());
string m3 = GetMD5 (macaddress+typeof (double). ToString ());
String code1 = Getnum (M1, 8);
String code2 = Getnum (m2, 8);
String code3 = Getnum (M3, 8);
Return code1 + Code2 + code3;
}
Generate registration Code
<summary>///Registration code generated according to machine code///</summary>///<param name= "Machinecode" &G t; machine code </param>///<param name= "Overtime" > Expiry date </param>///<returns></returns>
; public static string Createregistercode (string machinecode,datetime overtime) {int year = Int. Parse (OverTime.Year.ToString ().
Substring (2)) +33;
int month = overtime.month+21;
int day = overtime.day+54;
int section = MACHINECODE.LENGTH/4;
string reg = "";
int n = 1597; for (int i = 0; i < section; i++) {int sec = Int.
Parse (machinecode.substring (i*4,4));
int ResU = sec + N;
if (ResU >= 10000) {resu = sec-1597;
Reg + ResU;
n = n + 1597; //Insert month/day Information reg = InseRtnum (Reg, year, 0, 8, 4, 6, 7, 1, 3, 2, 5, 9);
Reg = Insertnum (reg, Month, 0, 6, 9, 7, 3, 8, 4, 1, 2, 5);
Reg = Insertnum (Reg, day, 0, 1, 2, 5, 6, 7, 3, 8, 9, 4); Return Reg.
ToString (); ///<summary>///Inserts content after the specified number///</summary>///<param name= "str" >&L
t;/param>///<param name= "num" ></param>///<param name= "index" ></param> <param name= "PMC" ></param>///<returns></returns> static string Insertnum ( String Str,int num,int index,params int[] PMC {int posi = str. IndexOf (Pmc[index].
ToString ());
if (posi <=-1) return insertnum (str, num, index + 1, PMC); Return str. Insert (posi, Num.
ToString ()); } Verify the registration code
<summary>///Check Registration code///</summary>///<param name= "Registercode" >&L t;/param>///<param name= "overtime" ></param>///<returns></returns> P
Ublic static bool Checkregister (string registercode,ref DateTime overtime) {try {
String machinecode = Getmachinecode (); Extract date int day = Int.
Parse (Extractnum (ref registercode, 0, 1, 2, 5, 6, 7, 3, 8, 9, 4)); int month = Int.
Parse (Extractnum (ref registercode, 0, 6, 9, 7, 3, 8, 4, 1, 2, 5)); int year = Int.
Parse (Extractnum (ref registercode, 0, 8, 4, 6, 7, 1, 3, 2, 5, 9));
Day-= 54;
month-= 21;
Year-= 33;
Overtime = new DateTime (year, month, day);
Check registration code int section = MACHINECODE.LENGTH/4;
int n = 1597;string reg = ""; for (int i = 0; i < section; i++) {int sec = Int.
Parse (Machinecode.substring (i * 4, 4));
int ResU = sec + N;
if (ResU >= 10000) {resu = sec-1597;
Reg + ResU;
n = n + 1597;
return Registercode = = reg;
catch {return false; }///<summary>///extract digital///</summary>///<param name= "str"
;</param>///<param name= "index" ></param>///<param name= "PMC" ></param>
<returns></returns> static string Extractnum (ref string str, int index, params int[] pmc) {int posi = str. IndexOf (Pmc[index].
ToString ());
if (posi <=-1) return Extractnum (ref str, index + 1, PMC); string resu = str.
Substring (Posi-2, 2); str = str.
Remove (Posi-2, 2);
return resu; }Invoke instance
Take machine code
string mcode = Reginfo.getmachinecode ();
Generate registration code
string regcode = Reginfo.createregistercode (Mcode, datetime.now);
DateTime time = DateTime.Now;
Verify registration code
BOOL ResU = Reginfo.checkregister (regcode+ "1", ref time);
Code download
Link: http://pan.baidu.com/s/1bpfFu3d Password: lth3