尝试解码不在base64字符集中的值

时间:2016-01-08 15:53:50

标签: c++ c++11 boost character-encoding base64

我正在使用以下代码片段对使用Boost C ++库的字符串进行base64编码和解码。

//Base64 Encode Implementation using Boost C++ library
const std::string base64_padding[] = {"", "=", "=="};

std::string X_Privet_Token_Generator::base64_encode(const std::string & s)
{
  namespace bai = boost::archive::iterators;

  std::stringstream os;

  // convert binary values to base64 characters
  typedef bai::base64_from_binary

  // retrieve 6 bit integers from a sequence of 8 bit bytes
  <bai::transform_width<const char *, 6, 8> > base64_enc; // compose all the above operations in to a new iterator

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

  os << base64_padding[s.size() % 3];
  return os.str();
}

std::string X_Privet_Token_Generator::base64_decode(std::string & s)
{
  namespace bai = boost::archive::iterators;

  std::stringstream os;

  // convert binary values to base64 characters
  typedef bai::binary_from_base64

  <bai::transform_width<const char *, 8, 6> > base64_dec;

  unsigned int size = s.size();

  // Remove the padding characters, cf.
  if (size && s[size - 1] == '=')
  {
      --size;
      if (size && s[size - 1] == '=')
          --size;
  }

  if (size == 0)
      return std::string();

  LOGINFO("Hash decoded token : %s", s.c_str());
  std::copy(base64_dec(s.data()), base64_dec(s.data() + size), std::ostream_iterator<char>(os));
  std::cout<< os.str();
  return os.str();
}

编码效果很好,但是,在解码时我收到以下错误:

  

在抛出boost::archive::iterators::dataflow_exception

的实例后终止调用      

what():尝试解码不在base64 char set

中的值

是导致此问题的填充字符之一吗?我在这里错过了什么吗?

3 个答案:

答案 0 :(得分:4)

填充字符&#39; =&#39;是b64编码数据的一部分,在解码之前不应删除。 b64是用4个字符的块编码的,我怀疑它在解码时会读取一个&#39; \ 0&#39;而不是预期的&#39; =&#39;在字符串的末尾。

答案 1 :(得分:0)

更改

std::copy(base64_dec(s.data()), base64_dec(s.data() + size), std::ostream_iterator<char>(os))

return std::string( base64_dec(s.c_str()), base64_dec(s.c_str() + size))

解决了这个问题。

答案 2 :(得分:0)

base64编码和解码的更有效解决方案如下:

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include <boost/algorithm/string.hpp>
#include <bits/stl_algo.h>

std::string X_Privet_Token_Generator::base64_encode(std::string s)
{
    namespace bai = boost::archive::iterators;

    std::stringstream os;

    // convert binary values to base64 characters
    typedef bai::base64_from_binary

    // retrieve 6 bit integers from a sequence of 8 bit bytes
    <bai::transform_width<char *, 6, 8> > base64_enc; // compose all the above operations in to a new iterator

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

    os << base64_padding[s.size() % 3];
    return os.str();
}

std::string X_Privet_Token_Generator::base64_decode(std::string s)
{
    namespace bai = boost::archive::iterators;

    std::stringstream os;

    typedef bai::transform_width<bai::binary_from_base64<char * >, 8, 6>
      base64_dec;

    unsigned int size = s.size();

    // Remove the padding characters.
    if(size && s[size - 1] == '=') {
        --size;
    if(size && s[size - 1] == '=')
        --size;
    }

    if(size == 0) return std::string();

    unsigned int paddChars = count(s.begin(), s.end(), '=');
    std::replace(s.begin(),s.end(), '=', 'A');
    std::string decoded_token(base64_dec(s.c_str()), base64_dec(s.c_str() + size));
    decoded_token.erase(decoded_token.end()-paddChars,decoded_token.end());
    return decoded_token;

}