PHP 加密与 iOS 和 .NET 的区别
我在 iOS 和 PHP 之间进行加密通信时遇到问题。我有一个应用程序,可以加密字符串并将其发送到 PHP 服务器进行解密。那部分工作得很好。现在 PHP 服务器需要将加密的响应发送回应用程序,这似乎导致了一些问题 更多白发。
问题是,当我在 PHP 中加密字符串时,它看起来与在 iOS 甚至 .NET 中加密的同一字符串不同 - 显然所有地方都使用相同的算法、密钥和 IV。
我在 CBC 模式下使用 Rijndael 128,IV 包含空字节(到目前为止)。
PHP 加密看起来像这样:
$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->secret_key, $str, MCRYPT_MODE_CBC, $this->iv );
$encrypted = base64_encode( $encrypted );
iOS 加密附加在此文件中:
StringEncryption.m: http://pastie.org/1365766< /a>
我希望有人可以帮助我找出我遗漏的地方或有一些不同的参数值。我已经看了几个小时了,找不到其他可以尝试的东西。
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.
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 use Rijndael 128 in CBC mode with an IV consisting of empty bytes (so far).
The PHP encryption looks so:
$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->secret_key, $str, MCRYPT_MODE_CBC, $this->iv );
$encrypted = base64_encode( $encrypted );
The iOS encryption is attached in this file:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很可能是填充问题...请参阅此处 或此处了解更多信息。
OP 注释后编辑:
除了
NULL
填充之外,PHP 没有内置支持其他填充模式。至少 .Net 允许您指定 NULL 填充(我认为),另一个选择是在 PHP 中实现 PKCS#7 填充,这并不难做到。Most likely it's a padding issue... Please see here or here for more information.
EDIT after OP comment:
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.经过长时间的测试,我认为这种加密方法适合测试:
我在 iOS 上得到了这个:
v+cB4woDYANTozUbOgxJ4rWKb59EfLf6NkRE/Ee0kYY=
但如果我尝试解密(见上文),我会得到(null);
另一方面,如果我在 iOS 上加密,我会得到这个:
UUfn34iyNlSK40VaehloaQ==
肯定是短的(或者另一个是长的)...再次搜索错误。
After long test's I think this encrypt method will be right for tests:
I got this on iOS:
v+cB4woDYANTozUbOgxJ4rWKb59EfLf6NkRE/Ee0kYY=
But if I try to decrypt (see above), I got (null)
On the Other if I encrypt on iOS, I got this one:
UUfn34iyNlSK40VaehloaQ==
definitely to short (or the other is to long)...searching again for errors.