TEA对称加密算法

  今天在看《Distributed Systems Concepts and Design》这本书的时候,在讲到分布式系统的安全性的时候,给出了TEA算法,书本上有现成的代码,所以摘录下来以备后用。下面摘自百度百科的简短介绍:

  TEA算法由剑桥大学计算机实验室的David Wheeler和Roger Needham于1994年发明[3]。它是一种分组密码算法,其明文密文块为64比特,密钥长度为128比特。TEA算法利用不断增加的Delta(黄金分割率)值作为变化,使得每轮的加密是不同,该加密算法的迭代次数可以改变,建议的迭代次数为32轮。

  加密算法代码如下:

 1 void encrypt(unsigned long k[], unsigned long text[])
 2 {
 3     unsigned long y = text[0];
 4     unsigned long z = text[1];
 5     unsigned long delta = 0X9E3779B9;
 6     unsigned long sum = 0;
 7     int n;
 8     
 9     for(n = 0; n < 32; n++)
10     {
11         sum += delta;
12         y += ((z << 4) + k[0]) ^ (z + sum) ^ ((z >> 5) + k[1]);
13         z += ((y << 4) + k[2]) ^ (y + sum) ^ ((y >> 5) + k[3]);
14     }
15     
16     text[0] = y;
17     text[1] = z;
18 }

  解密算法代码如下:

 1 void decrypt(unsigned long k[], unsigned long text[])
 2 {
 3     unsigned long y = text[0];
 4     unsigned long z = text[1];
 5     unsigned long delta = 0X9E3779B9;
 6     unsigned long sum = delta << 5; 
 7     
 8     int n;
 9     for (n= 0; n < 32; n++) 
10     {
11         z -=((y << 4) + k[2]) ^ (y + sum) ^ ((y >> 5) + k[3]);
12         y -=((z << 4) + k[0]) ^ (z + sum) ^ ((z >> 5) + k[1]);
13         sum -=delta;
14     }
15     text[0] = y; 
16     text[1] = z;     
17 }

  测试用例代码如下:

 1 #include    <iostream>
 2 int main()
 3 {
 4     char key[16] = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0xA7, 0xB8, 0xC9, 0xD1, 0xE2, 0xF3, 0xA4, 0xB5, 0xC6, 0xD7};
 5     char text[8] = {'A','B',' ','C','D', 0, 0, 0};
 6     
 7     std::cout << "origin:" << text << std::endl;
 8     encrypt((unsigned long*)key, (unsigned long*)text);
 9     std::cout << "Encrypt:[" << std::endl;
10     std::cout << text << std::endl;
11     std::cout << "]" << std::endl;
12     decrypt((unsigned long*)key, (unsigned long*)text);
13     std::cout << "Decrypt:" << text << std::endl;
14     return 0;
15 }

  测试时,将以上三段代码复制到TEA.c文件中,然后编译执行。我运行的过程和结果如下:

[kiven@localhost encode]$ g++ -o TEA TEA.c
[kiven@localhost encode]$ ./TEA
origin:AB CD
Encrypt:[
ô·Kñ
y0:k¡²ÃÔåö§¸ÉÑâó¤µÆ×
]
Decrypt:AB CD
[kiven@localhost encode]$

  我从书中看到说,该算法运行效率很高,而且比较难破解,所以用在自己的网络项目中应该很合适。实现简单,效率高。但具体效率如何,还需要进一步测试。书中提供了一个对文件加密解密的函数,提供如下:

 1 void tea(char mode, FILE *infile, FILE *outfile, unsigned long k[]) 
 2 {
 3     /* mode is 'e' for encrypt, 'd' for decrypt, k[] is the key.*/
 4     char ch, Text[8]; int i;
 5     while(!feof(infile)) 
 6     {
 7         i = fread(Text, 1, 8, infile); /* read 8 bytes from infile into Text */
 8         if (i <= 0) break;
 9         while (i < 8) { Text[i++] = ' ';} /*pad last block with spaces */
10         switch (mode) 
11         {
12             case 'e':
13             encrypt(k, (unsigned long*) Text); break;
14             case 'd':
15             decrypt(k, (unsigned long*) Text); break;
16         }
17         fwrite(Text, 1, 8, outfile); /* write 8 bytes from Text to outfile */
18     }
19 }

 

    原文作者:kiven.li
    原文地址: http://www.cnblogs.com/kiven-code/archive/2013/11/30/3451267.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