質問

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で暗号化されます。なぜそれはなぜですか?ありがとう

役に立ちましたか?

解決

から base64encoderドキュメント, 、コンストラクターの署名はです

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