基于openssl的base64加解密是怎样的
发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,今天就跟大家聊聊有关基于openssl的base64加解密是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1.什么是base64base
千家信息网最后更新 2024年10月18日基于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安全错误
数据库的锁怎样保障安全
护苗 网络安全课程之五
双滦网络安全大队李翔
文件怎么存储在云服务器
以网络安全为主题论文
网络安全监督管理规定
网络安全大赛演讲
网络安全考试浙江2021
麻城警方网络安全宣传
三国志3赛季是几个服务器整合
服务器 立讯
计算机网络技术未来发展理解
gis软件开发 是什么
有域名和服务器怎么建会员制网站
浙江标准网络安全工程介绍
阿里云服务器密码忘记
国内外大型数据库网站
部队网络安全政治教育
软件开发记录
飞驰数据库
网上书店管理系统 数据库
js 从网址取数据库
低电压用于软件开发
软件开发 小额纳税人
网络安全市场是什么
英雄联盟服务器ping
如何将服务器函数化
哇嘎网络服务器
网络安全靶场服务器
保定网络技术哪家强
数据库表查询前几条记录