http://bbs.mydigit.cn/read.php?tid=545086
#include "reg52.h" void send_char(unsigned char txd);void printuf(char *str, unsigned char cb) ; void encrypt(unsigned long *v1, unsigned long *v2, unsigned long *k); /*tea加密函数定义*/ void decrypt(unsigned long *v1, unsigned long *v2, unsigned long *k); /*tea解密函数定义*/ unsigned long *key = (unsigned long *)"1234567890abcde\0"; /*常量密钥*/ unsigned ]; unsigned char i; void InitUART(void) { TMOD = 0x20; SCON = 0x50; TH1 = 0xFD; TL1 = TH1; PCON = 0x00; EA = ; ES = ; TR1 = ; } void main() { unsigned long *y1,*y2; tea[]=0x31; tea[]=0x32; tea[]=0x33; tea[]=0x34; tea[]=0x35; tea[]=0x36; tea[]=0x37; tea[]=0x38; InitUART(); ; i < ; i++) send_char(tea); send_char(); y1=(unsigned ]; y2=(unsigned ]; encrypt (y1,y2,key) ; ; i < ; i++) printuf("%x ",tea); send_char(); y1=(unsigned ]; y2=(unsigned ]; decrypt(y1,y2,key); ; i < ; i++) send_char(tea); send_char(); ) ; } void encrypt(unsigned long *v1, unsigned long *v2,unsigned long *k) { unsigned , i; unsigned long delta=0x9e3779b9; unsigned ], b=k[], c=k[], d=k[]; ; i < ; i++) { sum += delta; y += ((z<<) + a) ^ (z + sum) ^ ((z>>) + b); z += ((y<<) + c) ^ (y + sum) ^ ((y>>) + d); } *v1=y; *v2=z; } void decrypt(unsigned long *v1,unsigned long *v2, unsigned long *k) { unsigned long y=*v1, z=*v2, sum=0xC6EF3720, i; unsigned long delta=0x9e3779b9; unsigned ], b=k[], c=k[], d=k[]; ; i<; i++) { z -= ((y<<) + c) ^ (y + sum) ^ ((y>>) + d); y -= ((z<<) + a) ^ (z + sum) ^ ((z>>) + b); sum -= delta; } *v1=y; *v2=z; } // 传送一个字符 void send_char(unsigned char txd) { SBUF = txd; while(!TI); // 等特数据传送 TI = ; // 清除数据传送标志 } // *str内容中遇到%x的内,就会从cb中的值转成十六进显示 开始 code ] = {', 'A', 'B', 'C', 'D', 'E', 'F'}; void printuf(char *str, unsigned char cb) //*str内容中遇到%x的内,就会从cb中的值转成十六进显示 { idata char *ct = str; idata char cx1; idata char cx2; while (*ct != '\0') { if (*ct == '%') { ) == 'x') { ct += ; cx1 = cb / ; cx2 = cb % ; send_char (hex[cx1]); send_char(hex[cx2]); continue; } ) == 'c') { ct += ; send_char(cb); continue; } } if (*ct == '\n') { send_char(); } send_char(*ct); ct++; } } // *str内容中遇到%x的内,就会从cb中的值转成十六进显示 结束