-1

经过很长时间寻找在 PHP 和 iOS 之间加密/解密数据的最佳方法后,我现在请您提供 PHP 中的函数。

我在 Objective-C 中使用这个框架( https://gist.github.com/2507415 ),我的代码是:

NSString *string= @"Affe";
NSString *key = @"12345678901234567890123456789012";
NSLog(%@,[string AES256EnryptWithKey:key];

输出:UUfn34iyNlSK40VaehloaQ==

我在 PHP 中尝试了很多,但没有任何效果。我希望有人知道如何解密这个服务器端。

4

3 回答 3

2

I dont know much about IOS, but I've implemented same logic recently between a PHP and Java API. I needed to encrypt the communication between an android device and a PHP backend.

I wrote a small summary, maybe the PHP part may help you out.

http://blog.cwill-dev.com/2012/10/09/encryption-between-javaandroid-and-php/

You should have a look at the mcrypt library, as Louis already mentioned.

于 2012-10-09T11:20:19.310 回答
0

好像 PHP 不支持 AES 256,http://www.php.net/manual/en/function.hash-algos.php

我找到了使用 Rijndael-256 的实现,但我不确定它是否正常工作:

http://kix.in/2008/07/22/aes-256-using-php-mcrypt/

http://snipperize.todayclose.com/snippet/php/Encrypt-and-Decrypt-AES-256--17234/

于 2012-10-09T11:14:37.713 回答
0

这似乎是使用 AES 128,在 ECB 模式下没有 IV 和 PKCS-7 兼容填充,试试这个

function encrypt($str, $key)
{
    $block = mcrypt_get_block_size('rijndael-128', 'ecb');
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);

    return base64_encode(mcrypt_encrypt('rijndael-128', $key, $str, 'ecb'));
}

function decrypt($str, $key)
{   
    $str = mcrypt_decrypt('rijndael-128', $key, base64_decode($str), 'ecb');

    $block = mcrypt_get_block_size('rijndael-128', 'ecb');
    $pad = ord($str[($len = strlen($str)) - 1]);
    return substr($str, 0, strlen($str) - $pad);
}

PS:我忘记了base64编码,现在已经修复了。

于 2012-10-09T11:30:57.630 回答