基于openssl的base64加解密是怎样的
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,今天就跟大家聊聊有关基于openssl的base64加解密是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1.什么是base64base
千家信息网最后更新 2026年11月03日基于openssl的base64加解密是怎样的
今天就跟大家聊聊有关基于openssl的base64加解密是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
1.什么是base64
base64指64个可打印字符,"A-Z" 、"a~z"、 "0~9" 、 "+" 、"/" 共64个。一般使用base64来表示二进制数据(二进制流的每个字节不可能全部是可见字符,所以就传送不了,可以采用base64转换后传输)。
2.使用openssl的库封装base64的加解密库
使用动态内存分
/************************************************ @Filename: base64_1_by_openssl.c* @Author:edwin* @Description: ---* @Create: 2020-12-21 10:19:20* @Last Modified: 2020-12-21 10:31:39***********************************************/#include
#include #include #include "openssl/evp.h"#include "openssl/bio.h"#include "openssl/buffer.h"char * base64_encode(const char *input, int length, bool newLine);char * base64_decode(const char *input, int length, bool newLine,int *outLength);int main(int argc, char* argv[]){ bool newLine = false; char input[64] = "test string"; int outlength; char * encode = base64_encode(input, strlen(input), newLine); char * decode = base64_decode(encode, strlen(encode), newLine,&outlength); printf("base64 encode:%s\n",encode); printf("base64 decode:%s\n,output length:%d\n",decode,outlength); free(encode); free(decode); return 0;}// base64编码,输出长度为字符串的长度,如果需要可以再使用strlen获取char * base64_encode(const char *input, 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, input, 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 * base64_decode(const char *input, int length, bool newLine,int *outLength){ BIO *b64 = NULL; BIO *bmem = NULL; char *buffer = (char *)malloc(length); if(buffer == NULL){ return NULL; } 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); *outLength = BIO_read(bmem, buffer, length); BIO_free_all(bmem); return buffer;} 2. 使用数组,报文相对稳定,这样降低系统开销(虽然可能微不足道)
/************************************************ @Filename: base64_2_by_openssl.c* @Author:edwin* @Description: ---* @Create: 2020-12-21 10:19:20* @Last Modified: 2020-12-21 11:33:41***********************************************/#include
#include #include #include "openssl/evp.h"#include "openssl/bio.h"#include "openssl/buffer.h"char * base64_encode(const char *input, int inputLength, bool newLine,char *output,int outputMaxLength);char * base64_decode(const char *input, int inputLength, bool newLine,char *output,int outputMaxLength,int *outLength);int main(int argc, char* argv[]){ bool newLine = false; char input[64] = "Hello World!i\nsddsdds"; char output[64]; char * encode = base64_encode(input, strlen(input), newLine,output,64); printf("base64 encode:%s\n",encode); int outlength=0; char * decode = base64_decode(encode, strlen(encode), newLine,output,64,&outlength); printf("base64 decode:%s\n,output length:%d\n",output,outlength); return 0;}// base64 编码char * base64_encode(const char *input, int inputLength, bool newLine,char *output,int outputMaxLength){ 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, input, inputLength); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); BIO_set_close(b64, BIO_NOCLOSE); if(bptr->length >= outputMaxLength){ BIO_free_all(b64); return NULL; } memcpy(output, bptr->data, bptr->length); output[bptr->length] = '\0'; BIO_free_all(b64); return output;}// base64 解码,输出数组的大小应大于输入字符串大小以保证有足够空间char * base64_decode(const char *input, int inputLength, bool newLine,char *output,int outputMaxLength,int *outLength){ BIO *b64 = NULL; BIO *bmem = NULL; b64 = BIO_new(BIO_f_base64()); if (!newLine) { BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); } bmem = BIO_new_mem_buf(input, inputLength); bmem = BIO_push(b64, bmem); *outLength = BIO_read(bmem, output, inputLength); if(*outLength >= outputMaxLength){ BIO_free_all(bmem); return NULL; } output[*outLength] = '\0'; BIO_free_all(bmem); return output;}
看完上述内容,你们对基于openssl的base64加解密是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
字符
内容
二进制
大小
字符串
数组
编码
长度
输出
微不足道
内存
动态
字节
开销
报文
数据
更多
知识
空间
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手冲咖啡和咖啡机区别
互联网科技公司
百单网互联网科技有限公司
山东特亿宝互联网科技有限公司
科技创新与互联网经济
互联网科技开发公司
科技与互联网发展
万易互联网科技有限公司
中国互联网科技发展
极限挑战互联网科技
智慧科技互联网
互联网 视角下的科技创新
互联网科技汛
互联网巨头投资的硬核科技
互联网之后的科技是什么
港股科技和互联网
本届互联网博览会上的黑科技
江苏互联网智能科技软件
同城互联网络科技
北京天晋互联网科技有限公司
山东特亿宝互联网科技有限公司
互联网络科技有限公司
丰台科技园 互联网
万达互联网科技
互联网科技公司的市场部门
中国互联网有什么科技含量
互联网金融科技展
中科明德北京互联网科技
互联网科技大佬云集
优雇互联网科技有限公司