Use OpenSSL for BASE64 encoding and decoding

Source: Internet
Author: User

Use OpenSSL for BASE64 encoding and decoding

OpenSSL can directly use commands to encode and decode the file in base64. OpenSSL provides APIs to achieve this.

Let's just talk about the code. Note that each 64-Byte Character After base64 encoding has a line break.

Static int base64_encode (char * str, int str_len, char * encode, int encode_len ){
BIO * bmem, * b64;
BUF_MEM * bptr;
B64 = BIO_new (BIO_f_base64 ());
Bmem = BIO_new (BIO_s_mem ());
B64 = BIO_push (b64, bmem );
BIO_write (b64, str, str_len); // encode
BIO_flush (b64 );
BIO_get_mem_ptr (b64, & bptr );
If (bptr-> length> encode_len ){
DPRINTF ("encode_len too small \ n ");
Return-1;
}
Encode_len = bptr-> length;
Memcpy (encode, bptr-> data, bptr-> length );
// Write (1, encode, bptr-> length );
BIO_free_all (b64 );
Return encode_len;
}

Static int base64_decode (char * str, int str_len, char * decode, int decode_buffer_len ){
Int len = 0;
BIO * b64, * bmem;
B64 = BIO_new (BIO_f_base64 ());
Bmem = BIO_new_mem_buf (str, str_len );
Bmem = BIO_push (b64, bmem );
Len = BIO_read (bmem, decode, str_len );
Decode [len] = 0;
BIO_free_all (bmem );
Return 0;
}

Provides FTP + SSL/TLS authentication through OpenSSL and implements secure data transmission.

Use OpenSSL to sign multi-domain certificates

OpenSSL details: click here
OpenSSL: click here

This article permanently updates the link address:

Related Article

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.