473,857 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

base64 decode help needed

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_b ase64() ) ;
bio = BIO_new(BIO_s_m em()) ;

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

bio = BIO_push(b64, bio) ;
//i = BIO_ctrl_pendin g(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(bi o) ;
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(decodebu f),
decodebuf ) ;
decodebio( filebuf, decodebuf, sizeof(decodebu f) ) ;
//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
3 3793
On 21 May, 18:22, d-fan <rafel.co...@pf shouston.comwro te:
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_b ase64() ) ;
bio = BIO_new(BIO_s_m em()) ;

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

bio = BIO_push(b64, bio) ;
//i = BIO_ctrl_pendin g(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(bi o) ;
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(decodebu f),
decodebuf ) ;
decodebio( filebuf, decodebuf, sizeof(decodebu f) ) ;
//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
On May 21, 12:52*pm, Spiros Bousbouras <spi...@gmail.c omwrote:
On 21 May, 18:22, d-fan <rafel.co...@pf shouston.comwro te:
void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
destbuf ) {
/* * * * ReadBase64encod ed 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_b ase64() ) ;
bio = BIO_new(BIO_s_m em()) ;
i=BIO_write(bio , encbuf, strlen(encbuf)) ;
bio = BIO_push(b64, bio) ;
//i = BIO_ctrl_pendin g(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(bi o) ;
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(decodebu f),
decodebuf ) ;
decodebio( filebuf, decodebuf, sizeof(decodebu f) ) ;
//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
On Wed, 21 May 2008 10:22:08 -0700 (PDT), d-fan
<ra*********@pf shouston.comwro te:
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.ne t
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8144
by: Oliver Kurz | last post by:
Hello, I have a problem by converting a string to base64 and back again. I have a string with german special chars like . This string i convert with base64str=base64.encode('') into a base64 representation. Backwards I got an error:
3
7687
by: Patrick | last post by:
Hi... I have a problem with Mime/base64 decoding. Maybe someone can help. I have an original Text, that I send using outlook. The text is: "This is a testmail " (without the ") So on the mailserver I open the message and the body is base64 encoded and contains VGhpcyBpcyBhIHRlc3RtYWlsIOQg/CD2IOkg4CDoDQo=
27
15083
by: gRizwan | last post by:
Hello all, We have a problem on a webpage. That page is sent some email data in base64 format. what we need to do is, decode the base64 data back to original shape and extract attached image from it. Any help will be highly appriciated. Thanks
4
5378
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back a decoded String. Any help is very much appreciated. Thanks Philip.
5
8620
by: robert.bull | last post by:
Hello - I am currently working on a project in classic ASP where I receive an XML file that contains an encoded base64 string that I need to do the following to: 1. Decode the base64 string for a binary write 2. Insert the decoded string into SQL Server for later binary writes I have searched the web but I cannot find anything that works. There are many decode functions available but I cannot insert the results
14
4364
by: BB | last post by:
Hello. i am trying to decode a block of (what i think is) base64 into text, and i have NO idea how to begin. I'm going to paste the whole string here, but i want to know the steps necessary to convert it to text. i've tried the usual binary/ascii techniques, but they just seem to loop back in to each other. what is the step by step to get from base64 to text? this is for an evil riddle i'm trying to solve. thanks. Becca. here's the...
3
18860
by: Kuldeep | last post by:
Hi All, Could you please give me some guidelines on dealing with Base64 encoded string. The actual purpose is to decode Base64 Encoded string and stream the data to a browser so that it can be viewed in a web application Regards, Kuldeep
1
4994
by: mirandacascade | last post by:
I am attempting to implement a process, and I'm pretty sure that a major roadblock is that I do not understand the nomenclature. The specs indicate that the goal is to calculate a message digest using an SHA-256 algorithm. There are 2 examples included with the specs. The label on the 2 examples are: 'HMAC samples'. In both examples, the message on which the digest is to be calculated is (the 33 chars within the quotes): 'This is a...
13
14804
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or send me the library file which helps that is really grateful. aruna.
0
9923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10807
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4190
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.