简体   繁体   中英

Value after cryptopp base64 encoding/decoding not the same

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM