问题
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