. Net (c #) Implementation of the simple software registration function:

Source: Internet
Author: User

The main idea is to get the machine's CPU serial number and machine code. After performing MD5 operations on the machine code, you can obtain the registration code. Write the registration code into the registry or system directory.

// 1. Check whether the registration file exists at the entrance of the Program (winform). If not

// The system prompts that the user's registration file is damaged or the user has not registered. If the registration file exists, the system reads the file content and determines whether the registration code is correct.
// The file in which the program's entry function is located must reference using System. IO; for file operations.

Using System;
Using System. Collections. Generic;
Using System. Windows. Forms;
Using System. IO;

Namespace LoginWindowTest
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
String sysFolder = System. Environment. SystemDirectory; // obtain the System installation directory, for example, c: \ windows \ system32.
Reg rg = new Reg (); // There are several functions in this class (obtain the CPU serial number and perform MD5 operations on the string)
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
If (! File. Exists (sysFolder + "http://www.cnblogs.com/chenbg2001/admin/file://sixi.ini "))
{
// If the registration file does not exist. Registration failed
// Create a registration file
File. Create (sysFolder + "http://www.cnblogs.com/chenbg2001/admin/file://sixi.ini ");

MessageBox. Show ("register! ");
Application. Run (new RegForm (); // enter the registration page.

}
Else
{// If the registered file exists, read the file content and compare it with the password
Byte [] arry = new byte [32];
String str = "";
FileInfo fi = new FileInfo (sysFolder + "http://www.cnblogs.com/chenbg2001/admin/file://sixi.ini ");
FileStream fs = fi. OpenRead ();
Int I = fs. Read (arry, 0, 32 );
Fs. Close ();
Str = System. Text. Encoding. ASCII. GetString (arry );
If (str = rg. getMd5 (rg. GetCpuID (). Trim () // if the character string in the registration file is the same as the registration code after MD5 calculation, the registration is successful.
{
MessageBox. Show ("the software has been successfully registered! ");
Application. Run (new MainForm (); // enter the main software interface
}
Else
{
MessageBox. Show ("register software! ");
Application. Run (new RegForm (); // enter the registration page.
}

}

}
}
}

// Class 2. Reg. Obtain the CPU serial number and perform MD5 encryption on the string. System. Managerment must be referenced
// You can also optimize this class and put the verification code in it. You can write a method, such as Public bool ISRight ()

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Management;

Namespace LoginWindowTest
{
Class Reg
{
// Obtain the CPU ID
Public string GetCpuID ()
{
Try
{
ManagementClass mc = new ManagementClass ("Win32_Processor ");
ManagementObjectCollection moc = mc. GetInstances ();

String strCpuID = null;
Foreach (ManagementObject mo in moc)
{
StrCpuID = mo. Properties ["ProcessorId"]. Value. ToString ();
Break;
}
Return strCpuID;
}
Catch
{
Return "";
}

} // End method

// Obtain the first hard disk number. Due to the complexity of the mobile hard disk, we do not recommend that you perform CPUID and HDID operations during encryption. If you set the mobile hard disk as the boot disk, this will cause an error that does not match the registration code and machine code after computation.
Public string GetHardDiskID ()
{
String HDid = "";
Try
{

ManagementClass cimobject1 = new ManagementClass ("Win32_DiskDrive ");
ManagementObjectCollection moc1 = cimobject1.GetInstances ();
Foreach (ManagementObject mo in moc1)
{
HDid = (string) mo. Properties ["Model"]. Value;

}
Return HDid;
}
Catch
{
Return "";
}
}

Public string getMd5 (string md5)
{
System. Security. Cryptography. MD5CryptoServiceProvider md = new System. Security. Cryptography. MD5CryptoServiceProvider ();
Byte [] value, hash;
Value = System. Text. Encoding. UTF8.GetBytes (md5 );
Hash = md. ComputeHash (value );
Md. Clear ();
String temp = "";
For (int I = 0, len = hash. Length; I <len; I ++)
{
Temp + = hash [I]. ToString ("x"). PadLeft (2, '0 ');
}
Return temp;
}
}
}

 

/// 3. The registration page can be written as follows:

Private void button3_Click (object sender, EventArgs e)
{
Reg rg = new Reg ();
String sysFolder = System. Environment. SystemDirectory;
// Register,
// 1. Modify app. config
If (textBox2.Text. Trim ()! = Rg. getMd5 (GetCpuID ()))
{
MessageBox. Show ("incorrect registration code! ");
}
Else
{
MessageBox. Show ("registration successful! ");
File. WriteAllText (mailto: sysFolder + @ "% 20sixi. ini", rg. getMd5 (rg. GetCpuID ()));

// You can enter the main interface of the software. Or restart the program.
}

}

//////////////////////////////////////// ////////////////////

/// // OVER

//////////////////////////////////////// ///////////////////

At the beginning, this is just a simple Implementation of Software registration. If you want to be more complex, you can perform some complex operations on the machine code (CPUID. You need to design a complex algorithm. You can also write the registration code into the registry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.