Base64 koduje za pomocą wyjątku boost throw

Próbuję użyć kodera boost base64, znalazłem przykład, ale mam i wyjątek

typedef 
transform_width< binary_from_base64<std::string::const_iterator>, 8, 6 > it_binary_t

An I used

std::string b64E(it_binary_t(Encrip.begin()), it_binary_t(Encrip.end()));

I get it

Nieobsługiwany wyjątek na 0x75b1b9bc w agentid_coder.exe: Microsoft C++ wyjątek: boost:: archiwum:: Iteratory:: dataflow_exception w pamięci lokalizacja 0x0046ed94..

Znalazłem to obejście, ale otrzymałem ten sam wynik

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

Używam MSVS2008 i boost 1.38

Author: Agus, 2012-05-09

1 answers

Niestety połączenie tych dwóch iterator_adaptors binary_from_base64 i transform_width nie jest kompletnym koderem/dekoderem base64. Base64 reprezentuje grupy 24 bitów (3 bajty) jako 4 znaki, z których każdy koduje 6 bitów. Jeżeli dane wejściowe nie są liczbą całkowitą z takich 3 grup bajtów, muszą być wypełnione jednym lub dwoma bajtami zerowymi. Aby wskazać, ile bajtów wypełnienia zostało dodanych, do zakodowanego ciągu dołącza się jeden lub dwa znaki =.

transform_width, który jest odpowiedzialny za binarne 8Bit do 6bit konwersja liczby całkowitej nie stosuje tego wypełnienia automatycznie, zostało to wykonane przez użytkownika. Prosty przykład:

#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;
}

Zauważ, że dodałem Iteratory insert_linebreaks i remove_whitespace, dzięki czemu wyjście base64 jest ładnie sformatowane, a wejście base64 z podziałem linii może być dekodowane. Są one jednak opcjonalne.

Uruchom z różnymi ciągami wejściowymi, które wymagają innego wypełnienia:

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

Możesz sprawdzić ciągi Base64 za pomocą tego online-encoder / decoder.

 35
Author: PiQuer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-08 11:02:28