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

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)海鹽免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
1.什么是base64
base64指64個可打印字符,“A-Z” 、“a~z”、 “0~9” 、 “+” 、“/” 共64個。一般使用base64來表示二進制數(shù)據(jù)(二進制流的每個字節(jié)不可能全部是可見字符,所以就傳送不了,可以采用base64轉(zhuǎn)換后傳輸)。
2.使用openssl的庫封裝base64的加解密庫
使用動態(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編碼,輸出長度為字符串的長度,如果需要可以再使用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ù)組,報文相對穩(wěn)定,這樣降低系統(tǒng)開銷(雖然可能微不足道)
/***********************************************
* @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)容,你們對基于openssl的base64加解密是怎樣的有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
名稱欄目:基于openssl的base64加解密是怎樣的
標(biāo)題來源:http://chinadenli.net/article48/jggchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化、移動網(wǎng)站建設(shè)、網(wǎng)站排名、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)