How to save public key and private key for ECC?

442 views
Skip to first unread message

Harry

unread,
Apr 16, 2007, 12:18:08 AM4/16/07
to Crypto++ Users
Hi, all Crypto++ Users,

I used secp160k1 to create a pair of publick/private key into files,
the length for private.key is 408 byte and for public.key is 428 byte.
How can I convert the file into a 160bit length, that is, 20 byte
length string?

This is the code:
char* seed = "1234567890";
char* privFilename = "Private.key";
char* pubFilename = "Public.key";

CryptoPP::ECIES< CryptoPP::ECP >::PrivateKey PrivateKey;
CryptoPP::ECIES< CryptoPP::ECP >::PublicKey PublicKey;
CryptoPP::AutoSeededRandomPool rng;

rng.Put((byte *)seed, strlen(seed));

PrivateKey.Initialize( rng, CryptoPP::ASN1::secp160k1() );
PrivateKey.MakePublicKey( PublicKey );

CryptoPP::HexEncoder privFile(new CryptoPP::FileSink(privFilename));
PrivateKey.Save(privFile);
privFile.MessageEnd();

CryptoPP::HexEncoder pubFile(new CryptoPP::FileSink(pubFilename));
PublicKey.Save(pubFile);
pubFile.MessageEnd();

Wei Dai

unread,
Apr 16, 2007, 1:16:16 AM4/16/07
to Harry, Crypto++ Users
To minimize the size of public and private keys, what you need to do is
encode only the private exponent of the private key, and the public point of
the public key. Try this:

// save private exponent
PrivateKey.GetPrivateExponent().DEREncode(privFile);

// load private exponent
Integer x;
x.BERDecode(privFile);
PrivateKey.AccessGroupParameters().Initialize(CryptoPP::ASN1::secp160k1());
PrivateKey.SetPrivateExponent(x);

// save public element
PublicKey.GetGroupParameters().GetCurve().EncodePoint(pubFile,
PublicKey.GetPublicElement(), true);

// load public element
ECP::Point p;
PublicKey.AccessGroupParameters().Initialize(CryptoPP::ASN1::secp160k1());
PublicKey.GetGroupParameters().GetCurve().DecodePoint(p, pubFile,
PublicKey.GetGroupParameters().GetCurve().EncodedPointSize(true));
PublicKey.SetPublicElement(p);

Harry

unread,
Apr 16, 2007, 2:49:00 AM4/16/07
to Crypto++ Users
Dear Wei,
Thank you for your quick reply.

The code you posted did a greate job!

Now the length for public.key file is 42 byte, and the length for the
private.key is 46 byte. They are much smaller than before, but i
still don't know why these two length show that we use a ecc 160 bit
crypography. I thought in a very stupid way that a key length of 20
byte shows that we use a 160 bit crypography.

Any suggestion or explanation from you is greatly apprieciated.

Best Wishes,

Harry

> > pubFile.MessageEnd();- Hide quoted text -
>
> - Show quoted text -

Wei Dai

unread,
Apr 16, 2007, 11:42:27 PM4/16/07
to Harry, Crypto++ Users
The sizes are doubled because you're using HexEncoder. For the private key,
you can save a few more bytes by using the Encode() method instead of
DEREncode().

Harry

unread,
Apr 17, 2007, 3:10:33 AM4/17/07
to Crypto++ Users
When I use Encode, now the private key length is 40 byte, when
HexDecode it, it's exactly 20 byte, that is, 160 bit.

Thank you very much!

harry

> >> - Show quoted text -- Hide quoted text -

Reply all
Reply to author
Forward
0 new messages