By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

base64 decode help needed

P: n/a
void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
destbuf ) {

/* Read Base64 encoded data from standard input and write the
decoded data to standard output: */

BIO *b64, *bio ;
long i ;
char buffer[512] ;
memset(buffer, 0, 512) ;

b64 = BIO_new(BIO_f_base64() ) ;
bio = BIO_new(BIO_s_mem()) ;

i=BIO_write(bio, encbuf, strlen(encbuf)) ;

bio = BIO_push(b64, bio) ;
//i = BIO_ctrl_pending(bio);
i = BIO_read(bio, decbuf, 1024) ;
printf( "the old buffer size is %d %d\n\r", i, strlen(decbuf)) ;

BIO_set_mem_eof_return(bio,0) ;
BIO_free_all(bio) ;
printf( "the resulting data is Length %d size %d text %s \n\r" ,
strlen(decbuf), sizeof(decbuf)) ;
}

int main(int argc, char **argv)
{
char *plain ;
unsigned char filebuf[1024] ;
unsigned char decodebuf[1024] ;
unsigned char *res ;
char *enc;
char *dec;
int len;
char *key1 = "The quick brown fox jumped over the speckled orange
hen." ;
char *key2 = "The quick brown fox jumped over the speckled orange
hen." ;

if (argc != 4)
{
printf("usage: %s plaintext key1 key2\n", argv[0]);
exit(1);
}

plain = argv[1];
len = strlen(plain);

strcpy( filebuf, "the cat in the hat\n") ;
strcat( filebuf, "\0") ;
printf( "after reading file size %d %s\n\r", strlen(filebuf),
filebuf ) ;

printf( "before decoding file size %d %s\n\r", sizeof(decodebuf),
decodebuf ) ;
decodebio( filebuf, decodebuf, sizeof(decodebuf) ) ;
//printf( "after decoding file size %d %s\n\r", strlen(res), res ) ;

I am using the code above to base64 decode a string of text. When I
run the code I get not data on the output. I attempt to decode the
literal "thecat in the hat" and I get an empty string. Is there
something wrong with my code?
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 21 May, 18:22, d-fan <rafel.co...@pfshouston.comwrote:
void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
destbuf ) {

/* Read Base64 encoded data from standard input and write the
decoded data to standard output: */

BIO *b64, *bio ;
long i ;
char buffer[512] ;
memset(buffer, 0, 512) ;

b64 = BIO_new(BIO_f_base64() ) ;
bio = BIO_new(BIO_s_mem()) ;

i=BIO_write(bio, encbuf, strlen(encbuf)) ;

bio = BIO_push(b64, bio) ;
//i = BIO_ctrl_pending(bio);
i = BIO_read(bio, decbuf, 1024) ;
printf( "the old buffer size is %d %d\n\r", i, strlen(decbuf)) ;

BIO_set_mem_eof_return(bio,0) ;
BIO_free_all(bio) ;
printf( "the resulting data is Length %d size %d text %s \n\r" ,
strlen(decbuf), sizeof(decbuf)) ;

}

