试图去code的值不是以base64字符集 [英] attempt to decode a value not in base64 char set

查看:184
本文介绍了试图去code的值不是以base64字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code段为Base64 code和德code字符串使用Boost C ++库连接。

I am using the following code snippet to base64 encode and decode a string using Boost C++ library.

//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();
}

编码工作得很好,但是,当我解码收到以下错误:

Encoding works well, however, while decoding I get the following error:

终止投掷的boost ::存档::迭代器:: dataflow_exception一个实例后调用

什么():试图去code的值不是以base64字符集

what(): attempt to decode a value not in base64 char set

难道是造成这一问题的填充字符中的一个?我失去了一些东西在这里?

Is it one of the padded characters that is causing this issue? Am I missing something here?

推荐答案

的填充字符'='是B64 EN codeD数据的一部分,并解码之前不应该被删除。
B64是连接在4个字符块$ C $的CD,我怀疑,同时解码读取一个'\\ 0',而不是预期的'='在字符串的结尾。

The padding characters '=' are part of the b64 encoded data and should not be removed before decoding. b64 is encoded in blocks of 4 character, I suspect that while decoding it reads a '\0' instead of an expected '=' at the end of the string.

这篇关于试图去code的值不是以base64字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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