Software registration and encryption (1)

Source: Internet
Author: User
In order to make the developed software more widely used, developers hope that more users can try the software. On the other hand, they do not want users to use unauthorized software for free for a long time, this requires the design of software registration procedures. The following describes how to protect software security through several typical examples. Instance 468 registers the software using the INI File Instance descriptionIn this example, the user information of the software is registered using the INI file. Run the program, enter the logon name, logon password, and registration code, and click Register. If the registration is successful, a prompt is displayed. If the registration is successful, a prompt is displayed. Instance running result 16.6 is shown. Technical PointsTo implement this instance function, the API functions writeprivateprofilestring and getprivateprofilestring are used. The following is an introduction. (1) writeprivateprofilestring function this function implements write operations on the INI file. The function declaration is as follows. [Dllimport ("Kernel32")] Private Static extern long writeprivateprofilestring (string section, string key, string Val, string filepath); the parameters are described as follows. L section: Section in the INI file. L key: keyword in the INI file. L VAL: the value of the keyword in the INI file. L filepath: the complete path and name of the INI file. (2) getprivateprofilestring function this function implements read operations on INI files. The function declaration is as follows. [Dllimport ("Kernel32")] Private Static extern int getprivateprofilestring (string section, string key, string def, stringbuilder retval, int size, string filepath); parameter descriptions are shown in Table 16.5. Table 16.5 parameter descriptions
Parameter Value Description
Section Paragraph name in the INI File
Key Keyword in the INI File
Def Default value when cannot be read
Retval Read Value
Size Value size
Filepath Complete INI file path and name
Note: To use an API function, you must reference the system. runtime. interopservices namespace. Implementation Process(1) create a new windows application and name it ex16_07. The default form is form1. (2) In the form1 form, add three textbox controls for entering registration information, and add two button controls for registration and exit. (3) Main program code. The implementation code for registering user information is as follows: Private void form1_load (Object sender, eventargs e) {filestream c = new filestream ("C: // desck. ini ", filemode. openorcreate, fileaccess. write);} [dllimport ("Kernel32")] Private Static extern long writeprivateprofilestring (string section, string key, string Val, string filepath); [dllimport ("Kernel32")] private Static extern int getprivateprofilestring (string section, string K Ey, string def, stringbuilder retval, int size, string filepath); Private void button#click (Object sender, eventargs e) {stringbuilder temp = new stringbuilder (200 ); string filename = "C: // desck. ini "; // The complete path and name of the Ni file. Foreach (Object CT in controls) {If (Ct. getType (). tostring () = "system. windows. forms. textbox ") {textbox Tx = (textbox) CT; If (TX. TEXT = "") {MessageBox. show (TX. tag. tostring () + "cannot be blank") ;}} string section = textbox3.text; // The Section string key = textbox1.text In the INI file; // The keyword string keyValue = textbox2.text In the INI file; // The keyword int I = getprivateprofilestring (section, key, "cannot read the corresponding value! ", Temp, 200, filename); // you can check whether the value of IF (temp. tostring () =" cannot be read! ") {Writeprivateprofilestring (section, key, keyValue, filename); MessageBox. Show (" the INI file is successfully written! "," Information ") ;}else {MessageBox. Show (" this information has been registered ");}} Let aloneBased on this instance, you can implement the following functions. Encrypt the INI file to save registration information. Encrypt and save registration information for the combined INI file. Instance 469 use the registry to design software registration programs Instance descriptionMost applications write the registration information entered by the user into the registry. During the program running, the information can be read from the registry. This example mainly implements the registry operation function in the program. When you run the program, click Register to write user input information to the Registry. Instance running result 16.7 is shown. Technical PointsMicrosoft is used to implement the instance function. the currentuser attribute of the Registry class in the Win32 namespace, The opensubkey () method of the registrykey class, the getsubkeynames () method, the setvalue () method, and the createsubkey () method. The following is an introduction. (1) The Microsoft. Win32 namespace Microsoft. Win32 namespace provides two types of classes: the class for processing events caused by the operating system and the class for the operating system registry. (2) The registrykey class indicates the entry-level nodes in the Windows registry. This class is the Registry encapsulation. Syntax format: Public sealed class registrykey: registralbyrefobject. idisposable Note: To obtain a registrykey instance, you must use one of the static members of the Registry class. (3) The Registry class provides the registrykey object that represents the root item in the Windows registry, and provides the static () method of the access item/value pair. Syntax format: public static class Registry (4) currentuser attribute this attribute contains information about the current user preferences, this field reads the hkey _ CURRENT_USER registry key in the Windows registry. Syntax format: public static readonly registrykey currentuser Note: information stored in this item includes environment variable settings and data about program groups, colors, printers, network connections, and application preferences, this option makes it easier to set up the current user. In this item, the software supplier stores the current user-specific preferences to be used in its applications. (5) opensubkey () method this method retrieves the specified subitem. Syntax format: Public registrykey opensubkey (string name, bool writable) parameters are described as follows. L name: Name or path of the subitem to be opened. L writable: Set it to true if the write access permission of the item is required. L return value: the subitem of the request. If the operation fails, it is null. (6) createsubkey () method this method creates a new subitem or opens an existing subitem for write access. The string subkey is case insensitive. Syntax format: Public registrykey createsubkey (string subkey) parameters are described as follows. L subkey: Name or path of the subitem to be created or opened. L return value: registrykey object, which indicates the new subitem or empty reference. If a zero-length string is specified for the subkey, the current registrykey object is returned. (7) getsubkeynames () method this method retrieves a string array containing all subitem names. Syntax format: Public String [] getsubkeynames () l return value: A string array containing the name of the subitem of the current item. (8) setvalue () method this method sets the specified name/value pair. Syntax format: Public void setvalue (string name, object Value) parameters are described as follows. L name: name of the value to be stored. L value: the data to be stored. Note: When registry operations use the registrykey class and registry class, Microsoft. Win32 namespaces must be referenced. Implementation Process(1) create a new windows application and name it ex16_07. The default form is form1. (2) In the form1 form, add three textbox controls for entering registration information, and add two button controls for registration and exit. (3) Main program code. Private void button#click (Object sender, eventargs e) {If (textbox1.text = "") {MessageBox. show ("company name cannot be blank"); return;} If (textbox2.text = "") {MessageBox. show ("user name cannot be blank"); return;} If (textbox3.text = "") {MessageBox. show ("Registration Code cannot be blank"); return;} // instance registrykey Class Object Microsoft. win32.registrykey retkey1 = Microsoft. win32.registry. currentuser. opensubkey ("software "). opensubkey ("Zhy "). opensubkey ("Zhy. ini ", true); foreach (string strname in retkey1.getsubkeynames () // determines whether the registration code has expired {If (strname = textbox3.text) {MessageBox. show ("this registration code has expired"); Return ;}// start registration information Microsoft. win32.registrykey retkey = Microsoft. win32.registry. currentuser. opensubkey ("software", true ). createsubkey ("Zhy "). createsubkey ("Zhy. ini "). createsubkey (textbox3.text. trimend (); retkey. setvalue ("username", textbox2.text); retkey. setvalue ("capataz", textbox1.text); retkey. setvalue ("code", textbox3.text); MessageBox. show ("registered successfully, you can use this software"); application. exit ();} Let aloneBased on this instance, you can implement the following functions. The registration information is encrypted and stored in the registry. Register a program that records the number of user trials.
Related Article

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.