Sök…


Syntax

  • EVP_PKEY * EVP_PKEY_new (ogiltig);
  • RSA * RSA_new (ogiltig);
  • int RSA_generate_key_ex (RSA * rsa, int bitar, BIGNUM * e, BN_GENCB * cb);
  • int EVP_PKEY_assign_RSA (EVP_PKEY * pkey, RSA * -tangent);
  • int PEM_write_PrivateKey (FILE * fp, EVP_PKEY * x, const EVP_CIPHER * enc, unsign char * kstr, int klen, pem_password_cb * cb, void * u);
  • int PEM_write_bio_PrivateKey (BIO * bp, EVP_PKEY * x, const EVP_CIPHER * enc, unsigned char * kstr, int klen, pem_password_cb * cb, void * u);
  • EVP_PKEY * PEM_read_PrivateKey (FIL * fp, EVP_PKEY ** x, pem_password_cb * cb, void * u);
  • EVP_PKEY * PEM_read_bio_PrivateKey (BIO * bp, EVP_PKEY ** x, pem_password_cb * cb, void * u);
  • ogiltig EVP_PKEY_free (EVP_PKEY * -tangent);

Generera RSA-nyckel

För att generera en RSA-nyckel måste en EVP_PKEY först allokeras med EVP_PKEY_new :

EVP_PKEY *pkey;
pkey = EVP_PKEY_new();

En exponent för nyckeln behövs också, vilket kräver att du tilldelar ett BIGNUM med BN_new och sedan tilldelar med BN_set_word :

BIGNUM *bn;
bn = BN_new();
BN_set_word(bn, RSA_F4);

För att generera nyckeln, skapa en ny RSA med RSA_new och ring RSA_generate_key_ex :

RSA *rsa;
rsa = RSA_new();
RSA_generate_key_ex(
    rsa,  /* pointer to the RSA structure */
    2048, /* number of bits for the key - 2048 is a good value */
    bn,   /* exponent allocated earlier */
    NULL, /* callback - can be NULL if progress isn't needed */
);

För att tilldela den nyligen genererade nyckeln till EVP_PKEY strukturen, ring EVP_PKEY_assign_RSA :

EVP_PKEY_assign_RSA(pkey, rsa);

RSA strukturen frigörs automatiskt när EVP_PKEY strukturen frigörs. Detta görs med EVP_PKEY_free :

EVP_PKEY_free(pkey);

Spara privat nyckel

En EVP_PKEY kan sparas direkt på disken i flera format. PEM_write_PrivateKey används för att spara EVP_PKEY i ett PEM-format:

FILE *f;
f = fopen("key.pem", "wb");
PEM_write_PrivateKey(
    f,                  /* use the FILE* that was opened */
    pkey,               /* EVP_PKEY structure */
    EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
    "replace_me",       /* passphrase required for decrypting the key on disk */
    10,                 /* length of the passphrase string */
    NULL,               /* callback for requesting a password */
    NULL                /* data to pass to the callback */
);

Om du vill spara en privat nyckel till en BIO använder du PEM_write_bio_PrivateKey :

BIO *bio;
bio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(
    bio,                /* BIO to write the private key to */
    pkey,               /* EVP_PKEY structure */
    EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
    "replace_me",       /* passphrase required for decrypting the key on disk */
    10,                 /* length of the passphrase string */
    NULL,               /* callback for requesting a password */
    NULL                /* data to pass to the callback */
);

Ladda privat nyckel

För att ladda en privat nyckel direkt från disken använder PEM_read_PrivateKey funktionen PEM_read_PrivateKey :

FILE *f;
EVP_PKEY *pkey;
f = fopen("key.pem", "rb");
PEM_read_PrivateKey(
    f,     /* use the FILE* that was opened */
    &pkey, /* pointer to EVP_PKEY structure */
    NULL,  /* password callback - can be NULL */
    NULL   /* parameter passed to callback or password if callback is NULL */
);

För att ladda en privat nyckel från en BIO använder du PEM_read_bio_PrivateKey :

BIO *bio;
bio = BIO_new_mem_buf((void *)input, input_len);
PEM_read_bio_PrivateKey(
    bio,   /* BIO to read the private key from */
    &pkey, /* pointer to EVP_PKEY structure */
    NULL,  /* password callback - can be NULL */
    NULL   /* parameter passed to callback or password if callback is NULL */
);


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow