Incorrect EC private key creation

58 views Asked by At

I have written this code to create EC_KEY but unfortunately, when I try to create a PEM format file from it, I get an incorrect result. What am I doing wrong? I am not sure if the Private Key is correct or not

This is my code :

#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/pem.h>

int main(){
    EC_KEY *ec_key = EC_KEY_new();
    EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp384r1);

    if (EC_KEY_set_group(ec_key, group) != 1) {
        printf("SyncServer : Failed to set EC group\n");
        return 0;
    }

    if (EC_KEY_generate_key(ec_key) != 1) {
        printf("SyncServer : Failed to generate EC key\n");
        return 0;
    }

    EVP_PKEY *evp_key = EVP_PKEY_new();
    if (EVP_PKEY_set1_EC_KEY(evp_key, ec_key) != 1) {
        printf("failed to get ec_key!\n");
        return 0;
    }

    FILE *file = fopen("ec_private_key.pem", "w");
    if (file != NULL) {
        if (PEM_write_PrivateKey(file, evp_key, NULL, NULL, 0, NULL, NULL) != 1) {
            printf("Failed to create file\n");
            return 0;
        }
        fclose(file);
        }

     // Cleanup
    EVP_PKEY_free(evp_key);
    return 0;
}

This is what I got :

-----BEGIN PRIVATE KEY-----
MIICDAIBADCCAWQGByqGSM49AgEwggFXAgEBMDwGByqGSM49AQECMQD/////////
/////////////////////////////////v////8AAAAAAAAAAP////8wewQw////
//////////////////////////////////////7/////AAAAAAAAAAD////8BDCz
MS+n4j7n5JiOBWvj+C0ZGB2cbv6BQRIDFAiPUBOHWsZWOY2KLtGdKoXI7dPsKu8D
FQCjNZJqoxmieh0AiWpnc6SCes2scwRhBKqHyiK+iwU3jrHHHvMgrXRuHTtii6eb
mFn3QeCCVCo4VQLyXb9VKWw6VF44cnYKtzYX3kqWJixvXZ6Yv5KS3Cn49B29KJoU
fOnaMRO18LjACmCxzh1+gZ16Qx18kOoOXwIxAP//////////////////////////
/////8djTYH0Ny3fWBoNskiwp3rs7BlqzMUpcwIBAQSBnjCBmwIBAQQw8q0GAqh4
BeAZSu7AzUanABFjIvyVlsJN56M21Ora7O4YbPn7Qb64bTJQjwQSNeiRoWQDYgAE
qdajmPcqDLjcO5MdpwQQ8IbgCa4LthXJC/ERemGVT8+VJ/ZOFjJe9JvH2al/Sz3R
zkiQ38dG7a8cseEEvLDtHhQFnzROBfeZzybLvcZHzEmI5kTZ6mlLQz7NQl7fZM+2
-----END PRIVATE KEY-----
0

There are 0 answers