Further information. It looks like when saving the key to base64 the
string is truncated. If I save the key to a string first, then base64
encode it everything works correctly, however if I save it directly to
base64 a few bytes are truncated from the end. Not sure, but does
this have anything to do with Base64Encoder still having bytes left in
the buffer after messageEnd, whereas HexEncoder has 0 bytes left in
the buffer on messageEnd? Take a look at the following code - saving
directly to base64 gets truncated.
--------------------------------------
#include <iostream>
#include <crypto++/rsa.h>
#include <crypto++/osrng.h>
#include <crypto++/files.h>
#include <crypto++/filters.h>
#include <crypto++/hex.h>
#include <crypto++/base64.h>
#include <string>
#include <cstring>
using namespace std;
using namespace CryptoPP;
int main(int argc, char* argv[])
{
try
{
RSA::PrivateKey privateKey;
AutoSeededRandomPool rng;
InvertibleRSAFunction params;
string priv, hold, hold2;
params.GenerateRandomWithKeySize(rng, 256);
privateKey.AssignFrom(params);
// Save to a string first then bas64 encode.
privateKey.Save(StringSink(priv).Ref());
StringSource(priv, true,
new Base64Encoder(
new StringSink(hold)));
// Save directly to base64 (truncated).
privateKey.Save(Base64Encoder(
new StringSink(hold2)).Ref());
cout << "Saved to string then encoded: " <<
hold.length() << " bytes: " << endl << hold << endl;
cout << "Saved directly to base64 (truncated): " <<
hold2.length() << " bytes: " << endl << hold2 << endl;
}
catch (Exception e)
{
cout << e.what() << endl;
}
return 0;
}
--
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at
http://www.cryptopp.com.