Base64是一种二进制到文本的编码方案,用于将二进制数据转换为ASCII
字符串格式。它通过将二进制数据流转换为一系列64
个字符来工作,这些字符都可以安全地传输到设计用于处理文本数据的系统中。
如下代码中我们使用Boost中提供的base64_from_binary
头文件实现两个函数,其中Base64Decode
函数接收一个字符串并对其进行解压缩操作输出解密后的原始字符串内容,其次Base64Encode
函数用于将一个原始数据包压缩处理,有了这两个函数的支持,我们只需要在调用发送函数之前对数据进行压缩,在接收数据后在使用对等的函数对其进行解压缩即可,如下是该案例的完整实现。
服务端代码如下所示
#include <iostream> #include <boost/asio.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp> #include <boost/archive/iterators/binary_from_base64.hpp> #include <boost/archive/iterators/transform_width.hpp>
using namespace std; using namespace boost::asio; using namespace boost::archive::iterators;
bool Base64Decode(const string & input, string * output) { typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator; stringstream result; try { copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result)); } catch (...) { return false; } *output = result.str(); return output->empty() == false; }
int main(int argc, char* argv[]) { io_service io_service; ip::tcp::acceptor acceptor(io_service, ip::tcp::endpoint(ip::tcp::v4(), 6666)); ip::tcp::socket socket(io_service);
acceptor.accept(socket); boost::system::error_code error_code;
char recv_buffer[8196] = { 0 }; socket.read_some(boost::asio::buffer(recv_buffer, 8196), error_code); std::cout << "传输Base64: " << recv_buffer << std::endl;
string output_str; Base64Decode(recv_buffer, &output_str); std::cout << "解码后: " << output_str << std::endl;
std::system("pause"); return 0; }
|
客户端代码如下所示
#include <iostream> #include <string> #include <boost/asio.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp> #include <boost/archive/iterators/binary_from_base64.hpp> #include <boost/archive/iterators/transform_width.hpp>
using namespace std; using namespace boost::asio; using namespace boost::archive::iterators;
bool Base64Encode(const string & input, string * output) { typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator; stringstream result; try { copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result)); } catch (...) { return false; } size_t equal_count = (3 - input.length() % 3) % 3; for (size_t i = 0; i < equal_count; i++) { result.put('='); } *output = result.str(); return output->empty() == false; }
int main(int argc, char* argv[]) { io_service io_service; ip::tcp::socket socket(io_service); ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 6666);
boost::system::error_code error_code; socket.connect(ep, error_code);
char send_buffer[4096] = { 0 };
strcpy(send_buffer, "hello lyshark");
string base64_str; bool ref = Base64Encode(send_buffer, &base64_str);
socket.write_some(boost::asio::buffer(base64_str, sizeof(base64_str)));
std::system("pause"); return 0; }
|
运行上述代码片段,读者可看到传输字符串以及解密字符串数据,如下图所示;