crypto++ 使用Base64编码解码数组

使用Crypto++库中的Base64Encoder、Base64Decoder编码解码数组。代码如下:

//
// Created by gj on 12/24/19.
//
#include <iostream>
#include <crypto++/base64.h>
#include <crypto++/sha.h>
#include <crypto++/filters.h>
#include <cryptopp/base64.h>

using namespace std;
using namespace CryptoPP;

int main() {
    // "CBC Mode Test", without '\0'
    unsigned char plainText[] = {67, 66, 67, 32, 77, 111, 100, 101, 32, 84, 101, 115, 116};

    string encoded;

    Base64Encoder encoder;
    encoder.Put(plainText, sizeof(plainText));
    encoder.MessageEnd();

    word64 size = encoder.MaxRetrievable();
    if (size) {
        encoded.resize(size);
        encoder.Get((byte *) &encoded[0], encoded.size());
    }

    cout << encoded << endl;

    string decoded;
    Base64Decoder decoder;
    decoder.Put((byte *) encoded.data(), encoded.size());
    decoder.MessageEnd();

    size = decoder.MaxRetrievable();
    if (size && size <= SIZE_MAX) {
        decoded.resize(size);
        decoder.Get((byte *) &decoded[0], decoded.size());
    }
    cout << decoded << endl;

    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/areful/p/12092235.html