首页
社区
课程
招聘
[分享]Application Trial Maker By Hamed_ji
发表于: 2007-11-11 08:54 4104

[分享]Application Trial Maker By Hamed_ji

2007-11-11 08:54
4104
Introduction



It's very common to want to have trial version of your application. for example you want your application only run 30 days, or user can only run your application 200 times.

You can do this kind of tasks with this class.

And then if someone want to buy your application. he must call you and read his computer ID. then you will use Serial Maker to make Serial(Password). After entering Password application always will run as full version.

If the trial period finished. reinstalling the application won't let user to run it.

The computer id is 25 characters string that came from hardware information. ProccesorID, MainBoard Manufacturer, VGA name, HardDisk Serial number and etc. you can choose the hardware items you want to use to make Computer ID.

When you give Password to a customer, his password won't change until he changing his hardwares.


Computer ID

To make computer id you must indicate what's the name of your application. it's because if you have used Trial Maker to make trial more than one application, each application have it's own Computer ID. In source "Computer ID" = "BaseString"

Trial Maker use Management Objects to get hardware information.

Serial

With some changing in Computer ID the serial will produce. For making password you must have your own identifier. Identifier is 3 characters string. None of characters must be zero.

Files

There's two files to work with this class.

First file use for saving computer information and Days to finish trial time. This file must be in secure place. And user must not find it. (RegFile) it's not bad idea to save this file in application startup directory

Second file will use if user register the software. It will contain serial that user wrote.(HideFile) you can save Hide file in ex: c:\windows\system32\tmtst.dfg. i mean some un easy path.

Both files will encrypt with TripleDES algohrithm. key of encryption is custom. It's better you choose your own key for encryption.

How it works

Getting System Information:

SystemInfo class is for getting system information. it contains GetSystemInfo function. this function take application name and append it ProcessorID, BaseBoard product and etc.

public static string GetSystemInfo(string SoftwareName){   if(UseProcessorID == true)    SoftwareName += RunQuery("Processor", "ProcessorId");  if (UseBaseBoardProduct == true)    SoftwareName += RunQuery("BaseBoard", "Product");   if (UseBaseBoardManufacturer == true)    SoftwareName += RunQuery("BaseBoard", "Manufacturer");  // See more in source code   SoftwareName = RemoveUseLess(SoftwareName);   if (SoftwareName.Length < 25)    return GetSystemInfo(SoftwareName);  return SoftwareName.Substring(0, 25).ToUpper();}

Then remove useless characters from string any character out of A to Z and 0 to 9 is useless character. and if the string wasn't long enough call GetSystemInfoAgain to make it longer.

RunQuery function take ObjectName (TableName) and method name and return defined mehotd of first object

private static string RunQuery(string TableName, string MethodName)
{
  ManagementObjectSearcher MOS =     new ManagementObjectSearcher("Select * from Win32_" + TableName);
  foreach (ManagementObject MO in MOS.Get())
  {
    try
    {
      return MO[MethodName].ToString();
    }
    catch(Exception e)
    {
      System.Windows.Forms.MessageBox.Show(e.Message);
    }
  }
  return "";
}
when we have system information we must make Computer ID (BaseString) from it.

Making ID (BaseString)
private void MakeBaseString(){  _BaseString =     Encryption.Boring(Encryption.InverseByBase(SystemInfo.GetSystemInfo(_SoftName),10));}
To Making BaseString First we get system information then use Encryption.InverseByBase and Encryption.Boring.

InverseByBase: Encryption.InverseByBase("ABCDEF",3) will return CBAFED it's so simple it's inversing every 3 characters

Boring: move every characters in the string with the formula:
  NewPlace = (CurrentPlace * ASCII(character)) % string.length

Making Serial (Password)
We use Encryption.MakePassword. this function take BaseString and Identifier. using InverseByBase 3 times and Boring one time and then use ChangeChar function to changing characters

static public string MakePassword(string st, string Identifier)
{
  if (Identifier.Length != 3)
    throw new ArgumentException("Identifier must be 3 character length");  int[] num = new int[3];
  num[0] = Convert.ToInt32(Identifier[0].ToString(), 10);
  num[1] = Convert.ToInt32(Identifier[1].ToString(), 10);
  num[2] = Convert.ToInt32(Identifier[2].ToString(), 10);  st = Boring(st);
  st = InverseByBase(st, num[0]);
  st = InverseByBase(st, num[1]);
  st = InverseByBase(st, num[2]);

  StringBuilder SB = new StringBuilder();
  foreach (char ch in st)
  {
    SB.Append(ChangeChar(ch, num));
  }
  return SB.ToString();
}
Check Password
After Making BaseString and password check if user register software before. to do this use CheckRegister Fucntion. it will return true if registered before and false if not.

if CheckRegister return false open registration Dialog for user.

Show Dialog
Create new frmDialog and show it to user. if the dialog result were OK it means software registered successfully and if it was Retry it means its trial mode and any other DialogResult means cancel. Dialog class take BaseString, Password, days to end and Run times to end as argument.

Reading And Writing Files
There's a class named FileReadWrite. this class Read/Write file with TripleDES encryption algohrithm.

FileReadWrite.WriteFile take a 2 strings first one is file path and the second one is data to write. after writing all data it write byte 0 as finish. it will use for reading

public static void WriteFile(string FilePath, string Data)
{
  FileStream fout = new FileStream(FilePath, FileMode.OpenOrCreate,    FileAccess.Write);
  TripleDES tdes = new TripleDESCryptoServiceProvider();
  CryptoStream cs = new CryptoStream(fout, tdes.CreateEncryptor(key, iv),     CryptoStreamMode.Write);
  byte[] d = Encoding.ASCII.GetBytes(Data);
   cs.Write(d, 0, d.Length);
  cs.WriteByte(0);
   cs.Close();
  fout.Close();
}

The key for There Writing and reading is one. it's custom and you can change it.
How To Use
Where to use
The best part that you can check registration is before showing main dialog. when you have created a Windows application project first you must add SoftwareLocker.dll as reference

then in program.cs find main function. i think it's best place for checking registration. you can check registration when your main Dialog load. but before loading is better.
Change Main Dialog
it's better to add one argument to your main dialog constructor. a boolean value that indicate is this a trial running or it's full version running. and if it's trial mode disable some parts of your application

How to change main()
add namespace:

using SoftwareLocker;
Collapse
[STAThread]
static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
    TrialMaker t = new TrialMaker("TMTest1",    Application.StartupPath + "\\RegFile.reg",
    Environment.GetFolderPath(Environment.SpecialFolder.System) +      "\\TMSetp.dbf",
    "Phone: +98 21 88281536\nMobile: +98 912 2881860",
    5, 10, "745");   byte[] MyOwnKey = { 97, 250, 1, 5, 84, 21, 7, 63,
    4, 54, 87, 56, 123, 10, 3, 62,
    7, 9, 20, 36, 37, 21, 101, 57};  t.TripleDESKey = MyOwnKey;  // if you don't call this part the program will   //use default key to encryption   TrialMaker.RunTypes RT = t.ShowDialog();
  bool is_trial;
  if (RT != TrialMaker.RunTypes.Expired)
  {
    if (RT == TrialMaker.RunTypes.Full)
      is_trial = false;
    else
      is_trial = true;
        Application.Run(new Form1(is_trial));
  }
}
don't move first two lines. after them define TrialMaker class

