I'm using CryptoPP to encode a string "abc", the problem is the encoded base64 string always has a trailing '0x0a'? here's my code:
#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;
}
the string "abc" should be encoded to "YWJj", but the result is YWJj\n, (\n == 0x0a), and the length is 5 .
Changing the source string doesn't help, any string will be encrypted with a trailing \n Why is that? Thanks
From the Base64Encoder docs, the signature of the constructor is
Base64Encoder (BufferedTransformation *attachment=NULL, bool insertLineBreaks=true, int maxLineLength=72)
So you get line breaks by default in your encoded string. To avoid this, just do:
CryptoPP::StringSource ss(
in,
true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(encoded),
false
)
);