Source: Encrypted transmission of data--tea encryption and decryption algorithm implemented on single chip microcomputer
Heroes in doing data transmission, have not considered the data encryption for transmission, if the serial or wireless data to be transmitted in the encryption, it is not to increase the security of communications. Commonly used encryption and decryption algorithms such as DES, RSA, limited by the single-chip memory and operation speed, it is difficult to achieve, but a call tea encryption algorithm is particularly suitable for single-chip computer use.
TEA (Tiny encryption algorithm) is a simple and efficient encryption algorithm, which is known for its fast encryption and decryption speed and simplicity. The algorithm is simple, each time Tea algorithm can operate 64-bit (8-byte), using 128-bit (16-byte) as key, the algorithm uses the form of iteration, the recommended number of iterations is 64 rounds, at least 32 rounds. At present, I only know QQ has been using 16 rounds of tea.
I have done a number of wireless walkie-talkie, the voice data encryption after sending, both parties in advance to specify a good public key, it can be encrypted and decrypted. As for the tea algorithm speed, it seems to me very quickly, I was using a 16-bit msp430 microcontroller, Crystal oscillator only 6 m, about two hundred or three hundred times per second encryption and decryption operations (one time encryption and decryption of 32 bytes).
When it comes to encryption, the simplest way is to make the data to be sent and the same length of the password to be different or operation, the new data is the encrypted data, and then the receiver will encrypt the data and passwords to the original data can be obtained. But I don't know how safe this kind of approach is.
The following uploads the C + + implementation of the tea algorithm, you can debug in the VC look. I changed it to make it suitable for single-chip computer, the following TEA.h and TEA.C can be included in your project. When using, Modify the macro definition Block_size accordingto the length of the packet you want to encrypt, the length of the key is 16 bytes . Both the data and the key are stored in the array, for example:
//Tea KeyUnsignedChartea_key[ -]={ 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};//Data BuffersUnsignedChartx_buffer[ +];unsignedCharrx_buffer[ +];
Use functions when encrypting:
Btea_encrypt (Tx_buffer,tea_key); Tea encryption
In this way, the new content inside the array tx_buffer is the encrypted data.
The ciphertext data received is stored in Rx_buffer, calling the following function:
DECRPYT (Rx_buffer,tea_key); Tea decryption
will be able to get the previous plaintext.
/******************* tea encryption and decryption algorithm *******************/#include"TEA.h"#defineMX (z>>5^y<<2) + (y>>3^z<<4) ^ (sum^y) + (k[p&3^e]^z)#defineDELTA 0X9E3779B9#defineS_looptime 1//5#defineBlock_size 31//page_size, modify this parameter (in bytes) based on the length of the packet you want to encrypt/**key maybe 128bit =16 bytes.*buf maybe Block_size*/voidBtea_encrypt (unsignedChar* buf, unsignedChar*key) {unsignedCharn=block_size/4; unsignedLong*v= (unsignedLong*) buf; unsignedLong*k= (unsignedLong*) key; unsignedLongz = v[n-1],y = v[0],sum =0, E; unsignedCharp,q; //Coding PartQ= S_looptime + the/N; while(q-->0) {sum+=DELTA; E= Sum >>2&3 ; for(P =0; P < n-1; p++) y= V[p +1], Z= V[p] + =MX; Y= v[0] ; Z= V[n-1] +=MX; }}/**key Maybe 128bit =16bytes.*buf maybe block_sizeinbuf = = Outbuf = buf*/voidBTEA_DECRPYT (unsignedChar* buf, unsignedChar*key) {unsignedCharn=block_size/4; unsignedLong*v= (unsignedLong*) buf; unsignedLong*k= (unsignedLong*) key; unsignedLongz = v[n-1],y = v[0],sum =0, E; unsignedCharp,q; //Decoding part ...Q = s_looptime + the/N; Sum= Q *DELTA; while(Sum! =0) {e= Sum >>2&3 ; for(p = n-1; p >0; p--) Z= V[p-1], y= V[p]-=MX; Z= V[n-1] ; Y= v[0] -=MX; Sum-=DELTA; }}
#ifndef __tea_h__ #define __tea_h__//TEA cryptographic function voidcharchar * key); // Tea decryption Function void Char Char* key); #endif
Encrypted transmission of data--tea encryption and decryption algorithm implemented on single chip computer (EXT)