ECDSA Example

613 views
Skip to first unread message

bobby

unread,
Jul 18, 2009, 4:57:25 AM7/18/09
to Crypto++ Users
Hello,

I am interested in using ECC or more probably ECDSA to verify a string
transmitted by a server. The way I'd like it to work is that the
server will create any arbitrary text string. It'll take the string
and hash it and then calculate a signature using some private key.
Then it will transmit the string and the signature and I'd like the
client to be able to verify that the string and signature match using
only the public key information. My only other requirement is that I
need to be able to generate new private/public key pairs from time to
time. I also need fairly short key sizes (less than 160 bits).

From my limited knowledge, it sounds like ECDSA is what I want.
Unfortunately, I am not very math savvy and I start to get lost once
people start bringing up the formulas. I am also having a somewhat
difficult time understanding how all the Crypto++ code works with
regards to ECDSA.

So does anyone have any example code that shows this kind of
functionality?

Thanks!

Eugene Zolenko

unread,
Jul 20, 2009, 12:02:11 PM7/20/09
to Crypto++ Users
I wanted to put that on the wiki, but registration doesn't quite work
for me there...

Here is code to generate private/public key pair and then sign and
verify a message.

using namespace CryptoPP;

AutoSeededRandomPool rng;

// Generating private key
ECIES<ECP>::PrivateKey privateKey;
privateKey.Initialize(rng, ASN1::secp160r1());

// Generating matching public key
ECIES<ECP>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);

// Signing a message
ECDSA<ECP>::Signer signer(privateKey);
std::string message="message";
std::string signature;

StringSource(message, true, new SignerFilter(rng, signer, new
StringSink(signature)));
// signature contains bytes, not ascii string. Just use base32 or hex
encoder if needed.

// Verifying message
ECDSA<ECP>::Verifier verifier(publicKey);
bool isValid = verifier.VerifyMessage((const byte*)message.data(),
message.size(), (const byte*)signature.data(), signature.size());


Of course for this to be usefull you'll need to load and save keys:

Saving:
ECIES<ECP>::PrivateKey privateKey;
// generate key ...

std::string tmp;
StringSink sink(tmp);
privateKey.Save(sink);

std::string base32encodedKey;
StringSource(tmp, true, new Base32Encoder(new StringSink
(base32encodedKey)));

Loading:
ECIES<ECP>::PrivateKey privateKey;
ByteQueue bq;
StringSource(base32encodedKey, true, new Base32Decoder(new Redirector
(bq)));
privateKey.Load(bq);

Same with public keys.
Reply all
Reply to author
Forward
0 new messages