2012-05-09 16 views
7

मैं बढ़ावा बेस 64 encoder का उपयोग करने की कोशिश कर, मैं एक उदाहरण पाया लेकिन मुझे मिला है और अपवादबेस 64 एन्कोड बढ़ावा का उपयोग कर फेंक अपवाद

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 पर अनचाहे अपवाद: माइक्रोसॉफ्ट सी ++ अपवाद: boost :: archive :: iterators :: dataflow_exception स्मृति स्थान 0x0046ed94 ..

पर मैं इस समाधान का पाया, लेकिन मैं एक ही परिणाम

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

मैं MSVS2008 उपयोग कर रहा हूँ और 1.38

+0

बूस्ट सी ++ लाइब्रेरी का उपयोग कर बेस 64 एन्कोडिंग फ़ंक्शंस: http://stackoverflow.com/questions/34680998/attempt-to-decode-a-value-not-in-base64-char-set – ap6491

उत्तर

28

को बढ़ावा देने के दुर्भाग्य से दो iterator_adaptorsbinary_from_base64 के संयोजन और मिल transform_width एक पूर्ण बेस 64 एन्कोडर/डिकोडर नहीं है। बेस 64 24 बिट्स (3 बाइट्स) के समूहों को 4 अक्षरों के रूप में दर्शाता है, जिनमें से प्रत्येक 6 बिट्स को एन्कोड करता है। यदि इनपुट डेटा ऐसे 3 बाइट समूहों का पूर्णांक नहीं है तो इसे एक या दो शून्य बाइट्स के साथ गद्देदार होना चाहिए। यह इंगित करने के लिए कि कितने पैडिंग बाइट जोड़े गए थे, एक या दो = वर्ण एन्कोडेड स्ट्रिंग में जोड़े गए हैं।

transform_width, जो 8 बिट बाइनरी के लिए 6 बिट पूर्णांक रूपांतरण के लिए ज़िम्मेदार है, यह पैडिंग स्वचालित रूप से लागू नहीं होता है, यह उपयोगकर्ता द्वारा किया जाता है। एक साधारण उदाहरण:

#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 iterators कहा, ताकि base64 उत्पादन अच्छी तरह से स्वरूपित किया गया है और लाइन टूट के साथ बेस 64 इनपुट डीकोड किया जा सकता। हालांकि ये वैकल्पिक हैं।

विभिन्न इनपुट तार जो विभिन्न गद्दी की आवश्यकता के साथ रन:

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

आप इस online-encoder/decoder साथ बेस 64 तार देख सकते हैं।

+2

पूर्ण एन्कोड जोड़ने के लिए अच्छा किया गया/डीकोड कार्यान्वयन! पैडिंग की कमी ने मुझे भी पकड़ा। – DanDan

+0

क्या आप समझा सकते हैं कि आपको दूसरे 3 3 (3-एस। लम्बाई()% 3)% 3 – NoSenseEtAl

+0

में क्यों आवश्यकता है, हमें परिणामस्वरूप {0,1,2} की आवश्यकता है। दूसरे% 3 के बिना हमें परिणाम मिलता है {1,2,3}। अंतिम% 3 परिणाम 3 से 0 परिणाम और अकेले 1 और 2 छोड़ देता है। एक और संभावना एस। लम्बाई()% 3? 3-एस। लम्बाई()% 3: 0 – PiQuer