Search code examples
phpioscocoa-touchaesmcrypt

AES-128 UTF-8 characters in key (iOS ↔ PHP)


After a really long time of research I finally did encoding / decoding on iOS and PHP. I wrote a little algorithm that uses a pool of randomly created 16 Bytes keys both, on iOS and PHP. The algorithm keeps both systems synchronized so that I'm using the keys not multiple times.

However my keys contain some UTF8 Characters (I think). I'm using the standard [a-z][A-Z][0-9] characters including these special chars:

!\"§$%&/()=?+-*#.,£[]|{}

Unfortunately, when using one of these keys, the decryption fails on PHP. On iOS I'm using an extension on the stringByAddingPercentEscapes: method which escapes a bit more characters. Then I send the escaped data as POST variables to the server.

I played around a bit and it turned out that using only [a-z][A-Z][0-9] works great.

Any suggestions on solving my issue?


Solution

  • Of the characters you described, £ and § are not ASCII characters. Depending on how you are transmitting them, those two are probably being corrupted.

    That being said — encryption keys are data, not strings. If you represent your encryption keys as NSData, rather than NSString, character sets will cease to be an issue, and you should be able to use any randomly generated key, not just ones consisting of these 85 characters.