Note that there are some explanatory texts on larger screens.

plurals
  1. POUse AES to encrypt with Objective-C and decrypt with PHP
    primarykey
    data
    text
    <p>I want to use AES to encrypt a password in Objective-C, and then decrypt it in PHP, but I have two problems.</p> <ol> <li>I encrypt the password, but it's an <code>NSData</code> object, so I encode it with base64, but when I decode in PHP, the result is <code>nil</code>. So I can't decrypt it.</li> <li>I can encrypt and decrypt the password in Objective-C, so it is the PHP that is the problem, but when I encrypt with AES and then encode with base64, the results are not the same.</li> </ol> <p>Here is my code:</p> <p>PHP:</p> <pre><code> $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $key = "a16byteslongkey!"; $plaintext = "iphone"; $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB, $iv); $ciphertext = base64_encode($ciphertext); echo "ciphertext: ".$ciphertext."&lt;br/&gt;"; $ciphertext = base64_decode($ciphertext); $plaintext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext, MCRYPT_MODE_ECB, $iv); echo "plaintext: ".$plaintext."&lt;br/&gt;"; </code></pre> <p>output:</p> <pre><code> ciphertext: SXNepKfh0IrlDDdkq4EdmQ== plaintext: iphone </code></pre> <p>Objective-C: (Get the full source code here: <a href="https://gist.github.com/838614" rel="nofollow">https://gist.github.com/838614</a>)</p> <pre><code> NSString *key = @"a16byteslongkey!"; NSString *plaintext = @"iphone"; NSString *ciphertext = [plaintext AES256EncryptWithKey: key]; NSLog(@"ciphertext: %@", ciphertext); plaintext = [ciphertext AES256DecryptWithKey: key]; NSLog(@"plaintext: %@", plaintext); </code></pre> <p>output:</p> <pre><code> ciphertext: D19l3gsgXJlrLl7B2oCT6g== plaintext: iphone </code></pre> <p>i replace kCCKeySizeAES256 with kCCKeySizeAES128, and replace "kCCOptionPKCS7Padding" with "kCCOptionPKCS7Padding | kCCOptionECBMode",</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload