BIO Series 21 of openssl --- Base64 BIO, openssl21 --- base64

Source: Internet
Author: User

BIO Series 21 of openssl --- Base64 BIO, openssl21 --- base64

Base64 Type BIO

--- Based on openssl doc \ crypto \ bio_f_base64.pod translation and your own understanding, write

(Author: DragonKing, Mail: wzhah@263.net, published on: http://gdwzh.126.com o

Penssl Professional Forum)

This type is filter Type BIO, which is defined as follows (openssl \ bio. h, openssl \ evp. h)

:

BIO_METHOD * BIO_f_base64 (void );

[BIO_f_base64]

This function returns a Base64 BIO_METHOD structure, which is defined as follows (evp \ bio_b64.c)

:

Static BIO_METHOD methods_b64 =

{

BIO_TYPE_BASE64,

"Base64 encoding ",

B64_write,

B64_read,

NULL,/* b64_puts ,*/

NULL,/* b64_gets ,*/

B64_ctrl,

B64_new,

B64_free,

B64_callback_ctrl,

};

It should be noted that this type of BIO is not defined in the bio directory, but in the evp directory.

When writing data to this BIO, the data is base64-encoded. When reading data from this BIO, the data is base64-decoded.

. This BIO does not support BIO_gets and BIO_puts.

BIO_flush indicates that the data to be written has been written when BIO of this type is called.

A piece of data is written into BIO.

[BIO_set_flags]

This function can be used to set the flag BIO_FLAGS_BASE64_NO_NL. After this flag is set, all data

Encode the data into a row or expect all the data to be on a row. Note that the base64 encoding format

The end position of the encoded data block cannot be determined accurately and reliably.

Pay attention to the Data Length.

[Example]

The following program encodes the string "Hello World \ n" in base64 and writes it to the standard output device.

BIO * bio, * b64;

Char message [] = "Hello World \ n ";

B64 = BIO_new (BIO_f_base64 ());

Bio = BIO_new_fp (stdout, BIO_NOCLOSE );

Bio = BIO_push (b64, bio );

BIO_write (bio, message, strlen (message ));

BIO_flush (bio );

BIO_free_all (bio );

The following program reads base64 encoded data from the standard input device and outputs the decoded data to the standard output.

Device:

BIO * bio, * b64, bio_out;

Char inbuf [512];

Int inlen;

Char message [] = "Hello World \ n ";

B64 = BIO_new (BIO_f_base64 ());

Bio = BIO_new_fp (stdin, BIO_NOCLOSE );

Bio_out = BIO_new_fp (stdout, BIO_NOCLOSE );

Bio = BIO_push (b64, bio );

While (inlen = BIO_read (bio, inbuf, strlen (message)> 0)

BIO_write (bio_out, inbuf, inlen );

BIO_free_all (bio );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.