Tea,xxtea Introduction, symmetric encryption

Source: Internet
Author: User

Summary: In the use of encryption, we can add random numbers, so that the same plaintext, each time the encryption to obtain a different ciphertext, while the ciphertext can be added to the validity of the ciphertext, the effective length of time to control the cipher text.

For some function extension use, very good ideas.

Tea has a 128-bit encryption key for 64-bit data, which is very secure. Its reliability is ensured by the number of encryption wheels rather than by the complexity of the algorithm. We can see that the tea algorithm mainly uses the shift and the XOR operation. The key is always the same during the encryption process.

Tea (Tiny encryption algorithm) is a small symmetric encryption and decryption algorithm that supports 128-bit ciphers, which can only encrypt/decrypt 8 bytes of data at a time, like Blowfish. Tea is characterized by its fast speed, high efficiency and simple implementation. Tea has also developed several versions, namely Xtea, Block tea and Xxtea, as attacks on tea continue to occur.

Tea uses 64 bits of a set of encryption, a 128-length secret key, and encrypted to the result. Relatively xxtea its cryptographic reliability is low.

void Encrypt (unsigned long *v, unsigned long *k) {
2unsigned long y=v[0], z=v[1], sum=0, I; /* Set up */
3unsigned long delta=0x9e3779b9; /* A key schedule constant */
4unsigned long a=k[0], b=k[1], c=k[2], d=k[3]; /* Cache Key */
5for (i=0; i <; i++) {/* Basic cycle start */
6sum + = Delta;
7Y + = ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + B);
8z + = ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + D);/* End cycle */
9}
10V[0]=y;
11V[1]=z;
12}
13
14void decrypt (unsigned long *v, unsigned long *k) {
15unsigned long y=v[0], z=v[1], sum=0xc6ef3720, I; /* Set up */
16unsigned long delta=0x9e3779b9; /* A key schedule constant */
17unsigned long a=k[0], b=k[1], c=k[2], d=k[3]; /* Cache Key */
18for (i=0; i<32; i++) {/* Basic cycle start */
19Z-= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + D);
20         y -=  ( (z<<4)  + a)  ^  (z + sum)  ^  ((z>>5)  + b);
21         sum -=  delta;                                 /* end  cycle */
22     }
23     v[0]=y;
24     v[1]=z;
25 }

Xxtea encrypted string length is not an integer multiple of 4, these implementations can not be really restored after encryption, after the restoration of the string is actually not equal to the original string, but more than some of the following characters, or a few characters. The reason for this is that the Xxtea algorithm only defines how to encrypt a 32-bit array of information blocks, which is actually an array of 32-bit unsigned integers, and does not define how to encode strings into such arrays. In an existing implementation, the string length information is lost when the string is encoded as an integer array, so there is a problem with the restore.

#define MX (Z&GT;&GT;5^Y&LT;&LT;2) + (y>>3^z<<4) ^ (sum^y) + (k[p&3^e]^z);

Long Btea (long* V, long N, long* k) {
unsigned long z=v[n-1], y=v[0], sum=0, E, delta=0x9e3779b9;
Long p, q;
if (n > 1) {
Q = 6 + 52/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) {
n =-N;
Q = 6 + 52/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;
}

Long Btea (long* V, long N, long* K)

V is the starting address of the group to be encrypted, in 32bit, which is implemented with a long.

n is the number of tuples to encrypt, positive numbers are encrypted, and negative numbers are decrypted.

K is the starting address of the key, with a length of 4 elements, 4*32=128bit.

The return value is 0 or 1 (corresponds to n=0, not calculated).

The results of the encryption are directly written back to V.

After experimenting, I have one more thing to add, Xxtea's calculations are spatially related, that is, in a tuple, 4 bytes cannot be taken out of context, that is, part of the cipher, and cannot be reverted to a part of the plaintext. Therefore, when the data cannot be divisible by 4 bytes, it is necessary to do a good job of filling and assisting the byte.

Tea,xxtea Introduction, symmetric encryption

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.