简述

关于 OpenSSL 的介绍及安装请参见:Windows下编译OpenSSL

下面主要介绍有关 OpenSSL 使用 base64 编码/解码。

编码/解码

#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <string>
#include <iostream> using namespace std; char * base64Encode(const char *buffer, int length, bool newLine);
char * base64Decode(char *input, int length, bool newLine); int main(int argc, char* argv[])
{
bool newLine = false;
string input = "Hello World!"; char * encode = base64Encode(input.c_str(), input.length(), newLine);
char * decode = base64Decode(encode, strlen(encode), newLine); cout << "Base64 Encoded : " << encode << endl;
cout << "Base64 Decoded : " << decode << endl; cin.get();
} // base64 编码
char * base64Encode(const char *buffer, int length, bool newLine)
{
BIO *bmem = NULL;
BIO *b64 = NULL;
BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64());
if (!newLine) {
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, buffer, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
BIO_set_close(b64, BIO_NOCLOSE); char *buff = (char *)malloc(bptr->length + 1);
memcpy(buff, bptr->data, bptr->length);
buff[bptr->length] = 0;
BIO_free_all(b64); return buff;
} // base64 解码
char * base64Decode(char *input, int length, bool newLine)
{
BIO *b64 = NULL;
BIO *bmem = NULL;
char *buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
if (!newLine) {
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem); return buffer;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

输出如下所示:

Base64 Encoded : SGVsbG8gV29ybGQh 
Base64 Decoded : Hello World!

更多参考

http://blog.csdn.net/u011012932/article/details/52948306

最新文章

  1. Beta Daily Scrum 第七天
  2. bzoj 1191: [HNOI2006]超级英雄Hero
  3. 我的一个javascript项目的重构历程
  4. screen命令学习
  5. Sql Server Analysis Service 转换为UnknownMember的正确设置
  6. java程序 输入10个数字并求和
  7. C# BackgroundWorker的使用 转
  8. 关于C#迭代器
  9. 小菜鸟带着梦想学chromium
  10. CMDB反思1
  11. cubie两种固定MAC地址的方法
  12. UEditor使用------图片上传与springMVC集成 完整实例
  13. 【Java学习笔记之三十】详解Java单例(Singleton)模式
  14. 【C#】发票助手二维码生成
  15. HDU2824-The Euler function-筛选法求欧拉函数+求和
  16. Oracle 启动监听命令
  17. Reboot-less node fencing in Oracle Clusterware 11g Release 2
  18. Spring学习手札(四)谈谈Spring Bean的生命周期及作用域
  19. datawindow自动换行打印,需结合该函数一起使用
  20. JavaScript继承方式

热门文章

  1. .gitignore无效,不能过滤某些文件
  2. [网络配置相关]——ifconfig命令、ip命令、route命令
  3. why does angular js rock
  4. 用MSBuild和Jenkins搭建持续集成环境(2)
  5. Github的使用以及Git的简单入门 - 课程作业三
  6. github -- fork提交项目
  7. Careercup - Microsoft面试题 - 5752271719628800
  8. 搬瓦工VPS搭建PPTP VPN
  9. 【环境】openSUSE安装记录 - 古董本上的windows 7和opensuse双系统
  10. 4.C#基础篇--&gt;变量