int main(int argc, char **argv)
{
char *plain ;
unsigned char filebuf[1024] ;
unsigned char decodebuf[1024] ;
unsigned char *res ;
char *enc;
char *dec;
int len;
char *key1 = "The quick brown fox jumped over the speckled orange
hen." ;
char *key2 = "The quick brown fox jumped over the speckled orange
hen." ;

if (argc != 4)
{
printf("usage: %s plaintext key1 key2\n", argv[0]);
exit(1);
}

plain = argv[1];
len = strlen(plain);

strcpy( filebuf, "the cat in the hat\n") ;
strcat( filebuf, "\0") ;
printf( "after reading file size %d %s\n\r", strlen(filebuf),
filebuf ) ;

printf( "before decoding file size %d %s\n\r", sizeof(decodebuf),
decodebuf ) ;
decodebio( filebuf, decodebuf, sizeof(decodebuf) ) ;
//printf( "after decoding file size %d %s\n\r", strlen(res), res ) ;

I am using the code above to base64 decode a string of text. When I
run the code I get not data on the output. I attempt to decode the
literal "thecat in the hat" and I get an empty string. Is there
something wrong with my code?
There is at least one thing wrong with your code:
printf( "the resulting data is Length %d size %d text %s \n\r" ,
strlen(decbuf), sizeof(decbuf)) ;

The format string requires 3 arguments but you only
passs 2. Did you not get a warning from your compiler ?

Apart from that , your code uses several functions and
at least 1 datatype whose definitions you haven't showed
us so it's possible that one of those is broken.
Jun 27 '08 #2

P: n/a
On May 21, 12:52*pm, Spiros Bousbouras <spi...@gmail.comwrote:
On 21 May, 18:22, d-fan <rafel.co...@pfshouston.comwrote:
void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
destbuf ) {
/* * * * ReadBase64encoded data from standard input and write the
* * * *decoded data to standard output: */
BIO *b64, *bio ;
long i ;
char buffer[512] ;
memset(buffer, 0, 512) ;
b64 = BIO_new(BIO_f_base64() ) ;
bio = BIO_new(BIO_s_mem()) ;
i=BIO_write(bio, encbuf, strlen(encbuf)) ;
bio = BIO_push(b64, bio) ;
//i = BIO_ctrl_pending(bio);
i = BIO_read(bio, decbuf, 1024) ;
printf( "the old buffer size is %d %d\n\r", i, strlen(decbuf)) ;
BIO_set_mem_eof_return(bio,0) ;
BIO_free_all(bio) ;
printf( "the resulting data is Length %d *size %d text %s \n\r" ,
strlen(decbuf), sizeof(decbuf)) ;
}
int main(int argc, char **argv)
{
* char *plain ;
* unsigned char filebuf[1024] ;
* unsigned char decodebuf[1024] ;
* unsigned char *res ;
* char *enc;
* char *dec;
* int len;
* char *key1 = "The quick brown fox jumped over the speckled orange
hen." ;
* char *key2 = "The quick brown fox jumped over the speckled orange
hen." ;
* if (argc != 4)
* * {
* * * printf("usage: %s plaintext key1 key2\n", argv[0]);
* * * exit(1);
* * }
* plain = argv[1];
* len = strlen(plain);
strcpy( filebuf, "the cat in the hat\n") ;
strcat( filebuf, "\0") ;
printf( "after reading file size %d %s\n\r", strlen(filebuf),
filebuf ) ;
printf( "before decoding file size %d %s\n\r", sizeof(decodebuf),
decodebuf ) ;
decodebio( filebuf, decodebuf, sizeof(decodebuf) ) ;
//printf( "after decoding file size %d %s\n\r", strlen(res), res ) ;
I am using the code above tobase64decode a string of text. *When I
run the code I get not data on the output. *I attempt to decode the
literal "thecat in the hat" and I get an empty string. *Is there
something wrong with my code?

There is at least one thing wrong with your code:
printf( "the resulting data is Length %d *size %d text %s \n\r" ,
strlen(decbuf), sizeof(decbuf)) ;

The format string requires 3 arguments but you only
passs 2. Did you not get a warning from your compiler ?

Apart from that , your code uses several functions and
at least 1 datatype whose definitions you haven't showed
us so it's possible that one of those is broken.
I did correct the printf issues. The bio_read was returning a -1
value. I moved the "bio = BIO_push(b64, bio) ;" statement above the
BIO_write. Did I do any harm there or do you see any other reason why
I received a -1 return value?
Jun 27 '08 #3

P: n/a
On Wed, 21 May 2008 10:22:08 -0700 (PDT), d-fan
<ra*********@pfshouston.comwrote:
void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
destbuf ) {
<snip some OpenSSL stuff>
unsigned char filebuf[1024] ;
unsigned char decodebuf[1024] ;
<snip>
if (argc != 4)
{
printf("usage: %s plaintext key1 key2\n", argv[0]);
exit(1);
ObCLC: 1 as an exit value is not fully portable. EXIT_FAILURE from
stdlib.h is the only Standard failure value.
}

plain = argv[1];
len = strlen(plain);
Oddity: You don't actually use plain for anything. Nor do you even
look at the other two command-line arguments you demand the user
supply. I assume that is a result of playing with the code for
debugging, not intentional.
strcpy( filebuf, "the cat in the hat\n") ;
strcat( filebuf, "\0") ;
Useless: strcpy copies a string _with its null terminator_. A string
literal* already has a null terminator appended, so "\0" is actually
two zero bytes, but strcat() copies only the first, 'replacing' an
existing zero byte and thus accomplishing nothing. (* Pedantic: except
a string literal used as initializer for an exact-bound char array;
there the terminator isn't added. But that doesn't apply here.)
I am using the code above to base64 decode a string of text. When I
run the code I get not data on the output. I attempt to decode the
literal "thecat in the hat" and I get an empty string. Is there
something wrong with my code?
That string is not a valid b64 string, and so you're never going to
succeed in decoding it. If you do actually have "thecat" and not "the
cat" as in the code you posted, you could decode the first four chars
"thec" to the value bytes 0xB6 0x17 0x9C. Is that what you want?

The topic of this newsgroup is programming in standard/portable C, not
assuming any additional libraries like OpenSSL (or even an OS). If
your problem is with OpenSSL functionality, as opposed to generic C
programming methods used with it, you would be better to use the
openssl-users maillist, as described at http://openssl.org/support .

OTOH, b64 is so simple, you don't really need OpenSSL to do it. If you
want to write _just_ b64 decoding, encoding, etc., that could be
ontopic. As a rule, you need to explain your specific requirements,
but since b64 is so well known, and published in at least a few RFCs,
you can probably slide by on that one.

Where OpenSSL has a benefit is when you need b64 _plus_ its other
stuff. For example, a PKCS#12 file may be encrypted, ASN1-formatted,
AND b64/PEM-armored, and OpenSSL can handle all three: the first is
the most difficult and important; the second is moderately difficult;
and the third (b64/PEM) is pretty easy; but as long as you're using
OpenSSL for the first two it's convenient to do the third also.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.