65.9K
CodeProject 正在变化。 阅读更多。
Home

应用程序密钥生成器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.52/5 (19投票s)

2011年10月5日

CPOL

2分钟阅读

viewsIcon

53844

downloadIcon

5151

应用程序使用量充值工具

引言

如果您希望您的应用程序允许用户使用特定次数,这里有一个工具可以做到这一点。该工具生成一个密钥,该密钥指定应用程序的使用次数。用户可以通过支付费用并获取充值密钥来获得更多的应用程序使用次数。代码使用方面已在每个级别添加注释,以便更好地理解。

关于该工具

该代码是用 C# .NET 和 .NET framework 4 编写的。该工具包含两个组件(都附加在文章中),一个是用于供应商端,另一个是用于客户端。相同的算法在两端运行以生成密钥。但在客户端,除了生成之外,还进行生成的密钥与用户输入密钥的匹配。在此工具中,默认的应用程序使用次数为 999。

工作

该工具在客户系统上生成一个随机字符串,该字符串对于每次充值都是唯一的。该随机字符串在每次成功充值时都会更改。充值密钥在供应商系统上基于客户系统上生成的随机字符串生成。该充值密钥可用于在客户系统上进行充值。

应用程序用法

如果客户想延长其应用程序的使用次数,他/她应将系统 ID(生成的随机字符串)以及付款详细信息通过电子邮件发送给应用程序供应商。

1.jpg

在验证付款详细信息后,供应商应使用客户提供的系统 ID 为用户生成充值密钥,并将其发送给客户。

客户可以一次性使用充值密钥进行充值。

1.jpg

Using the Code

  1. 随机字符串生成(仅在客户系统上)
    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. 充值密钥生成(在两个系统上)

    这里 GetChar()GetNum() 是用于编码密钥的函数。

    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. 匹配充值密钥并应用应用程序使用次数的变化(仅在客户系统上)
    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);
     } 

在某些系统上,IDE 中不允许访问路径 "Environment.GetFolderPath(Environment.SpecialFolder.System)" 中的文件。

安装并检查应用程序。

关注点

  • 一个充值密钥只能被客户使用一次。
  • 一个客户系统的充值密钥不能在其他系统上使用。
  • 密钥和应用程序使用次数即使在应用程序卸载/重新安装后也会保留。
  • 涉及简单的字符串操作,不需要外部库。

历史

  • 2011年10月5日:初始发布
© . All rights reserved.