Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <openssl/bio.h>
- #include <openssl/evp.h>
- #include <stdlib.h>
- string base64_encode( const string &str ){
- BIO *base64_filter = BIO_new( BIO_f_base64() );
- BIO_set_flags( base64_filter, BIO_FLAGS_BASE64_NO_NL );
- BIO *bio = BIO_new( BIO_s_mem() );
- BIO_set_flags( bio, BIO_FLAGS_BASE64_NO_NL );
- bio = BIO_push( base64_filter, bio );
- BIO_write( bio, str.c_str(), str.length() );
- BIO_flush( bio );
- char *new_data;
- long bytes_written = BIO_get_mem_data( bio, &new_data );
- string result( new_data, bytes_written );
- BIO_free_all( bio );
- return result;
- }
- string base64_decode( const string &str ){
- BIO *bio, *base64_filter, *bio_out;
- char inbuf[512];
- int inlen;
- base64_filter = BIO_new( BIO_f_base64() );
- BIO_set_flags( base64_filter, BIO_FLAGS_BASE64_NO_NL );
- bio = BIO_new_mem_buf( (void*)str.c_str(), str.length() );
- bio = BIO_push( base64_filter, bio );
- bio_out = BIO_new( BIO_s_mem() );
- while( (inlen = BIO_read(bio, inbuf, 512)) > 0 ){
- BIO_write( bio_out, inbuf, inlen );
- }
- BIO_flush( bio_out );
- char *new_data;
- long bytes_written = BIO_get_mem_data( bio_out, &new_data );
- string result( new_data, bytes_written );
- BIO_free_all( bio );
- BIO_free_all( bio_out );
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement