网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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


标题名称:基于openssl的base64加解密是怎样的
文章地址:http://bjjierui.cn/article/jggchp.html

其他资讯