Practice ^ operator and practice Operator

Source: Internet
Author: User

Practice ^ operator and practice Operator

[Instance description]

In this example, the key point is to use the exercise ^, which has no technical content. Haha ..


Before introducing an instance, let's take a short look at the concept of encryption.
Encryption refers to modifying the content of existing information through a special method, so that unauthorized users can obtain encrypted information without correct decryption methods, therefore, the information cannot be obtained.

When talking about encryption, some readers will be very interested and think of complicated encryption algorithms, in this example, only the unique or "^" operator is used to encrypt a number.

[Key Technologies]
In this example, the "XOR" operator is used to perform "XOR" operations on numbers to encrypt numbers in a simple way. The following describes how to encrypt a number in detail:

This article mainly uses encrypted numbers. If it is text, you can convert it to byte, and then encrypt it and then convert it back to implement encryption, after decryption, convert it to byte and computation, and then convert it back.

Many simple registration machines use a combination of PCId and hard disk ID to encrypt the registration code.


The XOR operator "^" is used to compare the corresponding bits of two binary numbers. During the bitwise "XOR" operation, if the corresponding bits of both binary numbers are 1 or the corresponding bits of both binary numbers are 0, 0 is returned; if one of the two bits is 1 and the other is 0, 1 is returned.

Now let's take a look at the execution process of encryption or decryption using "exclusive or". The value 23 is converted to binary 10111, and the value 15 of the encrypted number is converted to 1111.
Compare the values of two binary numbers by bit from the right to the left. If the corresponding bits of the two binary numbers are both 1 or the corresponding bits of the two binary numbers are both 0, 0 is returned ,; if one of the two bits is 1 and the other is 0, 1 is returned.
Finally, the encrypted result 24 and the number 15 are used to perform the "XOR" operation, convert 24 to 11000 in binary format, convert 15 to 1111 in binary format, and perform the "XOR" operation, the result is 23, which restores the encrypted data.

Design Process]
(1) Open Visual Studio, create a Windows form application, and name it Encrypt.
(2) Change the Name attribute of the default form Form1 to FrmEncrypt. The form layout is as follows:
 

The key source code is as follows:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace Encrypt {public partial class FrmEncrypt: Form {public FrmEncrypt () {InitializeComponent ();} private void Encrypt (object sender, EventArgs e) {int P_int_Num, P_int_Key; if (int.TryParse(this.txt _ Nu M. text, out P_int_Num) & int.TryParse(this.txt _ Key. text, out P_int_Key) {this.txt _ Encrypt_1.Text = this.txt _ Encrypt_2.Text = (P_int_Num ^ P_int_Key ). toString ();} else {MessageBox. show ("enter a value! "," Error: ") ;}} private void btn_Revert_ OK _Click (object sender, EventArgs e) {int P_int_Key, P_int_Encrypt; if encryption _ Encrypt_2.Text, out P_int_Key) & int.TryParse(this.txt _ Key. text, out P_int_Encrypt) {this.txt _ Revert. text = (P_int_Encrypt ^ P_int_Key ). toString ();} else {MessageBox. show ("enter a value! "," Error: ") ;}}} key code

  

 

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.