Imagine I did the following:
Code:
$ echo a | mcrypt > b.nc
and typed test123 as the Password/KeyPhrase. I'm assuming mcrypt will use twofish and CBC by default.

I want to use libmcrypt and write a C program to decode the contents of b.nc.
I modified the example from libmcrypt/example.c at master * winlibs/libmcrypt * GitHub

Code:
#include<mcrypt.h>
#include<stdio.h>
#include<stdlib.h>

main(){

    MCRYPT td;
    int i;
    char*key;
    char password[20];
    char block_buffer;
    char*IV;
    int keysize=19;/* 128 bits */

    key=calloc(1, keysize);
    strcpy(password,"test123");

/* Generate the key using the password */
/*  mhash_keygen( KEYGEN_MCRYPT, MHASH_MD5, key, keysize, NULL, 0, password, strlen(password));
 */
    memmove( key, password, strlen(password));

    td = mcrypt_module_open("twofish", NULL,"cbc", NULL);
    if(td==MCRYPT_FAILED){
        return1;
    }
    IV = malloc(mcrypt_enc_get_iv_size(td));

/* Put random data in IV. Note these are not real random data,
 * consider using /dev/random or /dev/urandom.
 */

    /*  srand(time(0)); */
    for(i=0; i< mcrypt_enc_get_iv_size( td); i++){
        IV[i]=rand();
    }

    i=mcrypt_generic_init( td, key, keysize, IV);
    if(i<0){
        mcrypt_perror(i);
        return1;
    }

    /* Encryption in CFB is performed in bytes */
    while( fread (&block_buffer,1,1, stdin)==1){
        // mcrypt_generic (td, &block_buffer, 1);

/* Comment above and uncomment this to decrypt */
        mdecrypt_generic (td,&block_buffer,1);

        fwrite (&block_buffer,1,1, stdout);
    }
    mcrypt_generic_deinit(td);

    mcrypt_module_close(td);

    return0;

}

This builds into the executable mcrypt_dev. I tried to perform the decrypt by invoking

Code:
$ cat b.nc |./mcrypt_dev
but it just prints junk.

I suspect I must change the logic on the while loop that reads stdin, because it was originally written for CFB and I'm using CBC instead. Perhaps there's more to be done.
Any tips would be much appreciated.