cryptopp base64尾随0x0a

aj3*_*423 1 base64 crypto++

我正在使用CryptoPP来编码字符串"abc",问题是编码的base64字符串总是有一个尾随的'0x0a'?这是我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

字符串"abc"应编码为"YWJj",但结果为YWJj \n,(\n == 0x0a),长度为5.

更改源字符串没有帮助,任何字符串都将使用尾随加密\n为什么是这样?谢谢

Fra*_*ser 6

Base64Encoder文档中,构造函数的签名是

Base64Encoder (BufferedTransformation *attachment=NULL,
               bool insertLineBreaks=true,
               int maxLineLength=72)
Run Code Online (Sandbox Code Playgroud)

因此,您在编码字符串中默认获得换行符.为避免这种情况,只需:

CryptoPP::StringSource ss(
    in,
    true, 
    new CryptoPP::Base64Encoder(
        new CryptoPP::StringSink(encoded),
        false
    )
);
Run Code Online (Sandbox Code Playgroud)