|
// CryptoPP.cpp
//
#include "stdafx.h"
#include <osrng.h>
using CryptoPP::AutoSeededRandomPool;
#include <rsa.h>
using CryptoPP::RSAFunction;
typedef CryptoPP::InvertibleRSAFunction RSAPrivateKey;
using CryptoPP::InvertibleRSAFunction;
typedef CryptoPP::RSAFunction RSAPublicKey;
#include <files.h>
using CryptoPP::FileSource;
using CryptoPP::FileSink;
bool GenerateAndSaveKeys();
bool LoadKeys();
int main(int argc, char* argv[])
{
GenerateAndSaveKeys();
LoadKeys();
return 0;
}
bool GenerateAndSaveKeys()
{
try
{
AutoSeededRandomPool prng;
// Private Key
RSAPrivateKey privateKey;
privateKey.Initialize( prng, 1024 /*, e=17*/);
// Save as PKCS #8 (using ASN.1 DER Encoding )
privateKey.Save( FileSink("private.rsa.cpp.key") );
// Public Key
RSAPublicKey publicKey( privateKey );
// Save as X.509 (using ASN.1 DER Encoding )
publicKey.Save( FileSink("public.rsa.cpp.key") );
// Incorrect...
// publicKey.DEREncodePublicKey ( FileSink("public.rsa.cpp.key") );
cout << "Saved key with modulus: " << endl;
cout << privateKey.GetModulus() << endl;
cout << endl;
}
catch( CryptoPP::Exception& ex )
{
cout << "Caught Error:" << endl;
cout << " " << ex.what() << endl;
return false;
}
return true;
}
bool LoadKeys()
{
try
{
// Private Key
RSAPrivateKey privateKey;
// Load as PKCS #8 (using ASN.1 BER Decoding )
privateKey.Load( FileSource( "private.rsa.cpp.key", true ) );
// Public Key
RSAPublicKey publicKey;
// Load as X.509 (using ASN.1 BER Decoding )
publicKey.Load( FileSource( "public.rsa.cpp.key", true ) );
// Incorrect...
// The 2nd and 3rd parameters to BERDecodePublicKey are not used,
// so they can be anything. See RSAFunction::BERDecodePublicKey
// publicKey.BERDecodePublicKey(FileSource( "public.rsa.cpp.key", true ), false, 0);
cout << "Loaded key with modulus: " << endl;
cout << privateKey.GetModulus() << endl;
cout << endl;
}
catch( CryptoPP::Exception& ex )
{
cout << "Caught Error:" << endl;
cout << " " << ex.what() << endl;
return false;
}
return true;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.