一样的TEA加密代码,C51和STM32结果不一样?
测试了论坛里的一个TEA加密代码,在51和STM32里编译仿真,加密解密都正常,但是加密结果不一样,郁闷,这么说51加密的STM32解不了?
代码如下:
#include"at89x51.h"
#define MX (z>>5^y<<2)+(y>>3^z<<4)^(sum^y)+(k[p&3^e]^z)
#define DELTA 0x9e3779b9
#define S_LOOPTIME 32 //5
#define BLOCK_SIZE 64 //PAGE_SIZE,根据你所要加密的数据包长度修改此参数(单位:字节)
/*
*key maybe 128bit =16 Bytes.
*buf maybe BLOCK_SIZE
*/
void btea_encrypt( unsigned char* buf, unsigned char* key )
{
unsigned char n=BLOCK_SIZE/4;
unsigned long *v=(unsigned long *)buf;
unsigned long *k=(unsigned long *)key;
unsigned long z = v[n - 1],y = v[0],sum = 0,e ;
unsigned char p,q ;
// Coding Part
q = S_LOOPTIME + 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;
}
}
/*
*key maybe 128bit =16Bytes.
*buf maybe BLOCK_SIZE
inbuf == outbuf == buf
*/
//TEA密钥
unsigned char TEA_key[16]=
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10
};
//数据缓冲区
unsigned char xdata TX_buffer[64]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63};
unsigned char xdata RX_buffer[64];
void btea_decrpyt( unsigned char* buf, unsigned char* key )
{
unsigned char n=BLOCK_SIZE/4;
unsigned long *v=(unsigned long *)buf;
unsigned long *k=(unsigned long *)key;
unsigned long z = v[n - 1],y = v[0],sum = 0,e ;
unsigned char p,q ;
//Decoding Part...
q = S_LOOPTIME + 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 ;
}
}
void main(void)
{
while(1)
{
btea_encrypt(TX_buffer,TEA_key);
btea_decrpyt(TX_buffer,TEA_key);
}
}
复制代码
转载
|