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.
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
- Random string generation (on customer system only):
pkey = RandomString() + RandomInt();
private string RandomString() {
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() {
int n;
string ret ="";
Random r = new Random();
n = r.Next(11111, 99999);
ret = n.ToString();
return ret;
}
- Topup key generation (on both systems):
Here GetChar()
& GetNum()
are the functions used to code the key.
key = MakeKey();
private string MakeKey() {
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;
for (i = 0; i < 19; i++)
skey += key1[i];
return skey;
}
- Matching the topup keys and applying the change in application usage number (on customer system only):
if (textBox1.Text == key && textBox2.Text == keygen())
{
..........
..........
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