野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13615|回复: 4

谁能给个TEA算法的测试例程的C语言写的啊!

[复制链接]
发表于 2016-9-11 16:30:17 | 显示全部楼层 |阅读模式
1火花
谁能给个TEA算法的测试例程的C语言写的啊!不胜感激!!!!

回复

使用道具 举报

 楼主| 发表于 2016-9-11 16:30:40 | 显示全部楼层
顶起来啊!
回复

使用道具 举报

发表于 2016-9-11 17:17:59 | 显示全部楼层
用C写的TEA算法源代码
  1. /*
  2. The Tiny Encryption Algorithm, or TEA, is a Feistel cipher invented by David
  3. Wheeler. It is intended for use in applications where code size is at a
  4. premium, or where it is necessary for someone to remember the algorithm and
  5. code it on an arbitrary machine at a later time.

  6. Since its round function is relatively weak, with nonlinearity coming only from
  7. the carry propagation, TEA has 64 rounds. However, its simplicity means that it
  8. runs more quickly in software than many other algorithms with fewer, more
  9. complex, rounds.
  10. */

  11. void code(long* v, long* k)
  12. {
  13. unsigned long y=v[0],z=v[1],sum=0,             /* set up */
  14.               delta=0x9e3779b9, n=32 ;         /* key schedule constant*/

  15. while (n-->0)
  16. {                                              /* basic cycle start*/
  17.   sum += delta ;
  18.   y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1] ;
  19.   z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3] ;     /* end cycle */
  20. }
  21. v[0]=y ;
  22. v[1]=z ;
  23. }
复制代码
回复

使用道具 举报

发表于 2016-9-11 17:18:42 | 显示全部楼层
  1. /**********************************************************
  2.    TEA - Tiny Encryption Algorithm
  3.    Feistel cipher by David Wheeler & Roger M. Needham
  4.    (extended version)
  5. **********************************************************/

  6. #define ROUNDS 32
  7. #define DELTA 0x9e3779b9 /* sqr(5)-1 * 2^31 */

  8. #include "ctypes.h"

  9. /**********************************************************
  10.    Input values:         k[4]        128-bit key
  11.                         v[2]    64-bit plaintext block
  12.    Output values:        v[2]    64-bit ciphertext block
  13. **********************************************************/

  14. void tean(word32 *k, word32 *v, long N) {
  15.   word32 y=v[0], z=v[1];
  16.   word32 limit,sum=0;
  17.   if(N>0) { /* ENCRYPT */
  18.     limit=DELTA*N;
  19.     while(sum!=limit) {
  20.       y+=((z<<4)^(z>>5)) + (z^sum) + k[sum&3];
  21.       sum+=DELTA;
  22.       z+=((y<<4)^(y>>5)) + (y^sum) + k[(sum>>11)&3];
  23.     }
  24.   } else { /* DECRYPT */
  25.     sum=DELTA*(-N);
  26.     while(sum) {
  27.       z-=((y<<4)^(y>>5)) + (y^sum) + k[(sum>>11)&3];
  28.       sum-=DELTA;
  29.       y-=((z<<4)^(z>>5)) + (z^sum) + k[sum&3];
  30.     }
  31.   }
  32.   v[0]=y; v[1]=z;
  33. }

  34. void cl_enc_block(word32 *k, word32 *v) {
  35. tean(k,v,ROUNDS);
  36. }

  37. void cl_dec_block(word32 *k, word32 *v) {
  38. tean(k,v,-ROUNDS);
  39. }
复制代码
回复

使用道具 举报

发表于 2016-9-12 09:20:55 | 显示全部楼层
这是加密算法???长见识了 。。。。楼主记得问题解决后整理发帖。。。谢谢!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-29 06:21 , Processed in 0.026475 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表