User Guide: rsa.h
From Crypto++ Wiki
[edit] rsa.h
The classes actually defined in rsa.h, RSAFunction and InvertibleRSAFunction, are basic classes that envelop a plain RSA public key and private key, respectively. The classes support directly RSA computation through methods ApplyFunction() (the public-key operation) and CalculateInverse() (the private-key operation). You should only use these methods directly if you know what you are doing, and are applying appropriate padding.
More commonly, you will want to use typedefs RSAES_OAEP_SHA_Decryptor, RSAES_OAEP_SHA_Encryptor, RSASSA_PKCS1v15_SHA_Signer, RSASSA_PKCS1v15_SHA_Verifier, and other typedefs of the same sort also defined in rsa.h. The classes identified by these typedefs are derived from an array of base classes declared in pubkey.h. The various RSA Encryptor/Decryptor/Signer/Verifier typedef'ed classes will be easiest to use if you remember that they all ultimately inherit either from PK_FixedLengthEncryptor, PK_FixedLengthDecryptor, PK_Signer or PK_Verifier, all of them declared in cryptlib.h.
[edit] Examples
Generate an RSA keypair and save it:
// InvertibleRSAFunction is used directly only because the private key
// won't actually be used to perform any cryptographic operation;
// otherwise, an appropriate typedef'ed type from rsa.h would have been used.
AutoSeededRandomPool rng;
InvertibleRSAFunction privkey(rng, 1024);
// With the current version of Crypto++, MessageEnd() needs to be called
// explicitly because Base64Encoder doesn't flush its buffer on destruction.
Base64Encoder privkeysink(new FileSink("c:\\privkey.txt"));
privkey.DEREncode(privkeysink);
privkeysink.MessageEnd();
// Suppose we want to store the public key separately,
// possibly because we will be sending the public key to a third party.
RSAFunction pubkey(privkey);
Base64Encoder pubkeysink(new FileSink("c:\\pubkey.txt"));
pubkey.DEREncode(pubkeysink);
pubkeysink.MessageEnd();
Load a private key and sign a file:
string strContents;
FileSource("c:\\tobesigned.dat", true,
new StringSink(strContents));
RSASSA_PKCS1v15_SHA_Signer privkey(
FileSource("c:\\privkey.txt", true,
new Base64Decoder)));
SecByteBlock sbbSignature(privkey.SignatureLength());
AutoSeededRandomPool rng;
privkey.SignMessage(
rng,
(byte const*) strContents.data(),
strContents.size(),
sbbSignature.Begin());
FileSink sink("c:\\signed.dat");
sink.Put((byte const*) strContents.data(), strContents.size());
sink.Put(sbbSignature.Begin(), sbbSignature.Size());
Load a public key and encrypt a short string using RSA:
string strShortString =
"Must be shorter than the size of the RSA key minus OAEP decoration.";
RSAES_OAEP_SHA_Encryptor pubkey(
FileSource("c:\\pubkey.txt", true,
new Base64Decoder)));
// Cannot use std::string for buffer;
// its internal storage might not be contiguous
SecByteBlock sbbCipherText(pubkey.CipherTextLength(strShortString.size()));
AutoSeededRandomPool rng;
pubkey.Encrypt(
rng,
(byte const*) strShortString.data(),
strShortString.size(),
sbbCipherText.Begin());
FileSink("c:\\encrypted.dat").Put(sbbCipherText.Begin(), sbbCipherText.Size());
To find other topics in the User Guide, visit Category:User Guide.