Generate code similar to activation code (C #)

Source: Internet
Author: User

Because the project has a similar activation code of Dongdong, and then wrote a. Yesterday to ask questions, then combined with their own thoughts, finally succeeded. Just optimization is not very ideal, and sometimes die cycle, also did not go to debug to see which values of the problem.

I don't know what the algorithm is for Windows VS and SQL What, I'm getting the sum of each set of values equals constant.


Partial code (all code):


public const int Mapkeylen = 16;
public const int Registkeylen = 25; public static string[] Mapketitem =new string[36]{"U", "F", "2", "I", "X", "S", "A", "V", "P", "M", "C", "9", "Q", "1", "

G "," 7 "," K "," T "," B "," 4 "," W "," 6 "," H "," N "," E "," 8 "," Z "," O "," L "," 3 "," D "," 0 "," Y "," 5 "," J "," R "};
        public class Transtool {private static int[] Mapareas = new int[] {48, 60, 74, 90};
        private static int[] Registareas = new int[] {48,64,88,100,116}; Public Transtool () {}///<summary>///get key///</summary>///<param Nam E= "KeyType" >1 is 4 sets of 4-bit code, 2 is 5 Group 5-bit code.
        Can pass through the above value definition </param>///<returns></returns> public static string getkey (int keyType) {string Mapkey = string.
            Empty;
            string Mapkeya = "0000";
            string mapkeyb = "0000";
            string MAPKEYC = "0000";
            string mapkeyd = "0000";
      String mapkeysplit = "-";      Mapkeya = Getkeys (0, KeyType);
            Mapkeyb = Getkeys (1, KeyType);
            MAPKEYC = Getkeys (2, KeyType);
            Mapkeyd = Getkeys (3, KeyType);
            Mapkey = Mapkeya + mapkeysplit + mapkeyb + mapkeysplit + mapkeyc + mapkeysplit + mapkeyd;
                if (KeyType = = 2) {string mapkeye = "0000";
                Mapkeye = Getkeys (4, KeyType);
            Mapkey + + mapkeysplit + mapkeye;
        return mapkey; private static string Getkeys (int index, int keytype=1) {string item = string. empty;//the value of the current group int[] areas = Mapareas;
                Total field int Keyitemslen = staticstrings.mapkeylen/4;//per group length if (KeyType = 2) {
                areas = Registareas;
            Keyitemslen = STATICSTRINGS.REGISTKEYLEN/5; } if (index >= 0 && Index < keyitemslen) {int preitem = 0;//agoA value int total = 0;//currently totals for (int i = 0; i < Keyitemslen; i++) {
                    int curr = GetRight (Areas[index], Preitem, I, Keyitemslen, total);
                    Total + = Curr; if (curr >= staticStrings.MAPKETITEM.Length | | Curr <= 0 | |
                    (Total >= Areas[index] && i < keyItemsLen-2))
                    {item = Getkeys (index, KeyType);
                        else {preitem = Curr;
                    Item + + Staticstrings.mapketitem[curr];
            }} else {item = ' 0000 ';
        return item; }///<summary>//////</summary>///<param name= "area" > Sum value </para m>///<param name= "Preitem" > Previous value </param>///<param name= "No" > Current to ordinal number </param>///<param name= "Total" > Current totals </param>///<re
        turns></returns> private static int getright (int area, int preitem, int no,int keyitemslen, int total) {string keys = string.
            Empty;
            int item = 0;
            int len = staticStrings.MAPKETITEM.Length;
            if (no = = keyItemsLen-1)///When the last number is fetched, the direct sum value minus the current sum {item = Area-total;
                else {Random Random = new Random (); if (len-1 > Area-total)///When the last number has not been obtained and the total number of remaining available for the total is less than the range of values, go to random remaining available {item = Ran Dom.
                Next (1, area-total); else {item = random.
            Next (1, len-1);//random out a constant within the number of} System.Threading.Thread.Sleep (50); }//Extreme case Handling//A!=AREA-KEYITEMSLEn+1, because the first value is too large so that the last three values are 1, but in this case there is no//b!=area-total-keyitemslen+1, because the first two values are too large to make the last two values are 1, in which case the second number is again C!=area-total-c, although the last two values are not 1, it may also make the last two equal, and when you know that the value will be equal to the last value, discard this value and then take a value if (item = = Preitem | | (no = 0 && item = area-keyitemslen + no + 1) | | (no = 1 && Item = = Area-total-keyitemslen + no+1) | | (No==3&&item==area-total-item))
            Item<=0 when all is regenerated, so it is not judged {item = GetRight (area, Preitem, no, Keyitemslen, total);
        return item; }
    }


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.