Sharing or encryption

Source: Internet
Author: User
0. Preface

This article includes the following content:

  • Variance or Algorithm
  • Unique or encrypted
  • Two integer exchanges


1. XOR Algorithms

The basic concepts in different or digital logic are also the basic operations supported by each programming language. The basic principle is that there are the following calculation formulas for numbers 1 and 0:

1 ^ 1 = 00 ^ 0 = 01 ^ 0 = 10 ^ 1 = 1

Naturally, this operation can be extended to a long string of code streams.


More common is the exclusive or operation of level 1 or integer bytes. You can refer to the related introduction of the corresponding programming language.


2. Unique or encrypted

Exclusive or encryption is the simplest encryption method. In simple terms, assume that there is a code stream A, and then you can encrypt it with the key to save or send the encrypted data. After the encrypted data is read, the key is used for decryption.

Code is easier to understand, as shown in the following example:

#include <stdio.h>#include <stdlib.h>static void xor_encrypt(char *data, char *key, int len){    int i = 0;    for (i = 0; i < len; i++) {        data[i] ^= key[i];    }}static void dumphex(const unsigned char *data, int len){    int i;    for (i = 0; i < len; i++) {        printf("%02X", data[i]);    }    printf("\n");}static unsigned char* get_random_key(int len){    int i = 0;    unsigned char* buffer = (unsigned char*)malloc(len);    for (i = 0; i < len; i++) {        buffer[i] = rand() % 10 + '0';    }    return buffer;}int main(){    unsigned char data[] = "hello, xor!";    int len = sizeof(data);    unsigned char *key;    key = get_random_key(len);    printf("original data: %s\n", data);    printf("original data (hex): ");    dumphex(data, len);    xor_encrypt(data, key, len);    printf("encrypted data: %s\n", data);    printf("encrypted data (hex): ");    dumphex(data, len);    xor_encrypt(data, key, len);    printf("original data: %s\n", data);    printf("original data (hex): ");    dumphex(data, len);    free(key);    return 0;}

Running result:

original data: hello, xor!original data (hex): 68656C6C6F2C20786F722100encrypted data: [S[Y\JVC7encrypted data (hex): 5B535B595C19164A56431337original data: hello, xor!original data (hex): 68656C6C6F2C20786F722100

3. Exchange two integers

Some TX may encounter this problem during the interview:

There are two integers A and B. How do I swap the values of two integers without introducing the third variable?

After learning about the concept of exclusive or, this problem can be solved. Directly give the code:

#include <stdio.h>void swap(int &x, int &y){    x ^= y;    y ^= x;    x ^= y;}int main(){    int x = 55555;    int y = 33333;    printf("first, x = %d, y = %d\n", x, y);    swap(x, y);    printf("and now, x = %d, y = %d\n", x, y);    return 0;}

Execution result:

[email protected]:~/docs/The_Road_to_Cpp/src$ g++ swap_ints.cc [email protected]:~/docs/The_Road_to_Cpp/src$ ./a.out first, x = 55555, y = 33333and now, x = 33333, y = 55555[email protected]:~/docs/The_Road_to_Cpp/src$ 

4. Repeat key

The key mentioned above when talking about XOR encryption. For remote communication, if the sender sends encrypted data, the receiver must use the original key to decrypt the data. How can the receiver obtain the key? For more information about encryption, see Chapter 2 of core Java. -Books from foreigners are always easy to understand. Examples and stories are used to draw a series of advanced topics. We recommend that you read TXS several times.


Here we provide a simple data & key transmission method, which is also used by many projects. That is:

  • Assume that there is raw data and the length is Len;
  • Generate a string of key keys with the length of Len (such as the random number given in the preceding example );
  • The length of the sent data must be 2 * Len. The LEN part is data ^ key, and the Len part is key;
  • After receiving the data, the receiver uses the Len part to be different or to the Len part. The final result is the original data.


Of course, it may be relatively simple. Other algorithms can be further introduced on this basis, for example:

  • Add a byte to introduce the parity code. -- refer to imsi coding rules and parity algorithms.
  • Disrupt the code stream order according to certain rules, such as the exchange of two adjacent bytes;
  • And so on.


5. References

Wikipedia overview of exclusive or encrypted: http://zh.wikipedia.org/wiki/%E5%BC%82%E6%88%96%E5%8A%A0%E5%AF%86

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.