-1

After a long time searching for the best way to en/decrypt data between PHP and iOS, I'm now asking you for a function in PHP.

I'm using this framework (https://gist.github.com/2507415) in Objective-C and my code is:

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

Output: UUfn34iyNlSK40VaehloaQ==

I've tried so much in PHP but nothing works. I hope somebody knows how to decrypt this server-side.

kurtanamo
  • 1,808
  • 22
  • 27

3 Answers3

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.

Christopher Will
  • 2,991
  • 3
  • 29
  • 46
0

Seem like PHP not support AES 256, http://www.php.net/manual/en/function.hash-algos.php.

I have found implementations using Rijndael-256 but I'm not sure if it works properly:

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

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

m4t1t0
  • 5,669
  • 3
  • 22
  • 30
0

That seems to be using AES 128, with no IV in ECB mode and PKCS-7 compatible padding, try this:

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: I had forgotten about the base64 encoding, it's fixed now.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500