Domanda

Sto usando CryptopP per codificare una stringa "ABC", il problema è che la stringa Base64 codificata ha sempre un "0x0a" finali? Ecco il mio codice:

#include <iostream>
#include <string>

using namespace std;

#include "crypto/base64.h"
using namespace CryptoPP;

int main() {
string in("abc");

string encoded;

CryptoPP::StringSource ss(
    in,
    true, 
    new CryptoPP::Base64Encoder(
        new CryptoPP::StringSink(encoded)
    )
);

cout << encoded.length() << endl;// outputs 5, should be 4
cout << encoded;
}

La stringa "ABC" dovrebbe essere codificata in "ywjj", ma il risultato è ywjj n, ( n == 0x0a) e la lunghezza è 5.

La modifica della stringa di origine non aiuta, nessuna stringa verrà crittografata con un trailing n Perché? Grazie

È stato utile?

Soluzione

Dal Base64Ecoder Docs, la firma del costruttore è

Base64Encoder (BufferedTransformation *attachment=NULL,
               bool insertLineBreaks=true,
               int maxLineLength=72)

Quindi ottieni interruzioni di linea per impostazione predefinita nella stringa codificata. Per evitare questo, fai solo:

CryptoPP::StringSource ss(
    in,
    true, 
    new CryptoPP::Base64Encoder(
        new CryptoPP::StringSink(encoded),
        false
    )
);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top