vs2010 c++ 使用BIO_f_base64() 函数编码解码函数

该博客演示了如何在C++中使用openssl库的BIO_f_base64函数进行Base64编码和解码。通过创建BIO对象并设置相关标志,实现了字符串的编码和解码操作,并展示了代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//// test_rsa_03.cpp : 定义控制台应用程序的入口点。
////
//
#include "stdafx.h"
//
//
//int _tmain(int argc, _TCHAR* argv[])
//{
// return 0;
//}

#include <openssl/evp.h> 
#include <openssl/bio.h> 
#include <openssl/buffer.h> 
#include <string.h> 
#include <string> 
#include <iostream> 

using namespace std;

char * Base64Encode(const char* input, int length, bool with_new_line);
char * Base64Decode(char* input, int length, bool with_new_line);

//int main(int argc, char* argv[])
//{
//cout << "With new line? y/n ";
//string option;
//cin >> option;
//bool with_new_line = ( ("y" == option || "Y" == option) ? true : false );
//
//string enc_input = "Henry Alfred Kissinger is a German-born American writer, political scientist, diplomat, and businessman. A recipient of the Nobel Peace Prize, he served as National Security Advisor and later concurrently as Secretary of State in the administrations of Presidents Richard Nixon and Gerald Ford.";
//
//cout << endl << "To be encoded:" << endl << "~" << enc_input << "~" << endl << endl;
//
//char * enc_output = Base64Encode(enc_input.c_str(), enc_input.length(), with_new_line);
// cout << "Base64 Encoded:" << endl << "~" << enc_output << "~" << endl << endl;
//
//string dec_input = enc_output;
//char * dec_output = Base64Decode((char *)dec_input.c_str(), dec_input.length(), with_new_line);
// cout << "Base64 Decoded:" << endl << "~" << dec_output << "~" << endl << endl;
//
//free(enc_output);
//free(dec_output);
//
//cin.get();
//
//}
//

 


int main(int argc, char* argv[])
{
//cout << "With new line? y/n ";
//string option;
//cin >> option;
//bool with_new_line = ( ("y" == option || "Y" == option) ? true : false );

//string enc_input = "Henry Alfred Kissinger is a German-born American writer, political scientist, diplomat, and businessman. A recipient of the Nobel Peace Prize, he served as National Security Advisor and later concurrently as Secretary of State in the administrations of Presidents Richard Nixon and Gerald Ford.";
//
//cout << endl << "To be encoded:" << endl << "~" << enc_input << "~" << endl << endl;
//
//char * enc_output = Base64Encode(enc_input.c_str(), enc_input.length(), with_new_line);
// cout << "Base64 Encoded:" << endl << "~" << enc_output << "~" << endl << endl;
//
//string dec_input = enc_output;
//char * dec_output = Base64Decode((char *)dec_input.c_str(), dec_input.length(), with_new_line);
// cout << "Base64 Decoded:" << endl << "~" << dec_output << "~" << endl << endl;
//
//free(enc_output);
//free(dec_output);
//
//cin.get();
bool with_new_line=true;
string enc_input = "Henry Alfred Kissinger is a German-born American writer, political scientist, diplomat, and businessman. A recipient of the Nobel Peace Prize, he served as National Security Advisor and later concurrently as Secretary of State in the administrations of Presidents Richard Nixon and Gerald Ford.";
char * enc_output = Base64Encode(enc_input.c_str(), enc_input.length(), with_new_line);
cout << "Base64 Encoded:" << endl << "~" << enc_output << "~" << endl << endl;
std::cin.get();
}

 

char * Base64Encode(const char * input, int length, bool with_new_line)
{
BIO * bmem = NULL;
BIO * b64 = NULL;
BUF_MEM * bptr = NULL;

b64 = BIO_new(BIO_f_base64());
if(!with_new_line) {
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);

char * buff = (char *)malloc(bptr->length + 1);
memcpy(buff, bptr->data, bptr->length);
buff[bptr->length] = 0;

BIO_free_all(b64);

return buff;
}

char * Base64Decode(char * input, int length, bool with_new_line)
{
BIO * b64 = NULL;
BIO * bmem = NULL;
char * buffer = (char *)malloc(length);
memset(buffer, 0, length);

b64 = BIO_new(BIO_f_base64());
if(!with_new_line) {
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);

BIO_free_all(bmem);

return buffer;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值