PHP中的XTEA加密和C中的解密 [英] XTEA encryption in PHP and decryption in C

查看:17
本文介绍了PHP中的XTEA加密和C中的解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网站和 Arduino 之间建立通信.我需要验证从我的网站到 Arduino 的所有消息,所以我发现使用 XTEA 密码学.

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.

我的网站 PHP 代码是:

My PHP code for the website is:

mcrypt_encrypt(MCRYPT_XTEA, 'qwertyuiasdfghjk', 'asdfasdf', MCRYPT_MODE_ECB);

其中qwertyuiasdfghjk"是 128 位密钥,asdfasdf"是 64 位消息.

where "qwertyuiasdfghjk" is a 128 bits key and "asdfasdf" is a 64 bits message.

在我使用的 Arduino 端:

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;
}

参数在哪里:

char dest[9]; //Destination
char v[9]; //Encrypted message
char k[17]; //Key

但我解密的消息与原始消息相去甚远......它仍然具有64位,但完全不同......

but my decrypted message is far away from the original message... It still having 64 bits, but it is totally different...

我该怎么办?

(这是我第一次在这里提问,通常我所有的问题都在 StackOverflow 的某个地方解决了...)

(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);

这篇关于PHP中的XTEA加密和C中的解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