Android data encryption or encryption algorithm for the implementation of the method _android

Source: Internet
Author: User
Tags decrypt

These days the company temporarily pulled to do Android IM instant Messaging protocol implementation, the general view of their agreement, because the previous did not participate, is said to be due to server performance restrictions, only to achieve a non-clear delivery, specific reasons I am not clear, but the encryption used here is the use of XOR or encryption. This encryption method was used before the Android encryption Notepad. Today has the client heartbeat maintenance, data packet resolution docking, summed up this encryption method.

What is XOR or encryption?

In the XOR operation, if a character (or value) x is different from a value m, or if the operation gets y, then the Y and M can be converted to X, and therefore the encryption and decryption function of the data can be realized by applying this principle.

Usage scenarios for XOR or operation?

Two-variable interchange (with no third variable)

Simple encryption and decryption of data

Encryption or decryption implementation?

1. How to fix key

Public byte[] Encrypt (byte[] bytes) {
    int len = bytes.length;
    int key = 0x12;
    for (int i = 0; i < len; i++) {
      bytes[i] ^= key;
    }
    return bytes;
  }

Testing Cryptographic decryption

 byte[] bytes = Encrypt ("WHOISLCJ". GetBytes ());//Encrypt
 string str1 = new String (Encrypt (bytes));//Decrypt

2. The way of not fixing key

Encryption implementation

Public byte[] Encrypt (byte[] bytes) {
    int len = bytes.length;
    int key = 0x12;
    for (int i = 0; i < len; i++) {
      bytes[i] = (byte) (Bytes[i] ^ key);
      key = Bytes[i];
    }
    return bytes;
  }

Decryption implementation

Public byte[] Decrypt (byte[] bytes) {
    int len = bytes.length;
    int key = 0x12;
    for (int i = len-1 i > 0; i--) {
      bytes[i] = (byte) (Bytes[i] ^ bytes[i-1]);
    Bytes[0] = (byte) (Bytes[0] ^ key);
    return bytes;
  }

Test

byte[] bytes = Encrypt ("WHOISLCJ". GetBytes ());//Encrypt
string str1 = new String (decrypt (bytes));//Decrypt

Summarize:

Bit operations can achieve many advanced, efficient operations. For example, in the multiplication of the N-second side is the right to move N-bit, fast. Im binary packet using XOR or algorithm first to achieve encryption, the second use of XOR or encryption algorithm will not change the length of the binary data packets to the binary packet has no small benefit. Pretend this summary.

The above is small series for everyone to bring the Android data encryption or encryption algorithm to achieve the full content of the method, I hope that we support cloud-Habitat Community ~

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.