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: