使用提升抛出异常进行 Base64 编码
Base64 encode using boost throw exception
>我尝试使用boost base64编码器,我找到了一个示例,但我得到了异常
typedef
transform_width< binary_from_base64<std::string::const_iterator>, 8, 6 > it_binary_t
一个我用过的
std::string b64E(it_binary_t(Encrip.begin()), it_binary_t(Encrip.end()));
我明白了
agentid_coder.exe中0x75b1b9bc时未处理的异常:Microsoft C++ 异常: boost::存档::迭代器:内存:d ataflow_exception 位置0x0046ed94..
我找到了这个解决方法,但我得到相同的结果
string dec(
it_binary_t(Encrip.begin()),
it_binary_t(Encrip.begin() + Encrip.length() - 1)
);
我正在使用 MSVS2008 和提升 1.38
不幸的是,
两个iterator_adaptors
binary_from_base64
和transform_width
的组合并不是一个完整的base64编码器/解码器。Base64 将 24 位(3 个字节)的组表示为 4 个字符,每个字符编码 6 位。如果输入数据不是此类 3 字节组的整数倍,则必须用一个或两个零字节填充。为了指示添加了多少填充字节数,将一个或两个=
字符追加到编码字符串。
负责 8 位二进制到 6 位整数转换的transform_width
不会自动应用此填充,它必须由用户完成。一个简单的例子:
#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 <iostream>
#include <string>
using namespace boost::archive::iterators;
using namespace std;
int main(int argc, char **argv) {
typedef transform_width< binary_from_base64<remove_whitespace<string::const_iterator> >, 8, 6 > it_binary_t;
typedef insert_linebreaks<base64_from_binary<transform_width<string::const_iterator,6,8> >, 72 > it_base64_t;
string s;
getline(cin, s, 'n');
cout << "Your string is: '"<<s<<"'"<<endl;
// Encode
unsigned int writePaddChars = (3-s.length()%3)%3;
string base64(it_base64_t(s.begin()),it_base64_t(s.end()));
base64.append(writePaddChars,'=');
cout << "Base64 representation: " << base64 << endl;
// Decode
unsigned int paddChars = count(base64.begin(), base64.end(), '=');
std::replace(base64.begin(),base64.end(),'=','A'); // replace '=' by base64 encoding of ' '
string result(it_binary_t(base64.begin()), it_binary_t(base64.end())); // decode
result.erase(result.end()-paddChars,result.end()); // erase padding ' ' characters
cout << "Decoded: " << result << endl;
return 0;
}
请注意,我添加了insert_linebreaks
和remove_whitespace
迭代器,以便 base64 输出的格式很好,并且可以解码带有换行符的 base64 输入。不过,这些是可选的。
使用需要不同填充的不同输入字符串运行:
$ ./base64example
Hello World!
Your string is: 'Hello World!'
Base64 representation: SGVsbG8gV29ybGQh
Decoded: Hello World!
$ ./base64example
Hello World!!
Your string is: 'Hello World!!'
Base64 representation: SGVsbG8gV29ybGQhIQ==
Decoded: Hello World!!
$ ./base64example
Hello World!!!
Your string is: 'Hello World!!!'
Base64 representation: SGVsbG8gV29ybGQhISE=
Decoded: Hello World!!!
您可以使用此在线编码器/解码器检查 base64 字符串。
相关文章:
- 解码 c++ 中以 base64 编码的大量数据
- CryptoPP vs PHP Base64 编码/解码
- 在FLTK中显示Base64编码图像
- qt base64编码/decode添加逃脱字符
- 同一图像的不同 base64 编码
- Windows API base64编码/解码
- 如何从连接的 SSL 会话获取 base64 编码证书 (PEM)
- md5 base64 编码器在 C++ 与 Java 中
- Arduino SHA1-HMAC 和 base64 编码与 Python 之间的问题
- 使用提升抛出异常进行 Base64 编码
- Base64 编码失败
- base64编码空终止符
- 为什么Debian和AS400给出不同的base64编码字符串
- c++ base64编码- PHP兼容
- C++中Base64编码函数的逐位移位
- 来自终端的Base64编码字符串
- 在Cocos2d-x中加载Base64编码图像
- 使用base64编码CV_32FC1 Mat数据
- 如何将二进制字符串转换为base64编码的数据
- cryptopp-base64编码/解码后的值不相同