使用Boost解码Base64字符串 [英] Decode Base64 String Using Boost

查看:439
本文介绍了使用Boost解码Base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用boost和以下代码将std::vector<char>编码为Base64:

I've been able to encode a std::vector<char> to Base64 using boost and the following code:

using namespace boost::archive::iterators;

std::string message(binary.begin(), binary.end());
std::stringstream os;
using base64_text = insert_linebreaks<base64_from_binary<transform_width<const char *, 6, 8>>, 72>;

std::copy(
    base64_text(message.c_str()),
    base64_text(message.c_str() + message.size()),
    ostream_iterator<char>(os)
);

return os.str();

我在Stackoverflow上发现了这个.好吧,现在我想倒退同一件事,放入Base64格式的std::string并以std::vector<char>结尾.但是我无法适应我的榜样来做相反的事情.我在网上找到了一些其他代码,这些代码可以很好地与Hello World示例配合使用,但是当实际的Base64更大时,其中还包含诸如反斜杠的关键字符,整个过程就会崩溃.

I found this on Stackoverflow. Well, now I want to to the same thing backwards, putting in a Base64 formatted std::string and end up with a std::vector<char>. But I can't adapt my example to do the thing in reverse. I found some other code online, which works nice with a Hello World example, but when there's an actual bigger Base64 which also contains some critical characters like backslashes, the whole thing crashes.

这就是我现在要解码的内容:

This is what I'm doing now to decode:

using namespace std;
using namespace boost::archive::iterators;

typedef
    transform_width<
        binary_from_base64<string::const_iterator>, 8, 6
        > binary_t;
string dec(binary_t(str.begin()), binary_t(str.end()));
return dec;

当我要创建字符串时,它在返回之前的最后一行中崩溃. 你知道这是怎么回事吗?

It crashes in the last line before the return, when I'm about to create the string. Do you see what's wrong with it?

推荐答案

base64要求输入和输出都分别填充为3和4的倍数.

base64 requires both input and output to be padded into multiples of 3 and 4 respectively.

这是使用boost解码base64的功能:

Here's a function for decoding base64 using boost:

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <algorithm>    

std::string decode(std::string input)
{
  using namespace boost::archive::iterators;
  typedef transform_width<binary_from_base64<remove_whitespace
      <std::string::const_iterator> >, 8, 6> ItBinaryT;

  try
  {
    // If the input isn't a multiple of 4, pad with =
    size_t num_pad_chars((4 - input.size() % 4) % 4);
    input.append(num_pad_chars, '=');

    size_t pad_chars(std::count(input.begin(), input.end(), '='));
    std::replace(input.begin(), input.end(), '=', 'A');
    std::string output(ItBinaryT(input.begin()), ItBinaryT(input.end()));
    output.erase(output.end() - pad_chars, output.end());
    return output;
  }
  catch (std::exception const&)
  {
    return std::string("");
  }
}

它取自此处,在其中还可以找到使用boost进行填充的编码功能.

It was taken from here, where an encoding function with padding using boost can also be found.

这篇关于使用Boost解码Base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