[Serial] "C # communication (Serial and network) framework design and implementation"-14. Serial number design, do not repeat the implementation of a machine one yard

Source: Internet
Author: User

Directory

14th Chapter design of serial number ... 2

14.1 Design Principles ... 2

14.2 Design ideas ... 3

14.3 Code Implementation ... 4

14.4 Code Obfuscation ... 18

14.5 code hack ... 18

14.6 Summary ... 18

14th Chapter design of serial number

Serial number, as one of the licensing methods of software use, is widely used in application software. Mainly take into account these aspects: 1. The protection of intellectual property, after all, to pay for mental and manual labor. 2. The ability to increase defense in commercial competition, to prevent the theft by competitors. 3. Enhance the effectiveness of the execution of contracts and prevent the other party from undermining cooperation mechanisms for various reasons.

Based on the above aspects, it is necessary to increase the serial number function from the angle of protection and defensive thinking mode. Each author or company designs serial numbers differently, just because they are different, so that we can achieve the effect of adding that functionality.

14.1 Design Principles
    1. The serial number is as short as possible

Mainly from a cost perspective. For example, the user site needs a genuine software serial number, how do you pass the serial number information to the user? Suppose we generate a long serial number in a symmetric or asymmetric way, if we dictate to the other side, then the other party will have to use paper and pen to record, the final input to the software is not necessarily correct, if the serial number as a file through the network to the other side, then need to occupy network resources, In addition, each other's computer does not necessarily have an Internet environment. In any case, the costs involved in the generation and delivery of long serial numbers include: material costs, flow costs, labor costs, and time costs.

If a character can express the complete information required for a serial number, it is ideal. However, this is the ideal state, is impossible to achieve, at least with my current ability is unable to complete. Therefore, the full information of the serial number should be expressed at the best length.

    1. Avoid confusing words Fuzhou into a serial number sent to the user, this serial number includes: the number 0 and the letter O, the number 1 and the letter L. Let the user try again and again, such a user experience is too poor, although the mouth does not speak out, at least feel uncomfortable.
14.2 Design Ideas

The idea of design depends on the serial number to realize what kind of function and what attributes. From a functional point of view, including: 1. A serial number of a computer; 2. Although the input conditions are the same, each generated serial number is not the same; 3. The time limit for use is verified; 4. Serial number has a registration time limit, exceeding the specified use time, the serial number is invalid, avoids the short time multiple registration. From the attribute perspective, including: the same computer, the same input conditions generate a different serial number.

We take into account the above factors, the serial number length is 25 characters, serial number generation format and element information such as:

X01-X05: For the computer's signature, 5-bit string, to obtain the ID of a part of the machine, this part may be CPU, network card, hard disk and other information, the ID is MD5 encrypted after taking the first 5 characters as a signature, to achieve a machine one yard. In this way, signatures may have the same situation, but the odds are small.

X06-x13: A 8-bit string for the date that the serial number was generated, in the format: YYYYMMDD. Use in conjunction with the use time limit behind to verify the software's lifespan.

X14-X15: For the registration time limit, the 2-digit character, from the date the serial number is generated, exceeds this registration time limit, the serial number will not be able to register the operation properly.

x16-x20: To use time limits, 5-bit numeric characters, used in conjunction with the build serial number date, to verify the software's lifespan.

X21: The offset for the serial number, 1-bit characters, regardless of the scene, the offset of each generation sequence number is different.

X22-X25: To preserve data bits, temporarily do not use. Customize a serial number dictionary information, for example: _dictionary = "Jcb8ef2gh7k6mvp9qr3txwy4", remove the easily confusing characters, this can be customized. Each part of the serial number is displaced by a randomly generated offset (X21), which extracts the corresponding character as a character of the sequence number based on the input numeric information corresponding to the dictionary's subscript.

The approximate process for generating serial numbers:

    1. Randomly generates an offset number within the length range of the dictionary information.
    2. The left or right loop movement of the dictionary based on the offset number.
    3. The corresponding characters are extracted from the dictionary information according to the numeric information entered, for example: 2015 in 2, as subscript.

The reverse parsing is similar to the process, just according to the X21 character, and the dictionary character to match, the corresponding subscript as an offset, you can reverse parse out the information.

14.3 Code Implementation

1.MD5 Operation class:

public class safety{Public       static string MD5 (String str)       {              string strresult = "";              MD5 MD5 = System.Security.Cryptography.MD5.Create ();              byte[] bdata = Md5.computehash (Encoding.Unicode.GetBytes (str));              for (int i = 0; i < bdata.length; i++)              {                     strresult = strresult + bdata[i]. ToString ("X");              }              return strresult;}       }

