base64 encode/decode

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

base64 encode/decode

Patrick Guio

Dear all,

I am trying to base64-encode/decode a string using the BIO API.
The man page for BIO_f_base64 provides examples to
"base64 encode a string and write the result to standard output" and
"read base64 encoded data from standard input and write the decoded data to
standard output" (http://www.openssl.org/docs/crypto/BIO_f_base64.html#)

I would like to base64-encode/decode a string (char*) directly into memory
(into a char* buffer) and not make use of stdin/stdout, nor any temporary
file. Is there an interface for this?

Any help on this would be appreciated.

Sincerely,

Patrick

PS:
I think there is a typo in the code snippet on the webpage
http://www.openssl.org/docs/crypto/BIO_f_base64.html#
The statement while((inlen = BIO_read(bio, inbuf, 512) > 0)
should read
while((inlen = BIO_read(bio, inbuf, 512)) > 0)

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: base64 encode/decode

Adam Jones-2
I had the same problem. It took me a few days to figure it all out. Here is
my sample code that works. Let me know if you have any questions. Hope this
helps.

void main()
{
        BIO *bmem, *b64, *bmem2, *b642;
        BUF_MEM *bptr;
        char inbuff[21];
        char outbuff[12];
        char message[] = "Hello World \n";
        int nLoop = 0;
        int inlen;


        cout << "Start Encoding" << endl;
        cout << "Messege to Base64 Encode - " << message << endl;
        b64 = BIO_new(BIO_f_base64());
        bmem = BIO_new(BIO_s_mem());
        b64 = BIO_push(b64, bmem);
        BIO_write(b64, message, strlen(message));
        BIO_flush(b64);
        BIO_get_mem_ptr(b64, &bptr);

        cout << "Base64 Encoded Message - ";
        for (nLoop = 0; nLoop< bptr->length; nLoop++)
        {
                cout << bptr->data[nLoop];
        }
        cout << endl;

        memcpy(inbuff, bptr->data, bptr->length);
        int nLength = bptr->length;
        BIO_free_all(b64);

        cout << "\nStart Decoding Base64" << endl;
        cout << "Base64 message to decode -" ;
        for (nLoop = 0; nLoop< nLength; nLoop++)
        {
                cout << inbuff[nLoop];
        }
        cout << endl;

        b642 = BIO_new(BIO_f_base64());
        bmem2 = BIO_new_mem_buf(inbuff,21);

        bmem2 = BIO_push(b642, bmem2);

        inlen = BIO_read(bmem2, &outbuff, 21);
 
        BIO_free_all(bmem2);

        cout << "\nBase64 Decoded message - ";
        for (nLoop = 0; nLoop< inlen; nLoop++)
        {
                cout << outbuff[nLoop];
        }
        cout << endl;
}

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Patrick Guio
Sent: Tuesday, November 01, 2005 3:46 AM
To: [hidden email]
Subject: base64 encode/decode


Dear all,

I am trying to base64-encode/decode a string using the BIO API.
The man page for BIO_f_base64 provides examples to
"base64 encode a string and write the result to standard output" and "read
base64 encoded data from standard input and write the decoded data to
standard output" (http://www.openssl.org/docs/crypto/BIO_f_base64.html#)

I would like to base64-encode/decode a string (char*) directly into memory
(into a char* buffer) and not make use of stdin/stdout, nor any temporary
file. Is there an interface for this?

Any help on this would be appreciated.

Sincerely,

Patrick

PS:
I think there is a typo in the code snippet on the webpage
http://www.openssl.org/docs/crypto/BIO_f_base64.html#
The statement while((inlen = BIO_read(bio, inbuf, 512) > 0) should read
while((inlen = BIO_read(bio, inbuf, 512)) > 0)

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]


Confidentiality Notice:

This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.  If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system.  If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: base64 encode/decode

Patrick Guio
On Tue, 1 Nov 2005, Adam Jones wrote:

> I had the same problem. It took me a few days to figure it all out. Here is
> my sample code that works. Let me know if you have any questions. Hope this
> helps.

Hi Adams,

Thank you very much indeed! I have changed the message and encoded/decoded
messages to be std::string if you are interesting.
Sincerely,
Patrick

int main()
{
   string message("Hello World \n");

   cout << "Start Encoding" << endl;
   cout << "Message to Base64 Encode - " << message << endl;

   BIO * b64  = BIO_new(BIO_f_base64());
   BIO * bmem = BIO_new(BIO_s_mem());
   b64 = BIO_push(b64, bmem);
   BIO_write(b64, message.c_str(), message.length());
   BIO_flush(b64);

   BUF_MEM *bptr;
   BIO_get_mem_ptr(b64, &bptr);

   message.clear();
   message.insert(0, bptr->data, bptr->length);
   cout << "Base64 Encoded Message   - " << message << endl;

   string b64message(message);

   BIO_free_all(b64);

   cout << "\nStart Decoding Base64" << endl;
   cout << "Base64 message to decode - " << b64message << endl ;

   BIO * b642  = BIO_new(BIO_f_base64());
   BIO * bmem2 = BIO_new_mem_buf(const_cast<char*>(b64message.c_str()), b64message.length());

   bmem2 = BIO_push(b642, bmem2);

   char obuf[512];
   int olen = BIO_read(bmem2, &obuf, 512);

   message.clear();
   message.insert(0, obuf, olen);

   BIO_free_all(bmem2);

   cout << "Base64 Decoded message   - " << message << endl;

   return 0;
}

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: base64 encode/decode

Adam Jones-2
Thanks!

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Patrick Guio
Sent: Tuesday, November 01, 2005 9:51 AM
To: Adam Jones
Cc: [hidden email]
Subject: RE: base64 encode/decode

On Tue, 1 Nov 2005, Adam Jones wrote:

> I had the same problem. It took me a few days to figure it all out.
> Here is my sample code that works. Let me know if you have any
> questions. Hope this helps.

Hi Adams,

Thank you very much indeed! I have changed the message and encoded/decoded
messages to be std::string if you are interesting.
Sincerely,
Patrick

int main()
{
   string message("Hello World \n");

   cout << "Start Encoding" << endl;
   cout << "Message to Base64 Encode - " << message << endl;

   BIO * b64  = BIO_new(BIO_f_base64());
   BIO * bmem = BIO_new(BIO_s_mem());
   b64 = BIO_push(b64, bmem);
   BIO_write(b64, message.c_str(), message.length());
   BIO_flush(b64);

   BUF_MEM *bptr;
   BIO_get_mem_ptr(b64, &bptr);

   message.clear();
   message.insert(0, bptr->data, bptr->length);
   cout << "Base64 Encoded Message   - " << message << endl;

   string b64message(message);

   BIO_free_all(b64);

   cout << "\nStart Decoding Base64" << endl;
   cout << "Base64 message to decode - " << b64message << endl ;

   BIO * b642  = BIO_new(BIO_f_base64());
   BIO * bmem2 = BIO_new_mem_buf(const_cast<char*>(b64message.c_str()),
b64message.length());

   bmem2 = BIO_push(b642, bmem2);

   char obuf[512];
   int olen = BIO_read(bmem2, &obuf, 512);

   message.clear();
   message.insert(0, obuf, olen);

   BIO_free_all(bmem2);

   cout << "Base64 Decoded message   - " << message << endl;

   return 0;
}

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]




Confidentiality Notice:

This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.  If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system.  If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: base64 encode/decode

Nils Larsch
In reply to this post by Patrick Guio
Patrick Guio wrote:
...
> I think there is a typo in the code snippet on the webpage
> http://www.openssl.org/docs/crypto/BIO_f_base64.html#
> The statement while((inlen = BIO_read(bio, inbuf, 512) > 0)
> should read
> while((inlen = BIO_read(bio, inbuf, 512)) > 0)

I've committed a fix for the underlying pod file

Thanks,
Nils
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]