Value after cryptopp base64 encoding/decoding not the same

限于喜欢 提交于 2021-01-28 19:01:14

问题


I'm playing with cryptopp and have trouble with Base64 encoding/decoding.

In the following code hypothetically sig value should be equal tsig, but they are different on a last character (sig bigger then tsig by one symbol). I've tried also change insertLineBreaks parameter in Base64Encoder but the result is same. ...

RSASSA_PKCS1v15_SHA_Signer privkey(privateKey);
SecByteBlock sbbSignature(privkey.SignatureLength());
privkey.SignMessage(rng, (byte const*) strContents.data(),
    strContents.size(),sbbSignature);

Base64Encoder b(new StringSink(signedData));
b.Put(sbbSignature.begin(), sbbSignature.size());

string sig;
StringSink sinksig(sig);
sinksig.Put(sbbSignature.begin(), sbbSignature.size());

string tsig;
StringSource ss(signedData, true, 
    new Base64Decoder(
        new StringSink(tsig)
    )
);

Where is my mistake?


回答1:


b.Put(sbbSignature.begin(), sbbSignature.size());

Try:

b.Put(sbbSignature.begin(), sbbSignature.size());
b.MessgeEnd();

This does not quite look right:

SecByteBlock sbbSignature(privkey.SignatureLength());
privkey.SignMessage(rng, (byte const*) strContents.data(),
    strContents.size(),sbbSignature);

Try:

size_t maxLength = privkey.MaxSignatureLength();
SecByteBlock sbbSignature(maxLength);

size_t signatureLength = privkey.SignMessage(rng,
    (byte const*) strContents.data(), strContents.size(),
    sbbSignature);

if(maxLength != signatureLength)
    sbbSignature.resize(signatureLength);

There's an example on the Crypto++ wiki at RSA Signature Scheme with Appendix, but I think its wrong after looking at it.



来源:https://stackoverflow.com/questions/23095254/value-after-cryptopp-base64-encoding-decoding-not-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!