Advertisement
Guest User

aa

a guest
May 28th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <openssl/bio.h>
  2. #include <openssl/evp.h>
  3. #include <stdlib.h>
  4.  
  5. string base64_encode( const string &str ){
  6.  
  7.     BIO *base64_filter = BIO_new( BIO_f_base64() );
  8.     BIO_set_flags( base64_filter, BIO_FLAGS_BASE64_NO_NL );
  9.  
  10.     BIO *bio = BIO_new( BIO_s_mem() );
  11.     BIO_set_flags( bio, BIO_FLAGS_BASE64_NO_NL );
  12.  
  13.     bio = BIO_push( base64_filter, bio );
  14.  
  15.     BIO_write( bio, str.c_str(), str.length() );
  16.  
  17.     BIO_flush( bio );
  18.  
  19.     char *new_data;
  20.  
  21.     long bytes_written = BIO_get_mem_data( bio, &new_data );
  22.  
  23.     string result( new_data, bytes_written );
  24.     BIO_free_all( bio );
  25.  
  26.     return result;
  27.  
  28. }
  29.  
  30.  
  31.  
  32. string base64_decode( const string &str ){
  33.  
  34.     BIO *bio, *base64_filter, *bio_out;
  35.     char inbuf[512];
  36.     int inlen;
  37.     base64_filter = BIO_new( BIO_f_base64() );
  38.     BIO_set_flags( base64_filter, BIO_FLAGS_BASE64_NO_NL );
  39.  
  40.     bio = BIO_new_mem_buf( (void*)str.c_str(), str.length() );
  41.  
  42.     bio = BIO_push( base64_filter, bio );
  43.  
  44.     bio_out = BIO_new( BIO_s_mem() );
  45.  
  46.     while( (inlen = BIO_read(bio, inbuf, 512)) > 0 ){
  47.         BIO_write( bio_out, inbuf, inlen );
  48.     }
  49.  
  50.     BIO_flush( bio_out );
  51.  
  52.     char *new_data;
  53.     long bytes_written = BIO_get_mem_data( bio_out, &new_data );
  54.  
  55.     string result( new_data, bytes_written );
  56.  
  57.     BIO_free_all( bio );
  58.     BIO_free_all( bio_out );
  59.  
  60.     return result;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement