The source code of Xxtea encryption and decryption algorithm based on new Tang M0

Source: Internet
Author: User
Tags rounds

Source: Xxtea encryption and decryption algorithm based on new Tang M0 source code

/*--------------------------------------------------------------------------------- ------------------------in the field of data encryption and decryption, the algorithm is divided into two kinds: symmetric key and asymmetric key. Symmetric and asymmetric keys are different in the field of application due to their respective characteristics.    Symmetric key encryption algorithm is widely used in the field of digital signature because of its fast speed and the general data encryption, and the non-symmetric key encryption algorithm has good security performance. The tea algorithm was invented by David Wheeler and Roger Needham of Cambridge University's computer laboratory in 1994, and tea is the abbreviation of tiny encryption algorithm, which is known for its fast encryption and decryption speed and simplicity. Each time the tea algorithm can operate 64bit (8byte), using 128bit (16byte) as key, the algorithm uses an iterative form, the recommended number of iterations is 64 rounds, at least 32 rounds. To solve the problem of tea algorithm key table attack, tea algorithm has undergone several improvements, from Xtea to block TEA, up to the newest xxtea.    Xtea also called Tean, which uses the same simple operations as tea, but four subkeys are mixed in an informal manner to prevent key-table attacks. The Block tea algorithm can encrypt and decrypt 32-bit variable blocks of arbitrary integer length, and the algorithm applies the Xtea round-robin function to each word in the block in turn, and attaches it to the adjacent word of the applied word. Xxtea uses a structure similar to block tea, but uses adjacent words when dealing with each word in the block, and replaces the Xtea round function with an MX function with two inputs.    The encryption algorithm used in the security mechanism described in this paper is the improved version-xxtea algorithm which is the best security performance in tea algorithm. The structure of the Xxtea algorithm is very simple, only to perform addition, XOR and storage of hardware, and the software implementation of the code is very short, portable, ideal for embedded system applications. Because of the above advantages of Xxtea algorithm, it can be applied to embedded RFID system well.  ---------------------------------------------------------------------------------------------------------*/#include<string.h>#include<stdio.h>#defineMX (z>>5^y<<2) + (y>>3^z<<4) ^ (sum^y) + (k[p&3^e]^z)//Note: The delta value is random, but to avoid bad values, take//The number of golden segments (square root 5-2)/2 and 2 of the 32-time product.   For 0X9E3779B9. //in decryption, sum=deltaxround, such as: deltax32=13c6ef3720//v represents the first address of the long integer data for the operation//K is the first address of the key of the long integer type//N represents the number of tuples to be calculated as the base unit of 32bit, which represents the encryption and the negative representation of the decryptionLongXxtea (Long* V,LongNLong*k);LongXxtea (Long* V,LongNLong*k) {unsignedLongz=v[n-1], y=v[0], sum=0, E, Delta=0x9e3779b9; Longp, q; if(N >1)     {/*Encryption Process*/Q=6+ 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; }        return 0; }    Else    if(N <-1)     {/*decryption Process*/N=-N; Q =6+ 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; }        return 0; }      return 1; }voidXxtea_test (void) {unsignedLongbuff[4]; unsignedLongtest[4]={0x11223344,0x55667788,0X99AABBCC,0xddeeff00}; unsignedLongkeys[4]={0x12345678,0X9ABCDEF0,0x12345678,0X9ABCDEF0};  while(1) {printf ("\n\rxxtea test: \n\r"); memcpy (Buff,test, -); printf"0x%08x,0x%08x,0x%08x,0x%08x\n\r", buff[0],buff[1],buff[2],buff[3]); Xxtea (Buff,4, keys); printf"0x%08x,0x%08x,0x%08x,0x%08x\n\r", buff[0],buff[1],buff[2],buff[3]); Xxtea (Buff,-4, keys); printf"0x%08x,0x%08x,0x%08x,0x%08x\n\r", buff[0],buff[1],buff[2],buff[3]);  while(1); }}

The source code of Xxtea encryption and decryption algorithm based on new Tang M0

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.