title: Encrypt string with AES in Python and IOS
body: <p>I am trying to encrypt a string with AES in both IOS with CCCrypt and in Python with Crypto. However I seem to get different results. Anyone has any ideas why?</p>
  
  <p>I am trying to use 256 bit AES with null IV and CBC.</p>
  
  <p>Python code:</p>
  
  <pre><code>key = 'verysecretkey1111111111111111111'
  
  IV = 16 * '\x00'    
  mode = AES.MODE_CBC
  cipher = AES.new(key, AES.MODE_CBC, IV)
  
  y='aaaabbbbccccdddd'
  length = 16 - (len(y) % 16)
  y += chr(length)*length
  
  encoded = cipher.encrypt(y)
  print base64.b64encode(encoded)
  </code></pre>
  
  <p>The result is <strong>gyL9jv7bTgLz8xZQx/GLYNVnVrrwo6pLsc5Ew4Vl8Uk=</strong></p>
  
  <p>Objective C code</p>
  
  <pre><code>char keyPtr[kCCKeySizeAES256 ];
  bzero( keyPtr, sizeof( keyPtr ) );
  
  // fetch key data
  [key getCString:keyPtr maxLength:sizeof( keyPtr  encoding:NSUTF8StringEncoding];
  
  NSUInteger dataLength = [self length];
  
  
  size_t bufferSize = dataLength + kCCBlockSizeAES128;
  void *buffer = malloc( bufferSize );
  
  size_t numBytesEncrypted = 0;
  CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128,   kCCOptionPKCS7Padding,
                                        keyPtr, kCCKeySizeAES256,
                                        NULL /* initialization vector (optional) */,
                                        [self bytes], dataLength, /* input */
                                        buffer, bufferSize, /* output */
                                        &amp;numBytesEncrypted );
  </code></pre>
  
  <p>The result is <strong>DskVKDMGxFWLGSYszL/mEersVv9zq9PK9bPIV5UgLbs=</strong></p>