使用动态内存分
/***********************************************
* @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 <string.h>
#include <stdio.h>
#include <stdbool.h>
#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 <string.h>
#include <stdio.h>
#include <stdbool.h>
#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;
}