Base64编码的连接code。使用升压抛出异常 [英] Base64 encode using boost throw exception

查看:289
本文介绍了Base64编码的连接code。使用升压抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用升压的base64 EN codeR,我发现了一个例子,但我得到了和异常

I trying to use boost base64 encoder, I found an example but I got and exception

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未处理的异常:微软C ++
  例外:提高::档案::迭代器:: dataflow_exception在内存
  位置0x0046ed94 ..

Unhandled exception at 0x75b1b9bc in agentid_coder.exe: Microsoft C++ exception: boost::archive::iterators::dataflow_exception at memory location 0x0046ed94..

我发现这个解决办法,但我得到了同样的结果。

I found this workaround but I get the same result

 string dec( 
        it_binary_t(Encrip.begin()), 
        it_binary_t(Encrip.begin() + Encrip.length() - 1) 
        ); 

我使用MSVS2008并提高1.38

I am using MSVS2008 and boost 1.38

推荐答案

不幸的是,组合两 iterator_adaptors binary_from_base64 transform_width 不是一个完整的base64连接codeR /德$ C $铬。 Base64编码重新presents的24位(3个字节)为4个字符,每一个连接$ C $ 6 CS位。如果输入数据不是这样的3字节组的整数倍它有一个或两个零字节填充。为了表明填充字节是多少增加,一个或两个 = 字符被追加到EN codeD字符串。

Unfortunately the combination of the two iterator_adaptors binary_from_base64 and transform_width is not a complete base64 encoder/decoder. Base64 represents groups of 24 bits (3 bytes) as 4 characters, each of which encodes 6 bits. If the input data is not an integer multiple of such 3 byte groups it has to be padded with one or two zero bytes. To indicate how many padding bytes were added, one or two = characters are appended to the encoded string.

transform_width ,这是负责的8位二进制整数6bit的转换不会自动应用此填充,它也可以由用户来完成。一个简单的例子:

transform_width, which is responsible for the 8bit binary to 6bit integer conversion does not apply this padding automatically, it has do be done by the user. A simple example:

#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 '\0'
  string result(it_binary_t(base64.begin()), it_binary_t(base64.end())); // decode
  result.erase(result.end()-paddChars,result.end());  // erase padding '\0' characters
  cout << "Decoded: " << result << endl;
  return 0;
}

请注意,我添加了 insert_linebreaks remove_whitespace 迭代器,这样就很好地格式化和base64输入流中的Base64输出换行可德codeD。这些都是可选的,但。

Note that I added the insert_linebreaks and remove_whitespace iterators, so that the base64 output is nicely formatted and base64 input with line breaks can be decoded. These are optional though.

与要求不同的填充不同的输入字符串运行:

Run with different input strings which require different padding:

$ ./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!!!

您可以检查这个在线连接$ c中的字符串的base64 $ CR /德codeR

这篇关于Base64编码的连接code。使用升压抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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