Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Windows Forms

Keygen for Application

Rate me:
Please Sign up or sign in to vote.
4.52/5 (20 votes)
5 Oct 2011CPOL2 min read 51.1K   5.1K   86   11
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):
    C#
    pkey = RandomString() + RandomInt(); // generation of random string 
    C#
    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.

    C#
    key = MakeKey();  
    C#
    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):
    C#
    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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
R_Mahesh12-Mar-12 3:10
R_Mahesh12-Mar-12 3:10 
GeneralMy vote of 5 Pin
Warrick Procter16-Oct-11 15:29
Warrick Procter16-Oct-11 15:29 
GeneralMy vote of 5 Pin
Jhon 26-Oct-11 0:01
Jhon 26-Oct-11 0:01 
GeneralMy vote of 5 Pin
thund3rstruck5-Oct-11 14:07
thund3rstruck5-Oct-11 14:07 
GeneralMy vote of 3 Pin
Mahmood Rasuli5-Oct-11 11:50
Mahmood Rasuli5-Oct-11 11:50 
Hi Good job but the code is not optimized, for example you use many if statements in ur code. You can use one switch-case statement simply. Also it is better u use the if-else statement because the variables (like str and num) can accept only one value at the same time and if it assign 'A' it cant be 'B','C',etc.
Also u can use a property or field for Environment.GetFolderPath(Environment.SpecialFolder.System) so u can use it easier like
string SystemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); 
if (!File.Exists(SystemPath + "\\rem"))
{
     // ...
}

The rest of program is good.
GeneralRe: My vote of 3 Pin
Srikanth Anandateertha5-Oct-11 20:07
Srikanth Anandateertha5-Oct-11 20:07 
GeneralRe: My vote of 3 Pin
Charles Cox6-Oct-11 18:53
Charles Cox6-Oct-11 18:53 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas5-Oct-11 7:34
Sergio Andrés Gutiérrez Rojas5-Oct-11 7:34 
GeneralMy vote of 2 Pin
Wooters5-Oct-11 5:56
Wooters5-Oct-11 5:56 
GeneralRe: My vote of 2 Pin
Srikanth Anandateertha5-Oct-11 6:17
Srikanth Anandateertha5-Oct-11 6:17 
GeneralRe: My vote of 2 Pin
Srikanth Anandateertha5-Oct-11 6:50
Srikanth 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.