Crossing the Blues

Introduction

RegistrationDialog.jpg

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

You can do this kind of tasks with this library.

If someone try to buy your application must call to 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.

Collapse
Collapse
"MsoNormal" lang="cs" style="MARGIN: 0cm 0cm 0pt; DIRECTION: ltr; unicode-bidi: embed; TEXT-ALIGN:removed">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

Collapse
Collapse
"cs">

Collapse
"cs">private static string RunQuery(string TableName, string MethodName)

{

ManagementObjectSearcher MOS =








Collapse
    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)
Collapse
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

Collapse
Collapse
"cs">static public string MakePassword(string st, string Identifier)

{

if (Identifier.Length != 3)

throw new ArgumentException("Identifier must be 3 character length");








Collapse
  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);








Collapse
  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

Collapse
Collapse
"cs">

Collapse
"cs">public static void WriteFile(string FilePath, string Data)

{

FileStream fout = new FileStream(FilePath, FileMode.OpenOrCreate,








Collapse
    FileAccess.Write);

TripleDES tdes = new TripleDESCryptoServiceProvider();

CryptoStream cs = new CryptoStream(fout, tdes.CreateEncryptor(key, iv),








Collapse
     CryptoStreamMode.Write);

byte[] d = Encoding.ASCII.GetBytes(Data);









Collapse
  cs.Write(d, 0, d.Length);

cs.WriteByte(0);









Collapse
  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:

Collapse
using SoftwareLocker;
Collapse
Collapse
"cs">[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);









Collapse
  TrialMaker t = new TrialMaker("TMTest1",








Collapse
    Application.StartupPath + "\\RegFile.reg",

Environment.GetFolderPath(Environment.SpecialFolder.System) +








Collapse
      "\\TMSetp.dbf",

"Phone: +98 21 88281536\nMobile: +98 912 2881860",

5, 10, "745");








Collapse
 








Collapse
  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};








Collapse
  t.TripleDESKey = MyOwnKey;








Collapse
  // if you don't call this part the program will








Collapse
   //use default key to encryption








Collapse
 








Collapse
  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;









Collapse
    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):
Collapse
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)

Doesn't contains anything special.

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

Source:http://www.codeproject.com

Hi vọng bài viết có ích cho bạn!

Mọi thắc mắc xin liên hệ:

Yahoo:ghost0bk

Mail: ghost0bk@gmail.com

Phone: 093.43.63.833

Chân thành cám ơn!Upanh.com

Download Mediafire-Hotfile-Rapidshare-Megaupload-Mega 1280-Megashare..