I am using Openssl 0.96 and Openssl 0.97 with a goal to decode a
simple piece of data. The BIO_read always returns 0 and the resulting
string. Please review the sample code and see if you can figure out
what is wrong. Your help will be appreciated.

Please pardon the extra debug code.


#include
#include
#include
#include
#include

char *unbase64(unsigned char *input, int length) ;

void readfile( unsigned char * buf ) {
FILE * inf ;
char *op ;

/* inf = fopen( "rayencrypt.txt", "rb" ) ; */

// inf = fopen( "chilkat.bf", "rb" ) ;

// fread(buf, 1, 1024, inf) ;
// strcat( buf, "\0" ) ;
strcpy( buf,
"BAAADBFD631E7A6FB8ADC9F02E0F4D8471B517652F46AA0109 5D4976027766A877D90CCB98585292C95746D455FF355E");
strcat(buf, "\n\0");

op =
unbase64("BAAADBFD631E7A6FB8ADC9F02E0F4D8471B517652F46AA0109 5D4976027766A877D90CCB98585292C95746D455FF355E
\n\0",
strlen("BAAADBFD631E7A6FB8ADC9F02E0F4D8471B517652F46AA0109 5D4976027766A877D90CCB98585292C95746D455FF355E
\n\0"));

printf( "Bytes read %d\n\r", strlen(buf) ) ;

// fclose( inf ) ;
}
char *unbase64(unsigned char *input, int length)
{ BIO *b64, *bmem;
int ii ;
char *buffer =(char *) malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input, length) ;
bmem = BIO_push( b64, bmem);
ii = BIO_read(bmem, buffer, length);
printf( "size if I %d %d %s n\r", ii, length, input ) ;
BIO_free_all(bmem);
return buffer ;
}



int main(int argc, char **argv)
{
char *plain ;
unsigned char filebuf[1024] ;
char decodebuf[1024] ;
unsigned char *res ;
BF_KEY keystr[448] ;
char *enc;
char *nb;
char *dec;
int len;
int keylen ;

char *key1 = "The Password" ;
char *key2 =
"0123456789abcdefghijklmnopqrstuvwxyz9876543210ABCD EFGHIJ" ;

keylen = strlen(key2) ;
printf("before set\n\r") ;


printf("after set key\n\r") ;
memset(filebuf, 0, 1024) ;
memset(decodebuf, 0, 1024) ;

readfile( filebuf );

nb = unbase64( filebuf, strlen(filebuf));



len = strlen(nb ) ;

enc = malloc(len+1);
enc[len] = '\0';
dec = malloc(len+1);
dec[len] = '\0';



printf("key1: '%s'\n", key1);
printf("key2: '%s'\n", key2);
// printf("plaintext: '%s'\n", filebuf);
printf("decrypted: size %d '%s'\n", strlen(dec), dec);
free(nb);

}