Last active
          August 29, 2015 14:10 
        
      - 
      
 - 
        
Save wyyqyl/a28b4c2dd632dd393967 to your computer and use it in GitHub Desktop.  
    Base64 Encoding and Decoding
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | std::string base64_encode(const std::string& in) { | |
| BUF_MEM *bptr; | |
| BIO* b64 = BIO_new(BIO_f_base64()); | |
| BIO* bio = BIO_new(BIO_s_mem()); | |
| bio = BIO_push(b64, bio); | |
| BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); | |
| BIO_write(bio, in.c_str(), (int)in.size()); | |
| BIO_flush(bio); | |
| BIO_get_mem_ptr(bio, &bptr); | |
| std::string out(bptr->data, bptr->length); | |
| BIO_free_all(bio); | |
| return out; | |
| } | |
| std::string base64_decode(const std::string& in) { | |
| BIO* b64 = BIO_new(BIO_f_base64()); | |
| BIO* bio = BIO_new_mem_buf((void*)in.c_str(), in.size()); | |
| bio = BIO_push(b64, bio); | |
| BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); | |
| std::string out(in.size(), 0); | |
| int len = BIO_read(bio, (void*)out.c_str(), out.size()); | |
| out = out.substr(0, len); | |
| BIO_free_all(bio); | |
| return out; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment