C ++ exclusive or encrypted (most basic)

Source: Internet
Author: User

When I read a book today, I see: "The characteristics of the exclusive or operation are: if a ^ B = C, then C ^ B = A and C ^ A = B. This rule can be used

."

I plan to give it a try. I first assume that there is a key and then the number is given. However, in order to encrypt the key, the number ^ key can be re-obtained once the key is decrypted or different. So how much is the key.

So I first wrote

int a,b;cin>>a>>b;cout<<a^b;

Then, the compilation was naturally unable to pass. Later, I asked people everywhere to ignore me. I was a bit-wise or perhaps bit-wise, but int was 4 bytes, so it didn't match. Therefore, replace it with Char [] for saving. (I made a mistake at the beginning and explained it later)

The following code is attached:

# Include <iostream> using namespace STD; # define N 7 char key [] = {'A', 'B', 'C', 'D', 'E ', 'F', 'G'}; int main () {char number [N], encode [N]; for (INT I = 0; I <n; ++ I) cin> Number [I]; for (INT I = 0; I <n; ++ I) encode [I] = number [I] ^ key [I]; // encrypted for (INT I = 0; I <n; ++ I) cout <encode [I]; cout <Endl; For (INT I = 0; I <n; ++ I) cout <char (encode [I] ^ key [I]); // decrypt cout <Endl; System ("pause "); return 0 ;}

Another small problem is forced conversion. I found that the numeric type will change to int type after the difference or occurrence, and I have to force the conversion.

Here is a problem corrected. In fact, the reason why I cout <a ^ B; the error was actually because the priority of ^ is lower than the <symbol, because this problem can be solved with parentheses. -_-!

Cout <(a ^ B); I learned this time ..

In fact, ^ supports long, Int, short, and unsigned Int.

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.