title: Blowfish Encrypt and Decrypt char buffers
body: <p>i'm try to get a bowfish function working for simple char buffers. The program fails when I try to decrypt an encrypted buffer and fails on the EVP_CipherFinal_ex() call.</p>
  
  <pre><code>#include &lt;string.h&gt; 
  #include &lt;openssl/evp.h&gt; 
  #include &lt;openssl/buffer.h&gt; 
  #include &lt;openssl/blowfish.h&gt;
  #include &lt;openssl/evp.h&gt;
  
  int do_crypt(unsigned char *inbuf, int inlen, unsigned char *outbuf, int *outlen, int do_encrypt) { 
       outbuf=(unsigned char*) malloc(inlen+EVP_MAX_BLOCK_LENGTH); 
       int tmplen=0; 
       unsigned char key[] = "0123456789"; 
       unsigned char iv[] = "12345678"; 
  
       EVP_CIPHER_CTX ctx; 
       EVP_CIPHER_CTX_init(&amp;ctx); 
       EVP_CipherInit_ex(&amp;ctx, EVP_bf_cbc(), NULL, NULL, NULL, do_encrypt); 
       EVP_CIPHER_CTX_set_key_length(&amp;ctx, 10); 
       EVP_CipherInit_ex(&amp;ctx, NULL, NULL, key, iv, do_encrypt); 
  
       if(!EVP_CipherUpdate(&amp;ctx, outbuf, outlen, inbuf, inlen)) { 
           /* Error */ 
           printf("* update failed *\n"); 
           EVP_CIPHER_CTX_cleanup(&amp;ctx); 
           return 0; 
       } 
  
       int db=*outlen; 
  
       if(!EVP_CipherFinal_ex(&amp;ctx, outbuf+db, &amp;tmplen)) { 
           /* Error */ 
            ERR_print_errors_fp(stderr); 
           printf("* finalise failed *\n"); 
           EVP_CIPHER_CTX_cleanup(&amp;ctx); 
           return 0; 
       } 
  
  
       (*outlen)=db+tmplen; 
  
       EVP_CIPHER_CTX_cleanup(&amp;ctx); 
  
       return 1; 
  } 
  int main(int argc, char **argv) { 
       char *plain="ENCRYPT DECRYPT this string"; 
       int plain_len=strlen(plain); 
       unsigned char *cipher; 
       int cipher_len; 
  
       printf("***** ENCRYPT *****\n"); 
       if (!do_crypt((unsigned char*) plain, strlen(plain), cipher, &amp;cipher_len, 1)) { 
           printf("failed to encrypt\n"); 
           return 1; 
       } 
  
       char *decrypt; 
       int decrypt_len; 
       printf("***** DECRYPT *****\n"); 
       if(!do_crypt(  cipher ,cipher_len , decrypt, &amp;decrypt_len, 0)) { 
           printf("failed to decrypt\n"); 
           return 1; 
       }
   printf("decrypt=\"%s\"\n",decrypt); 
   printf("decrypt_len=%d\n",decrypt_len); 
       return 0; 
  } 
  </code></pre>
  
  <p>any help would be much appreciated. </p>