Extraction of the public key without compressing from the private key compressed in Opensl
When working with cryptographic keys, especially those of the elliptical curve of Cryptosystem (ECC), such as ECDSA, it is essential to understand how to extract the public key without compressing from a private compressed key. In this article, we will deepen the process and provide an example of a code fragment using the Opensl library.
Compressed private key format
The format of the private key table used by the ECC keys is based on the structure of the Curve25519 key, consisting of several fields:
Ec_Point
: The point of the elliptical curve.
Base_string
: a basic chain that contains the compressed public key (more about this later).
Length: The length of the base chain in bytes.
Compressed public key format
The uncompressed public key format is similar, but uses a different basic chain and has some differences:
- Ec_Point
: Another elliptical curve point.
- Base_string
: a basic chain that contains the public key without compressing (more about this later).
- Length: The length of the base chain in bytes.
Extraction of the public key without compressing from the compressed private key
To extract the public key without compressing, you must first decompose the compressed private key using the ECDK
a library an OpenSl library. This will give you a “basic coded chain 64”, which can be decoded to obtain the public key without compressing.
Here is an example of code fragment in C ++:
`CPP
#Iblude
#Include
#include
INT MAIN () {
// Load the private password from a file or buffer
Ec_key* pkey = null;
int ret = ec_Key_new_by_curve_name (nid_secp256k1, null);
if (ret! = 0) {
STD :: Cer << "Private Key Cargo Error" << STD :: Endl;
Return 1;
}
// Decompresses the compressed private password
Unsigned Char* Base64ENC = null; // your private key compressed here
REC = base64_Decode (base64ENC, null);
if (ret! = 0) {
STD :: Cer << "Error delivering the private key" << STD :: Endl;
Ec_key_free (pkey);
Return 1;
}
INT LEN = STRLEN ((Char*) Base64ENC); // Obtain the length of the base chain
Unsen char* not composedbase64 = new char [];
REC = base64_Decode (not composedbase64, null, Len);
if (ret! = 0) {
STD :: Cer << "Error delivering the private key" << STD :: Endl;
eliminate [] base64ENC; // Don't forget to free memory!
Ec_key_free (pkey);
Return 1;
}
// Convert the base without compressing a public key
Unsigned char* publickey = null;
REC = ECDP_Key_from_bytes (& publickey, non -compressedbase64, Len);
If (ret! = 0) {
STD :: Cer << "Error to make the private password to the public key" << STD :: Endl;
eliminate [] base64ENC; // Don't forget to free memory!
Ec_key_free (pkey);
Return 1;
}
// Print the public key without compress
No electro -ecd* pubstr = new charm without equal [256]; // Assign a space for the chain
REC = ECDP_Pub_Key_to_str (Publickey, Pubstr, 256);
if (ret! = 0) {
STD :: Cer << "Error to make the public key to a row" << STD :: Endl;
eliminate [] base64ENC; // Don't forget to free memory!
Ec_key_free (pkey);
Return 1;
}
// release all the assigned memory
eliminate [] base64ENC;
eliminate [] without compressing 64;
Eliminate [] publickey;
STD :: COUT << "PUBLIC KEY WITHOUT COMPRATED:" << Pubstr << STD :: Endl;
// Clean the private key (it is not required in this example)
Ec_key_free (pkey);
Return 0;
}
Keep in mind that you must replace the Base64nc
with its compressed private key.
Example of use of cases
This fragment of the code is just a demonstration of the way of extracting a public key without compressing a private compressed key using OpensSL.