I need to a sample code of generating an RSA-1024 bit keypair for sign and verify!

18 views
Skip to first unread message

Gary

unread,
Feb 4, 2009, 1:39:08 PM2/4/09
to Crypto++ Users
Hi dear friends!
I'm using "Visual C++ 2008" and "CryptoPP 5.5.2" version!
I want to generate an RSA-1024 bit keypair for sign and verify in a
function of my project,there is a sample code of "generate an RSA
keypair and save it" in the "Crypto++ user's guide" by denis bider as
below:

// 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();


But this code is applied for previous versions of "Cryptopp" library
and doesn't match with "CryptoPP 5.5.2" version!
Also,this code locates Generated keys in a file,but I want to locate
generated keys in two byte arrays.
I've studied "Reference" and I have found that should use of
"BufferedTransformation" type,but I don't know how to use it,because I
have no sample code of using it.
So far,I have not had any experience to implementing a cryptographic
scheme using "Cryptopp" library and really don't know what should I
do?!
I have really spent so much time on it, but I couldn't write any code!
I greatly ask you help me with some sample codes about "generate
RSA-1024 keypair" and "sign/verify with RSA-1024 and SHA1 hash
algorithm"
I'm so new to "Cryptopp" library and need to help as soon as
possible,hence ask you help me!
Thanks in Advance.

Jeffrey Walton

unread,
Feb 4, 2009, 4:39:57 PM2/4/09
to Gary, Crypto++ Users
Hi Gary,

You migh also want to look at the Wiki: http://www.cryptopp.com/wiki/RSA.

> Also,this code locates Generated keys in a file,but I want to locate
> generated keys in two byte arrays.

Use either a StringSource/StringSink or ArraySource/ArraySink rather
than a FIleSource/FileSink.

Jeff

On 2/4/09, Gary <b.rost...@gmail.com> wrote:
>
> Hi dear friends!
> I'm using "Visual C++ 2008" and "CryptoPP 5.5.2" version!
> I want to generate an RSA-1024 bit keypair for sign and verify in a
> function of my project,there is a sample code of "generate an RSA
> keypair and save it" in the "Crypto++ user's guide" by denis bider as
> below:
>

> [ SNIP ]

Gary

unread,
Feb 6, 2009, 1:11:22 PM2/6/09
to Crypto++ Users
Hi Dear Jeff and Thank you very much!

The link:http://www.cryptopp.com/wiki/RSA. was very useful for me,I
built all 5 programs successfully!
Now my questions:(I have two questions!)

The "RSAKeyGen" program is as below:
#include "stdafx.h"

// Crypto++ Includes
#include "rsa.h"
#include "osrng.h" // PRNG
#include "hex.h" // Hex Encoder/Decoder
#include "files.h" // File Source and Sink

int main(int argc, char* argv[])
{
try
{
std::string PrivateKeyFile = "key.pv";
std::string PublicKeyFile = "key.pb";

CryptoPP::AutoSeededRandomPool rng;

// Specify 512 bit modulus, accept e = 17
CryptoPP::RSAES_OAEP_SHA_Decryptor Decryptor( rng, 512 /*, e
*/ );
CryptoPP::HexEncoder privFile(new
CryptoPP::FileSink( PrivateKeyFile.c_str() )
); // Hex Encoder

Decryptor.DEREncode(privFile);
privFile.MessageEnd();

CryptoPP:: RSAES_OAEP_SHA_Encryptor Encryptor(Decryptor);
CryptoPP::HexEncoder pubFile(new
CryptoPP::FileSink( PublicKeyFile.c_str() )
); // Hex Encoder
Encryptor.DEREncode(pubFile);
pubFile.MessageEnd();
}

catch( CryptoPP::Exception& e ) {
std::cerr << "Error: " << e.what() << std::endl;
}

catch (...) {
std::cerr << "Unknown Error" << std::endl;
}

return 0;
}

Which locates Genereted keys in two files named: "key.pv" and
"key.pb"
The contents of "key.pb" is 92 bytes and the contents of "key.pv" is
344 bytes!
My first question is that why key files contents are as above,whereas
in the code defined 512 bits(64 bytes) for modulus?
Why does "key.pv" contain 344 bytes?!
(What is it's meaning? Does that mean 280 bytes for "d" parameter of
"private key"?



My second question:
Because I want to generate key pair and locate them in two byte
arrays,as your advice, I used of "ArraySink" instead of "FileSink" and
changed the above code as below:


#include "stdafx.h"

// Crypto++ Includes
#include "rsa.h"
#include "osrng.h" // PRNG
#include "hex.h" // Hex Encoder/Decoder
#include "files.h" // File Source and Sink
#include "filters.h" //Array Source and Sink

int main(int argc, char* argv[])
{
try
{
byte* privatekey;
byte* publickey;
//std::string PrivateKeyFile = "key.pv";
//std::string PublicKeyFile = "key.pb";

CryptoPP::AutoSeededRandomPool rng;

// Specify 512 bit modulus, accept e = 17
CryptoPP::RSAES_OAEP_SHA_Decryptor Decryptor( rng, 512 /*, e
*/ );
CryptoPP::HexEncoder privArray(new
CryptoPP::ArraySink(privatekey,sizeof(privatekey))
); // Hex Encoder

Decryptor.DEREncode(privArray);
privArray.MessageEnd();

CryptoPP:: RSAES_OAEP_SHA_Encryptor Encryptor(Decryptor);
CryptoPP::HexEncoder pubArray(new
CryptoPP::ArraySink( publickey, sizeof(publickey))
); // Hex Encoder
Encryptor.DEREncode(pubArray);
pubArray.MessageEnd();

}

catch( CryptoPP::Exception& e ) {
std::cerr << "Error: " << e.what() << std::endl;

}

catch (...) {
std::cerr << "Unknown Error" << std::endl;
}

return 0;
}

After compile,I got this output:(3 warnings and 0 error!)

------ Build started: Project: RSAKeyGen, Configuration: Debug Win32
------
Compiling...
RSAKeyGen.cpp
d:\rsakeygen.cpp(26) : warning C4700: uninitialized local variable
'privatekey' used
d:\rsakeygen.cpp(34) : warning C4700: uninitialized local variable
'publickey' used

Linking...
RSAKeyGen.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/
INCREMENTAL:NO'


RSAKeyGen - 0 error(s), 3 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped
==========


But after running it's .exe file,I got runtime error!

I want to show the contents of two key arrays in output and can use
of their contents, but I don't know what should I do?
Which section of the changed code is wrong and how can I see the
contents of arrays and use or manipulate them?

I'll greatly appreciate you, if help me again;
Good Luck!
Gary







On Feb 5, 12:39 am, Jeffrey Walton <noloa...@gmail.com> wrote:
> Hi Gary,
>
> You migh also want to look at the Wiki:http://www.cryptopp.com/wiki/RSA.
>
> > Also,this code locates Generated keys in a file,but I want to locate
> > generated keys in two byte arrays.
>
> Use either a StringSource/StringSink or ArraySource/ArraySink rather
> than a FIleSource/FileSink.
>
> Jeff
>
Reply all
Reply to author
Forward
0 new messages