PHP 加密与 iOS 和 .NET 的区别

发布于 2024-10-06 17:41:44 字数 665 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-10-13 17:41:44

很可能是填充问题...请参阅此处 或此处了解更多信息。

OP 注释后编辑

除了 NULL 填充之外,PHP 没有内置支持其他填充模式。至少 .Net 允许您指定 NULL 填充(我认为),另一个选择是在 PHP 中实现 PKCS#7 填充,这并不难做到。

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

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

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.

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);
聚集的泪 2024-10-13 17:41:44

经过长时间的测试,我认为这种加密方法适合测试:

function mc_encrypt($str = "Affe", $key = "12345678901234567890123456789012")
{
    $str = "Affe";
  $block = mcrypt_get_block_size('rijndael-256', 'cbc');
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);

    $encoded =  base64_encode(mcrypt_encrypt('rijndael-256', $key, $str, 'cbc',$key));
    file_put_contents("test.txt",$encoded);
    return $encoded;
}

我在 iOS 上得到了这个:
v+cB4woDYANTozUbOgxJ4rWKb59EfLf6NkRE/Ee0kYY=
但如果我尝试解密(见上文),我会得到(null);

另一方面,如果我在 iOS 上加密,我会得到这个:
UUfn34iyNlSK40VaehloaQ==

肯定是短的(或者另一个是长的)...再次搜索错误。

After long test's I think this encrypt method will be right for tests:

function mc_encrypt($str = "Affe", $key = "12345678901234567890123456789012")
{
    $str = "Affe";
  $block = mcrypt_get_block_size('rijndael-256', 'cbc');
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);

    $encoded =  base64_encode(mcrypt_encrypt('rijndael-256', $key, $str, 'cbc',$key));
    file_put_contents("test.txt",$encoded);
    return $encoded;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文