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 ~