OpenSSL 使用 base64 编码/解码
简述
关于 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
OpenSSL 使用 base64 编码/解码的更多相关文章
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- OpenSSL 使用 base64 编码/解码(liang19890820)
关于 OpenSSL 的介绍及安装请参见:Windows 下编译 OpenSSL 下面主要介绍有关 OpenSSL 使用 base64 编码/解码. 简述 编码解码 更多参考 编码/解码 #inclu ...
- Javascript中Base64编码解码的使用实例
Javascript为我们提供了一个简单的方法来实现字符串的Base64编码和解码,分别是window.btoa()函数和window.atob()函数. 1 var encodedStr = win ...
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net 1. Base64编码, 1 1.1. 子模式 urlsafe Or url ...
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net 1. Base64编码,1 1.1. 子模式 urlsafe Or url u ...
- delphi Base64编码/解码及数据压缩/解压知识
一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然还有第三方提供的单元或控件,其中我所接触到的认为比较好的有Indy的TIdMimeEncode / TIdMimeD ...
- Code:Base64 编码/解码
ylbtech-Code:Base64 编码/解码 1. C#返回顶部 1.编码 byte[] inArray = new byte[msgTxt.Length]; int x; ; x < m ...
- 原来浏览器原生支持JS Base64编码解码 outside of the Latin1 range
原来浏览器原生支持JS Base64编码解码 « 张鑫旭-鑫空间-鑫生活 https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-bto ...
- 王小胖之 Base64编码/解码
使用场景:编码网址作为URL参数,简单编码或加密数据,下载地址生成或解析. 实现功能:BASE64在线编码和解码. 数据实例:王小胖好啊,王小胖顶呱呱!! ~~ english 123 !@#$%^& ...
随机推荐
- windows phone和android,ios的touch事件兼容
1.开发背景 最近用html5写了个小游戏,中间踩过无数坑,有很多甚至百度都百度不到答案,可见html5还真是不成熟,兼容性的复杂度比ie6有过之而无不及,性能那个渣简直无力吐槽.. 好了,吐槽结束, ...
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- iOS网络传输Delegate不被触发的本质原因
NSURLSession一共有四种Delegate (文后附表有Session和SessionTask分类表格) NSURLSessionDelegate, NSURLSessionDownloadD ...
- HDU 5593 ZYB's Tree 树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5593 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
- Redis Master/Slave 实践
本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按 ...
- [工作积累] Google Play Game SDK details
https://developers.google.com/games/services/cpp/api/structgpg_1_1AndroidSupport For apps which targ ...
- The life of an HTML HTTP request
https://developer.mozilla.org/en-US/docs/The_life_of_an_HTML_HTTP_request http://www.html5rocks.com/ ...
- 15 个最佳的 jQuery 表格插件
现如今,网站开发设计的需求会要求自动适应所有移动设备,即响应式网站: 在开发网站时必须考虑对平板设备融合 fluid(流)和自适应性特点. 大多数网站设计要靠margins, guides, rows ...
- 定位position详解:relative与absolute
定位标签:position 包含属性:relative(相对) absolute(绝对) 1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在的位置上.然后通过设 ...
- 超快的 FastText
Word2Vec 作者.脸书科学家 Mikolov 文本分类新作 fastText:方法简单,号称并不需要深度学习那样几小时或者几天的训练时间,在普通 CPU 上最快几十秒就可以训练模型,得到不错的结 ...