2 . Registration Information class:

public class reginfo{public       reginfo ()       {              keysn = "";              Date=datetime.minvalue;              reglimitdays = 0;              uselimitdays = 0;              Offset = 0;       }       public string Keysn {get; set;}       Public DateTime Date {get; set;}       public int Reglimitdays {get; set;}       public int Uselimitdays {get; set;}       public int Offset {get; set;}}

3. offset operation type:

Internal enum offsettype{left       , right       }

4. serial Number Management class

public class Licensemanage {//<summary>//////serial number dictionary, remove the characters that are easily confused by numbers and letters.        The resulting 25-bit serial number is generated from this dictionary.        </summary> private static string _dictionary = "Jcb8ef2gh7k6mvp9qr3txwy4";            <summary>////can customize dictionary strings///</summary> public static string Dictionary {            get {return _dictionary;} set {if (value).                Length < 9) {throw new ArgumentOutOfRangeException ("The dictionary length set cannot be less than 9 characters");            } _dictionary = value; }}///<summary>///Generate serial number///</summary>//<param name= "key" > keywords, Generally CPU number, hard disk number, network card number, used to bind with serial number, to achieve a machine one yard </param>//<param name= "Now" > Current time </param>//<param Name= "Reglimitdays" > Registration days limit, more than this number of days, and then register, the serial number is invalid, can no longer use </param>//<param name= "Uselimitdays" > Use days limit, more than this number of days, you can set the software to stop operation and other operations </param>        <returns> returns the serial number, such as:xxxxx-xxxxx-xxxxx-xxxxx-xxxxx</returns> public static string Buildsn (Stri  Ng Key, DateTime now, int reglimitdays, int uselimitdays) {if (Reglimitdays < 0 | | reglimitdays >            9) {throw new ArgumentOutOfRangeException ("Registration limit is 0-9"); } if (Uselimitdays < 0 | | uselimitdays > 99999) {throw new Argumentoutofrangee            Xception ("Limit of 0-99999 days for use");            }/* keyword is encrypted with MD5 and takes 5 characters as the 1th set of characters */string MD5 = SAFETY.MD5 (key); String x1 = MD5. Substring (MD5.            LENGTH-5);            /* Generate random offsets */random Rand = new Random (); int offset = rand.            Next (1, dictionary.length-1); /* * The 1th character of group 5th holds the offset character, and the remaining 4 characters are randomly generated as reserved bits */String x5 = Dictionary[offset].            ToString ();     for (int i = 0; i < 4; i++)       {x5 + = Dictionary[rand. Next (0, Dictionary.length-1)].            ToString (); The 2nd and 3rd set of serial numbers are generated at the registration time (YYYYMMDD) and the registration time limit, altogether 10-bit strings */string DATESN = Getdat            ESn (now, offset);            String REGLIMITSN = GETREGLIMITSN (reglimitdays, offset);            String x2 = datesn.substring (0, 5);            String x3 = datesn.substring (datesn.length-3) + REGLIMITSN;            /* * To generate a 4th set of serial numbers using time limit, altogether 5-bit string */String x4 = GETUSELIMITSN (uselimitdays, offset);        Return String.Format ("{0}-{1}-{2}-{3}-{4}", X1, x2, X3, X4, X5); }///<summary>//Registration serial number///</summary>//<param name= "key" > keywords, typically CPU number, hard The disk number, the network card number, is used to bind with the serial number, realizes one machine one yard </param>//<param name= "SN" > Serial number </param>///<param name= "desc" & gt; Description information </param>///<returns> registration status, successful:0</returns> internal static int REGSN (string key, String sn, ref string desc) {if (String.IsNullOrEmpty (key) | |            String.IsNullOrEmpty (SN)) {throw new ArgumentNullException ("parameter is empty");            } licenseinfo reginfo = Getreginfo (SN);            string MD5 = SAFETY.MD5 (key); if (String.compareordinal (MD5. Substring (MD5.                Length-5), reginfo.keysn)! = 0) {desc = "Keyword does not match the serial number"; return-1;//keyword does not match the serial number} if (reginfo.date = = DateTime.MaxValue | | reginfo.date = = Datetime.minvalue ||                Reginfo.date > DateTime.Now.Date) {desc = "Serial number time error";            return-2;//Serial number time has error} TimeSpan ts = datetime.now.date-reginfo.date; if (TS. Totaldays > 9 | | Ts.                Totaldays < 0) {desc = "Serial number is invalid";            return-3;//Serial Number Invalid} if (reginfo.uselimitdays <= 0) {desc = "Limited use period";    return-4;//use term Limited} Application.UserAppDataRegistry.SetValue ("SN", SN);            desc = "Registered success";        return 0; }///<summary>//Detect serial number, try for clock timing call///</summary>//<param name= "key" > Keywords , generally CPU number, hard disk number, network card number, used to bind with serial number, to achieve a one yard </param>//<param name= "desc" > Description information </param>//<retur Ns> detection status, successful:0</returns> internal static int checksn (string key, ref string desc) {if (St Ring.            IsNullOrEmpty (key)) {throw new ArgumentNullException ("parameter is null");            } Object val = Application.UserAppDataRegistry.GetValue ("SN");                if (val = = null) {desc = "The serial number of the machine is not detected";            return-1; } string sn = val.            ToString ();            LicenseInfo reginfo = Getreginfo (SN);            string MD5 = SAFETY.MD5 (key); if (String.compareordinal (MD5. Substring (MD5. Length-5), reginfo.keysn)! = 0) {desc = "Keyword does not match the serial number"; The return-2;//keyword does not match the serial number} if ((Datetime.now.date-reginfo.date).                Totaldays > Reginfo.uselimitdays) {desc = "sequence use expires";            return-3;//keyword does not match serial number} desc = "Serial number available";        return 0; }///<summary>///For days remaining//</summary>//<param name= "key" > keywords, typically CPU number, Hard disk number, network card number, for binding with serial number, one yard </param>///<returns> remaining days </returns> internal static int Getremaind Ays (string key) {if (String.IsNullOrEmpty (key)) {throw new Argumentnullexcep            tion ("parameter is empty");            } Object val = Application.UserAppDataRegistry.GetValue ("SN");            if (val = = null) {return 0; } string sn = val.            ToString (); LicenseInfo reginfo = getreginfo (SN);            string MD5 = SAFETY.MD5 (key); if (String.compareordinal (MD5. Substring (MD5.            Length-5), reginfo.keysn)! = 0) {return 0;//keyword does not match the serial number and cannot be used.            }//<=0, proving that it is not available. return reginfo.uselimitdays-(int) (datetime.now.date-reginfo.date).        Totaldays; }///<summary>///According to the serial number, reverse the registration information///</summary>//<param name= "SN" > Serial number <             /param>///<returns> registration information </returns> private static LicenseInfo Getreginfo (String sn) {            LicenseInfo reg = new LicenseInfo (); string[] SPLITSN = sn.            Split ('-');            if (splitsn.length! = 5) {throw new FormatException ("The serial number is malformed and should have '-' characters '); } reg.            KEYSN = splitsn[0]; Reg.            Offset = Dictionary.indexof (splitsn[4][0]); Reg. Date = GetDate (splitsn[1] + splitsn[2]. Substring (0, 3), Reg.            Offset); Reg. Reglimitdays = Getreglimitdays (splitsn[2]. Substring (3, 2), Reg.            Offset); Reg. Uselimitdays = Getuselimitdays (Splitsn[3], Reg.            Offset);        return reg; }///<summary>///The current time and offset to generate the corresponding string for the present///</summary>//<param name= "Now" & gt; Current time </param>//<param name= "offset" > Offset </param>///<returns> returns the string corresponding to the date, 8-bit string &L            t;/returns> private static string Getdatesn (DateTime now, int offset) {string datesn = ""; String date = Now.            ToString ("YyyyMMdd");            string newdic = Dictionary; for (int i = 0; i < date. Length;                i++) {newdic = getnewdictionarystring (newdic, offset, licenseoffset.left); int num = Int. Parse (Date[i].                ToString ()); DATESN + = Newdic[num].            ToString ();        } return DATESN;  }///<summary>//reverse the registration time according to the registered time serial number//</summary>      <param name= "DATESN" > Time string </param>//<param name= "offset" > offset </param>//            /<returns> time </returns> private static DateTime GetDate (string datesn, int offset) {            String datestr = "";            string newdic = Dictionary; for (int i = 0; i < datesn.length; i++) {newdic = getnewdictionarystring (newdic, offset, Lic                Enseoffset.left);                int num = Newdic.indexof (datesn[i]);            Datestr + = num; } return new DateTime (int. Parse (datestr.substring (0, 4)), Int. Parse (Datestr.substring (4, 2)), Int.        Parse (Datestr.substring (6, 2)); }///<summary>//to generate the corresponding string with the registration time limit and offset///</summary>//<param name= "Reglimi Tdays "></param>//<param name=" offset "></param>//<returns> returns the corresponding registration time limit string, 2-bit String </returns> private static string GetreglimITSN (int reglimitdays, int offset) {string reglimitsn = "";            String reglimitstr = reglimitdays.tostring ("00");            string newdic = Dictionary; for (int i = 0; i < reglimitstr.length; i++) {newdic = getnewdictionarystring (newdic, offset                , Licenseoffset.left); int num = Int. Parse (Reglimitstr[i].                ToString ()); REGLIMITSN + = Newdic[num].            ToString ();        } return REGLIMITSN; }///<summary>///The registration time limit is based on the registration time limit///</summary>//<param name= "Reglimi TSn "> Registration time limit string </param>//<param name=" offset "> offset </param>//<returns> registration time limit &L t;/returns> private static int getreglimitdays (string reglimitsn, int offset) {string Reglimi            Tstr = "";            string newdic = Dictionary;             for (int i = 0; i < reglimitsn.length; i++) {   Newdic = getnewdictionarystring (newdic, offset, licenseoffset.left);                int num = Newdic.indexof (reglimitsn[i]);            Reglimitstr + = num; } return int.        Parse (REGLIMITSTR); }///<summary>//to generate the corresponding string using time limit and offset///</summary>//<param name= "Uselimi Tdays "> Use time limit </param>//<param name=" offset "> Offset </param>///<returns> Use time limit correspondence word String, 5-bit strings </returns> private static string getuselimitsn (int uselimitdays, int offset) {Stri            ng USELIMITSN = "";            String uselimitstr = uselimitdays.tostring ("00000");            string newdic = Dictionary; for (int i = 0; i < uselimitstr.length; i++) {newdic = getnewdictionarystring (newdic, offset                , Licenseoffset.left); int num = Int. Parse (Uselimitstr[i].                ToString ()); USELIMITSN + = Newdic[num].            ToString ();  }          return USELIMITSN; }///<summary>///Use time limit based on time limit string///</summary>//<param name= "Reglimi TSn "> Use time limit string </param>//<param name=" offset "> offset </param>//<returns> use time limit &L t;/returns> private static int getuselimitdays (string uselimitsn, int offset) {string Uselimi            Tstr = "";            string newdic = Dictionary;  for (int i = 0; i < uselimitsn.length; i++) {newdic = getnewdictionarystring (newdic, offset,                Licenseoffset.left);                int num = Newdic.indexof (uselimitsn[i]);            Uselimitstr + = num; } return int.        Parse (USELIMITSTR); }///<summary>///Create a new dictionary based on dictionary, offset, and offset type///</summary>//<param name= "dic" ;</param>//<param name= "offset" ></param>///<param name= "Offsettype" ></param>//<returns></returns> private static string Getnewdictionarystring (string dic, int off            Set, Licenseoffset offsettype) {StringBuilder sb = new StringBuilder (DIC);                    if (Offsettype = = Licenseoffset.left) {for (int i = 0; i < offset; i++) { String head = Sb[0].                    ToString (); Sb.                    Remove (0, 1); Sb.                Append (head); }} else if (Offsettype = = Licenseoffset.right) {for (int i = 0; i < o Ffset; i++) {string end = Sb[dic. LENGTH-1].                    ToString (); Sb. Remove (DIC.                    Length-1, 1); Sb.                Insert (0, end); }} return sb.        ToString (); }    }

14.4 Code Obfuscation

From a security standpoint. NET program if not confused, it is easy to decompile the source code. From a professional point of view, even if you add the serial number function, it does not help, professional personnel can be cracked off minutes, although this is very few people, but there is this possibility. If a software person wants to know a good software, the first reflection may be anti-compilation.

Adding confusion is necessary for companies or business-used software, although open source is now popular.

14.5 code hack

No matter. NET program how to confuse, theoretically can be cracked, the theory of things will not repeat. There are usually two ways to hack in contact: keygen mode and brute force mode.

The method of registering the machine, the process and mechanism of verifying serial number of the software need to calculate the generating algorithm of serial number in reverse, and develop a small software based on the algorithm of inverse push, which is used to generate the serial number from the author authorization. This way does not break the code of the program itself, relatively moderate. The brute force approach is to find the code in the serial Number verification section and not allow the code to execute effectively by removing or bypassing the validation code. This approach changes the code of the program itself, so there are some risks.

14.6 Summary

There are several ways to implement the serial number, which is not necessarily the best, but hopefully it will help developers.

The final implementation is as follows:

Only laugh at

Email:[email protected]

qq:504547114

. NET Development Technology Alliance: 54256083

Document Download: Http://pan.baidu.com/s/1pJ7lZWf

Official website: http://www.bmpj.net

[Serial] "C # communication (Serial and network) framework design and implementation"-14. Serial number design, do not repeat the implementation of a machine one yard

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.