Constructor:

SoftwareName: is your softwarename, will use to make ComputerID
RegFilePath: it's file path that if user registered the registration code will save there and in every running it will check
HidenFilePath: this file will use to save SystemInformation days to finish trial mode, how many other time user can run application, current date.
Text: It will show below the OK button on Registration Dialog. use this text for your phone number
DefaultDays: How many days user can run in trial mode
DefaultTimes: How many times user can run applicaition
Identifier: 3 Character string for making password. it's password making identifier

Optional and recomended:

after calling constructor you can change default TripleDES key. it's 24 byte key.

And then you can customize Management Objects to use for making Computer ID (BaseString):
t.UseBiosVersion = false// = true;
you can do this with any t.Use...

Don't disable all of them or none of them.enabling 3 or 4 of them is the best choice. you can leave it as it default.

Serial Maker
serial maker is another application that use encryption class. take your identifier. ID and generate password (serial)

this application don't have any special thing

Other Controls
In registration dialog i have used SerialBox control. it's only 5 textbox that you can write your serial in it

SerialBox use FilterTextBox itself

both of this controls are available to download

Notes:

-> All codes wrote in Visual Studio 2005
-> Serial Maker use Encryption class from Trial Maker as LINK file
-> Writing with one encryption key and reading with another will introduce exception
-> FilterTextBox Control is not the same that i wrote before in my articles
-> Always Secure your Identifier and don't loose them

bout Hamed_ji

I like programming because, you can create something that never exist before. and this is special part of programming.

other special thing about programming is that must of time that you make program. your program goal is out of computer area. you make math, accounting or multimedia applications for ex. this is the reason when you programming you learn somthing about other sciences and this make programming more nice.

http://www.codeproject.com/useritems/Trial_Maker.asp

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 97697
活跃值: (200729)
能力值: (RANK:10 )
在线值:
发帖
回帖
粉丝
2
Thanks.
2007-11-11 09:34
0
游客
登录 | 注册 方可回帖
返回
//