Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Keygen for Application

0.00/5 (No votes)
5 Oct 2011 1  
Application usage topup tool

Introduction

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.

Working

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.

1.jpg

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.

1.jpg

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)));
                builder.Append(ch);
              }
              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);
     }
    
    else
     {
      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.

History

  • 5th October, 2011: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here