gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:45:26 25 4
gpt4 key购买 nike

我正在使用以下代码片段使用 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();
}

编码工作正常,但是在解码时出现以下错误:

terminate called after throwing an instance of boost::archive::iterators::dataflow_exception

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

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

最佳答案

填充字符“=”是 b64 编码数据的一部分,在解码前不应删除。b64 以 4 个字符的 block 编码,我怀疑在解码时它会在字符串末尾读取“\0”而不是预期的“=”。

关于c++ - 尝试解码不在 base64 字符集中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com