Software registration and encryption (2)

Source: Internet
Author: User
Instance 470 use the serial number of the NIC to design the software registration program Instance descriptionIn this example, the software registration code is generated using the serial number of the local Nic. Run the program to automatically obtain the serial number of the local Nic. Click Generate registration code to generate the software registration code. Enter the registration code in the following text box and click Register. Instance running result 16.8 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, the createsubkey () method, and the system. manage the getinstances () method, managementobjectcollection class, And managementobject class of the managementclass class in the namespace. Microsoft. the classes and methods in the Win32 namespace have been introduced in chapter 16th instance 469, and are not described here. the management namespace and the classes in the namespace are described in detail. (1) system. the management namespace provides access to a large number of management information and management event sets, which are consistent with Windows Management specifications (Wmi) structure is related to Setting Detection Points for systems, devices, and applications. (2) The managementclass class indicates the CIM management class. ). Syntax format: public class managementclass: managementobject (3) getinstances () method returns a set of all instances of this class. Syntax format: Public managementobjectcollection getinstances () l return value: indicates the set of managementobject objects for this type of instance. (4) The managementobjectcollection class is based on the specified set of query and retrieval management objects. This class is one of the more common entry points for retrieving and managing information. For example, it can be used to enumerate all disk drives, network adapters, processes, and more management objects in the system, or to query all active network connections and suspended services. (5) The managementobject class indicates the WMI instance. Implementation Process(1) create a new windows application and name it ex16_08. The default form is form1. (2) Add four textbox controls, three button controls, and six label controls to the form1 form. The Textbox Control is used to enter the registration code, and the button control is used to perform registration, exit, and generate registration code operations. The label control is used to display the computer name, Nic serial number, software registration code, and prompt information. (3) Main program code. The following code obtains the NIC serial number and computer name: Private void form1_load (Object sender, eventargs e) {label2.text = environment. machinename. tostring (); // obtain the computer name label4.text = getnetcardmacaddress (); // obtain the NIC information} // obtain the NIC information function Public String getnetcardmacaddress () {managementclass MC = new managementclass ("win32_networkadapterconfiguration"); managementobjectcollection MOC = MC. getinstances (); string STR = ""; foreach (managementobj ECT Mo in MoC) {If (bool) Mo ["ipenabled"] = true) STR = Mo ["macaddress"]. tostring () ;}return STR ;}the registration code is as follows: String [] strlancode = new string [12]; // Nic information storage string [] strkey = {"Q", "W", "7", "E", "D", "F", "2 ", "G", "r", "T", "Y", "8", "P", "n", "B", "V", "C ", "X", "Z", "0", "9", "I", "8", "6", "U", "O", "P ", "M", "5", "4", "3", "1", "A", "S", "H", "J", "K ", "L"}; // generate the registration code public int intrand = 0; // determine the number of random builds private void button#click (Object sender, eventargs e) {// convert Nic information to string strcode = getnetcardmacaddress (); // call the function to obtain the NIC information strcode = strcode. substring (0, 2) + strcode. substring (3, 2) + strcode. substring (6, 2) + strcode. substring (9, 2) + strcode. substring (12, 2) + strcode. substring (15, 2); string strb = strcode. substring (0, 4) + strcode. substring (4, 4) + strcode. substring (8, 4); // Nic letter Information Storage for (INT I = 0; I <strlancode. length; I ++) // saves the NIC information to the array {strlancode [I] = strb. substring (I, 1) ;}random Ra = new random (); Switch (intrand) // the order in which the registration code is randomly generated {Case 0: label5.text = strcode. substring (0, 4) + "-" + strcode. substring (4, 4) + "-" + strcode. substring (8, 4) + "-" + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring (); intrand = 1; break; Case 1: label5.text = strcode. substring (0, 4) + "-" + strcode. substring (4, 4) + "-" + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + "-" + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring (); intrand = 2; break; Case 2: label5.text = strcode. substring (0, 4) + "-" + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + "-" + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + "-" + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37 )]. Tostring () + strkey [Ra. next (0, 37)]. tostring (); intrand = 3; break; Case 3: label5.text = strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + "-" + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + "-" + strlancode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + strla Ncode [Ra. next (0, 11)] + strlancode [Ra. next (0, 11)] + "-" + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring () + strkey [Ra. next (0, 37)]. tostring (); intrand = 0; break;} The implementation code of the software registration is as follows. Private void button2_click (Object sender, eventargs e) {If (label5.text = "") {MessageBox. show ("Please generate Registration Code");} else {string strnamekey = textbox1.text. trimend () + textbox2.text. trimend () + textbox3.text. trimend () + textbox4.text. trimend (); string strnumber = label5.text. substring (0, 4) + label5.text. substring (5, 4) + label5.text. substring (10, 4) + label5.text. substring (15, 4); If (strnamekey = s Trnumber) {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 = strnamekey) {MessageBox. show ("this registration code has expired"); Return ;}// start registration information Microsoft. win32.registrykey retkey = Microsoft. win32.registry. currentuser. opensubkey ("software", Tru E ). createsubkey ("Zhy "). createsubkey ("Zhy. ini "). createsubkey (strnumber. trimend (); retkey. setvalue ("username", "Tomorrow technology"); MessageBox. show ("registration successful! "," Prompt "); application. Exit () ;}else {MessageBox. Show (" incorrect registration code ");}}} Let aloneBased on this instance, you can implement the following functions. Register and use application components. Authorize the software products sold. Instance 471 design software registration program based on the CPU serial number and disk serial number Instance descriptionThe instance automatically generates a set of irregular registration codes based on the computer's CPU number and hard disk serial number after simple calculation to register the application. Run the program, click Generate machine code to generate a 24-bit machine code, click Generate registration code, automatically convert the 24-bit registration code according to the generated machine code, enter the registration code in the text box, click Register to complete the software registration function. Result 16.9. 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, the createsubkey () method, and the system. manage the getinstances () method of the managementclass class in the namespace, The managementobjectcollection class, And the next () method of the managementobject class, char character, and random class. Classes and methods in the Microsoft. Win32 and system. Management namespaces have been introduced in chapter 16th instances 469 and 470. The following describes other knowledge used in this instance in detail. (1) A constant of the char type can be written as a character, hexadecimal conversion sequence, or Unicode representation. You can also explicitly convert the integer character code. (2) the random class represents a pseudo-random number generator, a device that can generate digital sequences that meet certain random statistical requirements. (3) The next method returns a random number within the specified range. Syntax format: Public Virtual int next (INT minvalue, int maxvalue) parameters are described as follows. L minvalue: the lower bound of the returned random number (this lower bound value is recommended for a random number ). L maxvalue: the upper bound of the returned random number (the upper bound value cannot be obtained for a random number ). Maxvalue must be greater than or equal to minvalue. L return value: A 32-bit signed integer greater than or equal to minvalue and less than maxvalue. That is, the returned value range includes minvalue but not maxvalue. If minvalue is equal to maxvalue, minvalue is returned. Implementation Process(1) create a new windows application and name it ex16_08. The default form is form1. (2) In the form1 form, add a Textbox Control to enter the registration code. Add four button controls to perform registration, exit, generate registration code, and generate machine code; add three label controls to display information such as the software registration code and machine code. (3) Main program code. The implementation code for obtaining the CPU serial number and hard disk serial number is as follows: Public String getdiskvolumeserialnumber () to obtain the volume number of the device's hard disk {managementclass MC = new managementclass ("win32_networkadapterconfiguration "); managementobject disk = new managementobject ("win32_logicaldisk.deviceid =/" D:/""); disk. get (); Return disk. getpropertyvalue ("volumeserialnumber "). tostring ();} Public String getcpu () obtain the CPU serial number {string strcpu = NULL; managementclass mycpu = new m Anagementclass ("win32_processor"); managementobjectcollection mycpuconnection = mycpu. getinstances (); foreach (managementobject myobject in mycpuconnection) {strcpu = myobject. properties ["processorid"]. value. tostring (); break;} return strcpu;} the machine code generated is as follows: Private void button#click (Object sender, eventargs e) {label2.text = getcpu () + getdiskvolumeserialnumber (); // obtain the 24-bit CPU and hard disk serial number string [] stri D = new string [24]; for (INT I = 0; I <24; I ++) // assign the character to the array {Strid [I] = label2.text. substring (I, 1) ;}label2.text = ""; random rdid = new random (); For (INT I = 0; I <24; I ++) // randomly extract 24 characters from the array to form a new character to generate the machine code {label2.text + = Strid [rdid. the registration code generated by next (0, 24)] ;}} is as follows: public int [] intcode = new int [127]; // used to save the public void setintcode () // assign a random number of less than 10 values to the array {random Ra = new random (); For (INT I = 1; I <intcode. Length; I ++) {intcode [I] = Ra. next (0, 9) ;}} public int [] intnumber = new int [25]; // The ASCII value used to store the machine code public char [] charcode = new char [25]; // store the machine code word // generate the registration code private void button2_click (Object sender, eventargs E) {If (label2.text! = "") {// Save the machine code to the array setintcode (); // initialize the 127-Bit Array for (INT I = 1; I <charcode. length; I ++) // store the machine code into the Array {charcode [I] = convert. tochar (label2.text. substring (I-1, 1);} For (Int J = 1; j <intnumber. length; j ++) // Save the ASCII value of the character into an integer Group {intnumber [J] = intcode [convert. toint32 (charcode [J])] + convert. toint32 (charcode [J]);} string strasciiname = NULL; // used to store the machine code for (Int J = 1; j <intnumber. length; j ++ ){// MessageBox. show (convert. tochar (intnumber [J]). tostring (); If (intnumber [J]> = 48 & intnumber [J] <= 57) // determines whether the ASCII value of a character is between 0 and ~ Between 9 {strasciiname + = convert. tochar (intnumber [J]). tostring ();} else if (intnumber [J]> = 65 & intnumber [J] <= 90) // determines whether the ASCII value of the character is ~ Between Z {strasciiname + = convert. tochar (intnumber [J]). tostring ();} else if (intnumber [J]> = 97 & intnumber [J] <= 122) // determines whether the ASCII value of the character is ~ Between Z {strasciiname + = convert. tochar (intnumber [J]). tostring ();} else // determines that the ASCII value of the character is not in the above range {If (intnumber [J]> 122) // determine whether the ASCII value of the character is greater than z {strasciiname + = convert. tochar (intnumber [J]-10 ). tostring ();} else {strasciiname + = convert. tochar (intnumber [J]-9 ). tostring () ;}} label3.text = strasciiname; // get the registration code} else {MessageBox. show ("select generate machine code", "registration prompt ");}} Let aloneBased on this instance, you can implement the following functions. Obtain CPU information. Register remote software products.
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.