CTE中的XTEA加密和C中的解密 [英] XTEA encryption in PHP and decryption in C
问题描述
我的网站代码是:
code> mcrypt_encrypt(MCRYPT_XTEA,'qwertyuiasdfghjk','asdfasdf',MCRYPT_MODE_ECB);
其中qwertyuiasdfghjk是128位的密钥,asdfasdf是64位的消息。
在Arduino方面我使用的是:
void _xtea_dec (void * dest,const void * v,const void * k)
{
uint8_t i;
uint32_t v0 =((uint32_t *)v)[0],v1 =((uint32_t *)v)[1];
uint32_t sum = 0xC6EF3720,delta = 0x9E3779B9; (i = 0; i< 32; i ++)
{
v1 - =((v0 <4 ^ v0> 5)+ v0)^(sum +((uint32_t *)k)> 11& 3]);
sum - = delta;
v0 - =((v1 <4 ^ v1>> 5)+ v1)^(sum +((uint32_t *)k)[sum& 3]);
}
((uint32_t *)dest)[0] = v0; ((uint32_t的*)目标寄存器)[1] = V 1;
}
参数如下:
char dest [9]; // Destination
char v [9]; //加密消息
char k [17]; //键
但我的解密消息远离原始消息...它仍然有64 ;位,但是完全不同...
我该怎么办?
(这是第一次我在这里提出一个问题,通常我所有的问题都在Stack Overflow ...的某个地方得到解决。)
很可能你的密钥是不同的。确保两端都一样。
C:
//烦人的猴子
uint32_t key [4] = {0x6f6e6e61,0x676e6979,0x6e6f6d20,0x0079656b};
PHP:
code> mcrypt_encrypt(MCRYPT_XTEA,烦人的猴子,数据,MCRYPT_MODE_ECB);
I'm trying to establish communication between a website and an Arduino. I need to authenticate all the messages from my website to the Arduino, so I have found that the less time expensive way is using XTEA cryptography.
My PHP code for the website is:
mcrypt_encrypt(MCRYPT_XTEA, 'qwertyuiasdfghjk', 'asdfasdf', MCRYPT_MODE_ECB);
where "qwertyuiasdfghjk" is a 128 bits key and "asdfasdf" is a 64 bits message.
On the Arduino side I'm using:
void _xtea_dec(void* dest, const void* v, const void* k)
{
uint8_t i;
uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
for(i=0; i<32; i++)
{
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
sum -= delta;
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
}
((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
}
where the parameters are:
char dest[9]; //Destination
char v[9]; //Encrypted message
char k[17]; //Key
but my decrypted message is far away from the original message... It still having 64 bits, but it is totally different...
What should I do?
(This is the first time that I ask a question here, usually I all my questions are solved somewhere in Stack Overflow...)
Most likely your cipher keys are different. Make sure they are the same in both ends.
C:
// "annoying monkey"
uint32_t key[4] = {0x6f6e6e61, 0x676e6979, 0x6e6f6d20, 0x0079656b };
PHP:
mcrypt_encrypt(MCRYPT_XTEA, 'annoying monkey', 'data', MCRYPT_MODE_ECB);
这篇关于CTE中的XTEA加密和C中的解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!