title: What is the equivalent of my openssl cmdline in C?
body: <p>I'm trying the implement the following openssl command line in C:  </p>
  
  <pre><code>openssl enc -aes-256-cbc -d -in /tmp/out_enc -K \
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
      -iv 0 -nopad -p
  </code></pre>
  
  <p>Output of the command line: </p>
  
  <pre><code>key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  iv =00000000000000000000000000000000
  &lt;CONNECTION_REQUEST ATS_ID="2" ATP_SEQ_ID="1" REGISTRATION_ID="Y2G5R52S8PP6YX47" SERIAL_NUMBER="724574802" SPC_PRODUCT_TITLE="SPC5300" SPC_FW_VERSION="3.8.5 - R.31629" ATS_NAME="Syst&amp;#232;me (ATS) 2" ATP1_ID="2" ATP1_UID="34" ATP1_NAME="Principal ATP 1" ATP1_COMMS_INTERFACE="1" ATP1_DEST="1, 192.168.1.62:52000" ATP1_CATEGORY="50"/&gt;�z
  </code></pre>
  
  <p>This is my equivalent in C: </p>
  
  <pre><code>long
  _ast_crypt_decrypt_generic(unsigned char* ciphertext, long cipherlen, unsigned char* plaintext, const EVP_CIPHER *cipher) {
      long result = 0;
  
       /* A 256 bit key */
  unsigned char *key = (unsigned char *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  
  /* A 128 bit IV */
  unsigned char *iv = (unsigned char *)"0000000000000000";
      EVP_CIPHER_CTX *ctx = NULL;
      int len;
      long plaintext_len;
  
      if (cipher) {
          /* Create and initialise the context */
          if (!(ctx = EVP_CIPHER_CTX_new())) {
              g_warning("AstCrypt : EVP_CIPHER_CTX_new failed");
              goto end;
          }
  
          if (1 != EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv)) {
              g_warning("AstCrypt : EVP_DecryptInit_ex");
          }
  
          EVP_CIPHER_CTX_set_padding(ctx, 0);
  
          if (1 != EVP_DecryptUpdate(ctx, plaintext, &amp;len, ciphertext, cipherlen)) {
              g_warning("AstCrypt : EVP_DecryptUpdate");
              goto end;
          }
          plaintext_len = len;
  
          if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &amp;len)) {
               g_warning("AstCrypt : EVP_DecryptFinal_ex failed");
               goto end;
          }
          plaintext_len += len;
  
          result= plaintext_len;
      } 
      else {
          g_warning("AstCrypt : Failed to get the openssl elements.");
      }
  
  end:
      if (ctx)
          EVP_CIPHER_CTX_free(ctx);
  
      return result;
  }
  
  long plainSize = _ast_crypt_decrypt_generic(headerData, headerLength, bPlain, EVP_aes_256_cbc());
   //fwrite(bPlain, plainSize, 1, stdout);
  </code></pre>
  
  <p>The previous code sample doesn't decrypt the data correctly (in C). </p>
  
  <p>Do you have any ideas what I am missing?</p>
  
  <p>It's probably a missing understanding of the key/iv format in the openssl lib. </p>