title: Encrypting with OpenSSL and CommonCrypto
body: <p>I'm trying to ultimately encrypt a file with OpenSSL and decrypt with Objective-c CommonCrypto, but before that works I need both approaches to encrypt the same way. </p>
  
  <p>This is what I have:</p>
  
  <p>String to encrypt: "This is the string"</p>
  
  <p>Key: "thisisthekey"</p>
  
  <p>OpenSSL:</p>
  
  <pre><code>openssl enc -aes256 -a -e -nosalt -in InputFileWithString.txt -out OutputFile.txt thisisthekey
  </code></pre>
  
  <p>Resulting base 64 encoded string from openSSL: <code>HncUM4ryxSR7Rdi7Z49HPl9veOPxkk3l8GYIgorBhbk=</code></p>
  
  <p>Objective-c CommonCrypto:</p>
  
  <pre><code>+(NSString *)encryptText:(NSString *)text withKey:(NSString *)key{
      NSData *plainData = [text dataUsingEncoding:NSUTF8StringEncoding];
  
      NSData *encryptedData = [plainData AES256EncryptWithKey:key];
  
      NSString *base64String = [encryptedData base64EncodedStringWithOptions:0];
  
      return base64String;
  }
  
  - (NSData*)AES256EncryptWithKey:(NSString*)key {
      char keyPtr[kCCKeySizeAES256 + 1]; 
      bzero(keyPtr, sizeof(keyPtr)); 
  
      [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,
                                            [self bytes], dataLength, 
                                            buffer, bufferSize,
                                            &amp;numBytesEncrypted);
  
      if (cryptStatus == kCCSuccess)
      {
          return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
      }
  
      free(buffer);
      return nil;
  }
  </code></pre>
  
  <p>The resulting base 64 encoded string: <code>gNCs4d0GAxZHRcOtu8RVpLgN0ONKk1r5XkJ4GtL7W2I=</code></p>
  
  <p>As you can see, each approach is producing a different encrypted string. Any ideas how to make these both produce the same string? </p>