• C++,Base64编解码字符串或文件


    参考链接:在C语言中使用libb64进行Base64编解码

    GitHub地址:https://github.com/BuYishi/cpp_base64_demo

    base64_demo.cpp

    #include <iostream>
    #include <fstream>
    #include "Base64Encoder.h"
    #include "Base64Decoder.h"
    using namespace std;
    void encodeDemo();
    void decodeDemo();
    int main()
    {
        encodeDemo();
        decodeDemo();
        system("pause");
        return 0;
    }
    void encodeDemo()
    {
        const char *text = "John Rambo";
        Base64Encoder encoder;
        const string &encodedText = encoder.encode(text);
        cout << encodedText << endl;
        string inputFilename("E:/Packages/Java/jdk-8u144-windows-x64.exe");
        ifstream ifs(inputFilename, ifstream::binary);
        if (ifs)
        {
            string encodedFilename(inputFilename + ".txt");
            ofstream ofs(encodedFilename, ofstream::binary);
            if (ofs)
            {
                streamsize numberOfBytesRead;
                int numberOfBytesEncoded;
                constexpr int inputBufferSize = 10 * 1024;
                char inputBuffer[inputBufferSize];
                char encodedBuffer[inputBufferSize * 2];
                while (!ifs.eof())
                {
                    numberOfBytesRead = ifs.read(inputBuffer, inputBufferSize).gcount();
                    numberOfBytesEncoded = encoder.encode(inputBuffer, numberOfBytesRead, encodedBuffer);
                    ofs.write(encodedBuffer, numberOfBytesEncoded);
                }
                numberOfBytesEncoded = encoder.encodeEnd(encodedBuffer);
                ofs.write(encodedBuffer, numberOfBytesEncoded);
            }
            else
                cout << "Cannot open file: " << encodedFilename << endl;;
        }
        else
            cout << "Cannot open file: " << inputFilename << endl;
    }
    void decodeDemo()
    {
        Base64Decoder decoder;
        const string &decodedText = decoder.decode("QWxsIGlzIHdlbGwh");
        cout << decodedText << endl;
        string filename("E:/Packages/Android/docs-24_r01.zip.txt");
        ifstream ifs(filename, ifstream::binary);
        if (ifs)
        {
            filename = "android_docoment";
            ofstream ofs(filename, ofstream::out | ofstream::binary);
            if (ofs)
            {
                streamsize numberOfBytesRead;
                int numberOfBytesDecoded;
                constexpr int bufferSize = 10 * 1024;
                char inputBuffer[bufferSize];
                char decodedBuffer[bufferSize];
                while (!ifs.eof())
                {
                    numberOfBytesRead = ifs.read(inputBuffer, 1024).gcount();
                    numberOfBytesDecoded = decoder.decode(inputBuffer, numberOfBytesRead, decodedBuffer);
                    ofs.write(decodedBuffer, numberOfBytesDecoded);
                }
            }
            else
                cout << "Cannot open file: " << filename << endl;
        }
        else
            cout << "Cannot open file: " << filename << endl;
    }

    Base64Encoder.h

    #ifndef BASE64_ENCODER_H
    #define BASE64_ENCODER_H
    #include <string>
    extern "C"
    {
    #include <libb64/cencode.h>
    }
    class Base64Encoder
    {
    public:
        Base64Encoder();
        std::string encode(const std::string &text);
        int encode(const char *inputBuffer, unsigned count, char *encodedBuffer);
        int encodeEnd(char *encodedBuffer);
    private:
        base64_encodestate state;
    };
    #endif

    Base64Encoder.cpp

    #include "Base64Encoder.h"
    Base64Encoder::Base64Encoder() { base64_init_encodestate(&state); }
    std::string Base64Encoder::encode(const std::string &text)
    {
        char *buffer = new char[text.length() * 2];
        int numberOfBytesEncoded = base64_encode_block(text.c_str(), text.length(), buffer, &state);
        numberOfBytesEncoded += base64_encode_blockend(buffer + numberOfBytesEncoded, &state);
        buffer[numberOfBytesEncoded] = '';
        std::string result(buffer);
        delete[] buffer;
        base64_init_encodestate(&state);
        return result;
    }
    int Base64Encoder::encode(const char *inputBuffer, unsigned count, char *encodedBuffer)
    {
        return base64_encode_block(inputBuffer, count, encodedBuffer, &state);
    }
    int Base64Encoder::encodeEnd(char *encodedBuffer)
    {
        return base64_encode_blockend(encodedBuffer, &state);
    }

    Base64Decoder.h

    #ifndef BASE64_DECODER_H
    #define BASE64_DECODER_H
    #include <string>
    extern "C"
    {
    #include <libb64/cdecode.h>
    }
    class Base64Decoder
    {
    public:
        Base64Decoder();
        std::string decode(const std::string &code);
        int decode(const char *inputBuffer, int count, char *decodedBuffer);
    private:
        base64_decodestate state;
    };
    #endif

    Base64Decoder.cpp

    #include "Base64Decoder.h"
    Base64Decoder::Base64Decoder() { base64_init_decodestate(&state); }
    std::string Base64Decoder::decode(const std::string &code)
    {
        char *buffer = new char[code.length()];
        int numberOfCharacters = base64_decode_block(code.c_str(), code.length(), buffer, &state);
        buffer[numberOfCharacters] = '';
        std::string result(buffer);
        delete[] buffer;
        return result;
    }
    int Base64Decoder::decode(const char *inputBuffer, int count, char *decodedBuffer)
    {
        return base64_decode_block(inputBuffer, count, decodedBuffer, &state);
    }
  • 相关阅读:
    图论复习
    数据结构复习
    数学复习
    字符串算法复习
    还有9天就要走了
    JS 多个一维数组排例组合的所有可能
    PHP 多个一维数组排列组合的所有可能
    维护DOWNID
    LINUX
    Multiple Versions of PHP run on server
  • 原文地址:https://www.cnblogs.com/buyishi/p/8415465.html
Copyright © 2020-2023  润新知