Parsing Android data encryption or encryption algorithm _android

Source: Internet
Author: User
Tags decrypt md5 encryption

Objective:

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.

Several other encryption methods:

    • RSA encryption of Android data encryption
    • AES encryption of Android data encryption
    • Encryption des encryption of Android data
    • MD5 encryption of Android data encryption
    • BASE64 coding algorithm of Android data encryption

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

This way the encryption decryption algorithm is the same

Public byte[] Encrypt (byte[] bytes) {
 if (bytes = null) {return
  null;
 }
 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) {
 if (bytes = null) {return
  null;
 }
 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) {
 if (bytes = null) {return
  null;
 }
 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 the entire content of this article, I hope to help you learn, but also hope that we support the 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.