Use the tea algorithm to encrypt data

Source: Internet
Author: User
Tags crypt

If you have analyzed the QQ protocol, you may know that QQ data transmission is encrypted using the tea algorithm. The tea algorithm is a symmetric encryption algorithm featuring high speed and small amount of code (the Core algorithms for encryption and decryption are only 20 lines in total ). Although the security of the algorithm is not higher than that of AES, the difficulty of deciphering of the algorithm depends on the number of iterations. QQ is said to be 16 iterations on the Internet, and the recommended iterations are 64. I have no resistance to these simple and small things, so I searched for the code on the Internet and studied it. I found a class encapsulated by others, which is very useful. I will package it for you later. I processed the code and wrote a simple program for encrypting strings. First, a class on the internet is referenced. The 90 lines of code are concise. The encryption method is: 1 TEA (key, TIMES, false); 2 tea. encrypt (plain, crypt); the decryption method is: 1 TEA tea (key, TIMES, false); 2 tea. decrypt (crypt, plain); Input key in the constructor. TIMES is the number of iterations. I use 64 TIMES. The third parameter is "whether it is designed for network order ", if the encrypted ciphertext is not transmitted over the network, set this parameter to false. The encrypt method encrypts the ciphertext, plain is the plaintext, and crypt is the buffer zone of the output ciphertext. The decrypt method is the opposite. The following are some notes: first, the plain, key, and crypt parameter types are all byte *, that is, unsigned char *, which stores the binary code, it cannot be output directly on the screen, so if you want to encrypt the string and output hex, you have to add some code for processing. If you encrypt an object, you don't have to process it. Second, the key length is 16 bits, so I use an md5 for processing. Hash the input string of any length into md5, and then pass it into the tea constructor as a 16-bit key. Third, the length of the plaintext must be 8 bits. Therefore, I divide the plaintext into multiple 8-bit segments and encrypt each segment. If the ciphertext length is not a multiple of 8, I will add random binary digits to the end of the last plain text to make it 8bit. Therefore, the results of plaintext encryption may be different, but the last 8 bits must be different. The previous steps should be the same. In the last 8 bits, the valid length is placed first in the ciphertext. Therefore, the ciphertext length must be an odd number. I watched QQ's TEA encryption on the Internet using the following two techniques: similar to me, the random binary complement is a multiple of 8, but the intertwined algorithm is used: messages are divided into multiple encryption units, each encryption unit is 8 bytes. TEA is used for encryption. The encryption result is an exclusive or operation with the next encryption unit before being used as the plaintext to be encrypted. This will make it more complicated and safer, but I don't think it is so troublesome. If you have higher security requirements, you can change it yourself. I package the source files to everyone, including my entire project. Under the attachment. You can modify it as needed.

Http://www.bkjia.com/uploadfile/2013/1218/20131218010331498.zip

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.