【OpenSSL】base64 with BIO filter
2016-04-28 09:18
375 查看
Introduction
There are many ways to do base64 encoding/decoding in OpenSSL, Here are some demos code with BIO filter. Other ways, e.g. EVP_* will be introduced later Base64 with EVP.Encode
std::string CX509Cert::b64enc(const std::string & str) { BIO* out = BIO_new(BIO_s_mem());// sink to memory. if (!out){ return ""; } BIO * b64 = BIO_new(BIO_f_base64()); if (!b64){ BIO_free(out); return ""; } out = BIO_push(b64, out); BIO_write(out, (char *)str.c_str(), str.length()); BIO_flush(out); BUF_MEM * bufptr = NULL; BIO_get_mem_ptr(out, &bufptr); std::string b64str = ""; b64str.append((char *)bufptr->data, bufptr->length); BIO_free(b64); return b64str; }
Decode
std::string CX509Cert::b64dec(const std::string & b64str) { BIO * in = BIO_new_mem_buf((void*)b64str.c_str(), b64str.length()); if (!in) return ""; BIO * b64 = BIO_new(BIO_f_base64()); if (!b64){ BIO_free(in); return ""; } in = BIO_push(b64, in); char buf [16] = ""; int bufsz = sizeof(buf); std::string str = ""; for(;;){ int cb = BIO_read(in, buf, bufsz); if (cb <= 0){ break; } str.append(buf, cb); } BIO_free(b64); return str; }
Note
BIO_free(b64) will also free in/out BIOs, so, remeber do NOT double free them.相关文章推荐
- python, linux shell 使用expect模块自动输入密码
- getLocationInWindow getLocationOnScreen getLeft , getTop, getBottom,getRight
- OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
- 如何查看Linux操作系统版本
- Linux中的命令 make -f 是什么意思
- linux系统启动时更改MAC地址
- tomcat 远程调试配置
- 在linux下安装禅道
- 解决Serv-U服务停止的方法
- 真伪静态网站优缺点分析
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
- POSIX线程状态:Joinable和Detached
- windows系统下链接linux系统
- shell 函数返回值问题
- Linux每天学习一个命令之管道命令(pipe)
- Tomcat 在linux 服务器上的部署(32位debian)
- 昨天上架出现问题,you binary is not optimized for iphone5.。。。。
- Hadoop单机模式,并执行WordCount
- C# 调用 PowerShell
- CentOS 6.5环境实现corosync+pacemaker实现DRBD高可用