Click here to Skip to main content
15,392,679 members
Articles / Desktop Programming / Windows Forms
Posted 5 Oct 2011


86 bookmarked

Keygen for Application

Rate me:
Please Sign up or sign in to vote.
4.52/5 (20 votes)
5 Oct 2011CPOL2 min read
Application usage topup tool


If you want your application to allow the user to use it specific number of times, here is the tool for doing that. This tool generates the key which specifies the number of times the application can be used. The user can get more number of application usage by paying & getting topup keys. Comments are introduced at every level for better understanding of code usage.

About the Tool

The code is written in C# .NET with .NET framework 4. There are two components (both attached with the article) in the tool. One is for vendor side and the other is for customer side. The same algorithm runs on both the sides to generate key. But in customer side, in addition to generation, the matching of generated key with the user input key is done. In this tool, the default number of application usage is 999.


The tool generates a random string, which is unique for each topup, on the customer system. The random string will change on every successful topup. The topup key is generated on vendor system based on the random string generated on the customer system. This topup key can be used to get topup on customer system.

Application Usage

If the customer wants to extend his number of application usage, he/she should mail the system ID (generated random string) to the application vendor along with the payment details.


After verifying the payment details, the vendor should generate the topup key for the user using the system ID given by the customer and send it to the customer.

The customer can use the topup key for one time topup.


Using the Code

  1. Random string generation (on customer system only):
    pkey = RandomString() + RandomInt(); // generation of random string 
    private string RandomString() // gets the first part of random string
              StringBuilder builder = new StringBuilder();
              Random random = new Random();
              char ch;
              for (int i=0; i<8; i++)
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor
    				(26 * random.NextDouble() + 65)));
              return builder.ToString();
            private string RandomInt() // gets the second part of random string
                int n;
                string ret ="";
                Random r = new Random();
                n = r.Next(11111, 99999);
                ret = n.ToString();
                return ret;
  2. Topup key generation (on both systems):

    Here GetChar() & GetNum() are the functions used to code the key.

    key = MakeKey();  
    private string MakeKey() // Generating the topup key
                String pkey = keygen();
                char[] key1 = new char[19];
                key1[0] = GetChar(pkey[8]);
                key1[1] = GetNum(pkey[4]);
                key1[2] = GetNum(pkey[1]);
                key1[3] = '9';
                key1[4] = '-';
                key1[5] = GetChar(pkey[9]);
                key1[6] = '9';
                key1[7] = GetNum(pkey[2]);
                key1[8] = GetChar(pkey[10]);
                key1[9] = '-';
                key1[10] = GetChar(pkey[12]);
                key1[11] = GetNum(pkey[3]);
                key1[12] = GetNum(pkey[6]);
                key1[13] = '9';
                key1[14] = '-';
                key1[15] = GetChar(pkey[11]);
                key1[16] = GetNum(pkey[0]);
                key1[17] = GetNum(pkey[7]);
                key1[18] = GetNum(pkey[5]);
                string skey = "";
                int i;
    // 999 application runs by default 
    // To change the default value edit key[3], key[6] & key[13]
    //which denotes 3 digits of runs respectively (max : 999)
                    for (i = 0; i < 19; i++)
                        skey += key1[i];
                   return skey;
  3. Matching the topup keys and applying the change in application usage number (on customer system only):
    if (textBox1.Text == key && textBox2.Text == keygen())
       // check the source for complete code
       MessageBox.Show("Topup Successful", "Success", 
    		MessageBoxButtons.OK, MessageBoxIcon.Information);
      MessageBox.Show("Invalid Topup", "Unsuccess", 
    		MessageBoxButtons.OK, MessageBoxIcon.Warning);

On some systems, access for the files in the path "Environment.GetFolderPath(Environment.SpecialFolder.System)" are not permitted in IDE.

Install and check the application.

Points of Interest

  • One topup up key can be used only once by the customer.
  • Topup key of one customer system can't be used on other systems.
  • Keys & number of application usage are maintained even when the application is uninstalled/reinstalled.
  • Simple string manipulation involved, no external library required.


  • 5th October, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Srikanth Anandateertha
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 4 Pin
R_Mahesh12-Mar-12 3:10
MemberR_Mahesh12-Mar-12 3:10 
GeneralMy vote of 5 Pin
Warrick Procter16-Oct-11 15:29
MemberWarrick Procter16-Oct-11 15:29 
Great article, thanks.
GeneralMy vote of 5 Pin
Jhon 26-Oct-11 0:01
MemberJhon 26-Oct-11 0:01 
GeneralMy vote of 5 Pin
thund3rstruck5-Oct-11 14:07
Memberthund3rstruck5-Oct-11 14:07 
GeneralMy vote of 3 PinPopular
Mahmood Rasuli5-Oct-11 11:50
MemberMahmood Rasuli5-Oct-11 11:50 
GeneralRe: My vote of 3 Pin
Srikanth Anandateertha5-Oct-11 20:07
MemberSrikanth Anandateertha5-Oct-11 20:07 
GeneralRe: My vote of 3 PinPopular
Charles Cox6-Oct-11 18:53
MemberCharles Cox6-Oct-11 18:53 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas5-Oct-11 7:34
MemberSergio Andrés Gutiérrez Rojas5-Oct-11 7:34 
GeneralMy vote of 2 Pin
Wooters5-Oct-11 5:56
MemberWooters5-Oct-11 5:56 
GeneralRe: My vote of 2 Pin
Srikanth Anandateertha5-Oct-11 6:17
MemberSrikanth Anandateertha5-Oct-11 6:17 
GeneralRe: My vote of 2 PinPopular
Srikanth Anandateertha5-Oct-11 6:50
MemberSrikanth Anandateertha5-Oct-11 6:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.