千家信息网

基于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的加解密库

    1. 使用动态内存分

      /************************************************ @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加解密是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0