iOS和.NET中PHP加密的区别 [英] Difference in PHP encryption from iOS and .NET

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

问题描述

在iOS和PHP之间进行加密通信时,我有一个问题。我有一个应用程序加密一个字符串并将其发送到解密它的PHP服务器。那部分工作很好。现在,PHP服务器需要将加密的响应发送回应用程序,这似乎是导致一个
更多的头发。

I have an issue when communicating encrypted between iOS and PHP. I have an app that encrypts a string and sends it to a PHP server that decrypts it. That part works just fine. Now the PHP server needs to send an encrypted response back to the app, which seems to be causing a bit more gray hair.

问题是,那个当我加密PHP中的字符串时,它看起来与在iOS甚至.NET中加密的相同字符串不同 - 显然所有地方都使用相同的算法,密钥和IV。

The issue is, that when I encrypt a string in PHP it looks different from the same string encrypted in iOS and even .NET - obviously all places use the same algorithm, key and IV.

I在CBC模式中使用Rijndael 128,其中包含空字节(迄今为止)。

I use Rijndael 128 in CBC mode with an IV consisting of empty bytes (so far).

PHP加密看起来如此:

The PHP encryption looks so:

$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->secret_key, $str, MCRYPT_MODE_CBC, $this->iv );
$encrypted = base64_encode( $encrypted );

此文件中附有iOS加密:

The iOS encryption is attached in this file:

StringEncryption.m: http://pastie.org/1365766

StringEncryption.m: http://pastie.org/1365766

我希望有人可以帮助我找到我错过某些东西或者有不同参数的值。我已经看了几个小时,找不到任何其他的东西。

I hope someone can help me spot where I'm missing something or have some different parameters of values. I have looked at this for several hours, and can't find anything else to try.

推荐答案

很可能是一个填充问题...请参阅此处这里获取更多信息。

Most likely it's a padding issue... Please see here or here for more information.

编辑

PHP没有内置的支持其他填充模式比 NULL -padding。至少.Net允许您指定NULL填充(我认为),另一个选项是在PHP中实现PKCS#7填充,这不是很难做到。

PHP has no built-in support for other padding modes than the NULL-padding. At least .Net allows you to specify NULL-padding (I think), the other option would be to implement PKCS#7-padding in PHP which is not that difficult to do.


使用填充字符串
在1到8个字节之间填充输入,以使
的总长度为8
字节的精确倍数。
填充字符串的每个字节的值被设置为添加的
字节的数量 - 即值为
0x08的8个字节,值为0x07,...,2 $ b的7个字节$ b字节的0x02,或一个字节的值
0x01。

Pad the input with a padding string of between 1 and 8 bytes to make the total length an exact multiple of 8 bytes. The value of each byte of the padding string is set to the number of bytes added - i.e. 8 bytes of value 0x08, 7 bytes of value 0x07, ..., 2 bytes of 0x02, or one byte of value 0x01.



$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$padding   = $blockSize - (strlen($data) % $blockSize);
$data      .= str_repeat(chr($padding), $padding);

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

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