title: Openssl giving unpadblock:bad decrypt error
body: <p>I get this error while trying to decrypt in c++ what I encrypt in php, but it happens only sometimes:</p>
  
  <pre><code>B8:48:00:00:error:Provider routines:unpadblock:bad decrypt:providers\implementations\ciphers\ciphercommon_block.c:104:
  </code></pre>
  
  <p>I use php server to encrypt:</p>
  
  <pre class="lang-php prettyprint-override"><code>$iv = generateRandomString(16); // 16char random -&gt; send as first 16chars in message
  $key = getKey(); // 32char
  
  $data_en = openssl_encrypt($data, "AES-256-CBC", $key, 0, $iv);
  
  echo $iv . $data_en;
  </code></pre>
  
  <p>This is my c++ client code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int decrypt(unsigned char* ciphertext, int ciphertext_len, unsigned char* key,
      unsigned char* iv, unsigned char* plaintext)
  {
      EVP_CIPHER_CTX* ctx;
  
      int len;
  
      int plaintext_len;
  
      /* Create and initialise the context */
      if (!(ctx = EVP_CIPHER_CTX_new()))
          handleErrors();
  
      /*
       * Initialise the decryption operation. IMPORTANT - ensure you use a key
       * and IV size appropriate for your cipher
       * In this example we are using 256 bit AES (i.e. a 256 bit key). The
       * IV size for *most* modes is the same as the block size. For AES this
       * is 128 bits
       */
      if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
          handleErrors();
  
      /*
       * Provide the message to be decrypted, and obtain the plaintext output.
       * EVP_DecryptUpdate can be called multiple times if necessary.
       */
      if (1 != EVP_DecryptUpdate(ctx, plaintext, &amp;len, ciphertext, ciphertext_len))
          handleErrors();
      plaintext_len = len;
  
      /*
       * Finalise the decryption. Further plaintext bytes may be written at
       * this stage.
       */
      if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &amp;len))
          handleErrors();
      plaintext_len += len;
  
      /* Clean up */
      EVP_CIPHER_CTX_free(ctx);
  
      return plaintext_len;
  }
  </code></pre>
  
  <p>The most weird thing about this it gives the error once in a time.</p>