溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于openssl的base64加解密是怎樣的

發(fā)布時(shí)間:2021-12-14 17:40:32 來(lái)源:億速云 閱讀:206 作者:柒染 欄目:互聯(lián)網(wǎng)科技

今天就跟大家聊聊有關(guān)基于openssl的base64加解密是怎樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.什么是base64 

base64指64個(gè)可打印字符,“A-Z” 、“a~z”、 “0~9” 、 “+” 、“/” 共64個(gè)。一般使用base64來(lái)表示二進(jìn)制數(shù)據(jù)(二進(jìn)制流的每個(gè)字節(jié)不可能全部是可見(jiàn)字符,所以就傳送不了,可以采用base64轉(zhuǎn)換后傳輸)。

2.使用openssl的庫(kù)封裝base64的加解密庫(kù)

    1. 使用動(dòng)態(tài)內(nèi)存分

      /***********************************************
      * @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編碼,輸出長(zhǎng)度為字符串的長(zhǎng)度,如果需要可以再使用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.    使用數(shù)組,報(bào)文相對(duì)穩(wěn)定,這樣降低系統(tǒng)開(kāi)銷(雖然可能微不足道)

      /***********************************************
      * @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 解碼,輸出數(shù)組的大小應(yīng)大于輸入字符串大小以保證有足夠空間
      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;
      }


       

看完上述內(nèi)容,你們對(duì)基于openssl的base64加解密是怎樣的有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI