我正在使用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;
}

字符串“ ABC”应编码为“ ywjj”,但结果是ywjj n,( n == 0x0a),长度为5。

更改源字符串无济于事,任何字符串都会使用尾随 n加密?谢谢

有帮助吗?

解决方案

来自 BASE64CODER文档, ,构造函数的签名是

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

因此,默认情况下,您会在编码字符串中获得断路。为了避免这种情况,只需做:

CryptoPP::StringSource ss(
    in,
    true, 
    new CryptoPP::Base64Encoder(
        new CryptoPP::StringSink(encoded),
        false
    )
);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top