수색…


통사론

  • EVP_PKEY * EVP_PKEY_new (void);
  • RSA * RSA_new (void);
  • int RSA_generate_key_ex (RSA * rsa, int 비트, BIGNUM * e, BN_GENCB * cb);
  • int EVP_PKEY_assign_RSA (EVP_PKEY * pkey, RSA * key);
  • int PEM_write_PrivateKey (FILE * fp, EVP_PKEY * x, const EVP_CIPHER * enc, 부호없는 char * kstr, int klen, pem_password_cb * cb, void * u);
  • int PEM_write_bio_PrivateKey (BIO * bp, EVP_PKEY * x, const EVP_CIPHER * enc, 부호없는 char * kstr, int klen, pem_password_cb * cb, void * u);
  • EVP_PKEY * PEM_read_PrivateKey (FILE * 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);
  • void EVP_PKEY_free (EVP_PKEY * key);

RSA 키 생성

RSA 키를 생성하기 위해에서는, EVP_PKEY 먼저 할당해야 EVP_PKEY_new :

EVP_PKEY *pkey;
pkey = EVP_PKEY_new();

키에 대한 지수는 또한 할당이 필요한 것이다, 필요 BIGNUMBN_new 다음으로 할당 BN_set_word :

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

키를 생성하려면 RSA_new 사용하여 새 RSARSA_new 를 호출 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 */
);

새로 생성 된 키를 EVP_PKEY 구조체에 할당하려면 EVP_PKEY 를 호출 EVP_PKEY_assign_RSA .

EVP_PKEY_assign_RSA(pkey, rsa);

EVP_PKEY 구조체가 해제되면 RSA 구조체가 자동으로 해제됩니다. 이것은 EVP_PKEY_freeEVP_PKEY_free :

EVP_PKEY_free(pkey);

개인 키 저장

EVP_PKEY 는 여러 형식으로 디스크에 직접 저장할 수 있습니다. PEM_write_PrivateKeyEVP_PKEY 를 PEM 형식으로 저장하는 데 사용됩니다.

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 */
);

개인 키를 BIO 저장하려면 PEM_write_bio_PrivateKey 사용 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 */
);

개인 키로드

디스크에서 직접 개인 키를로드하려면 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 */
);

BIO 에서 개인 키를로드하려면 PEM_read_bio_PrivateKey 사용 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
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow