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();
// 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);
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 -
Thank you very much!
harry
> >> - Show quoted text -- Hide quoted text -