8

我正在使用 OpenSSL 的高级EVP_*()函数在我的应用程序中实现加密/解密方案,因此我可以轻松切换使用的实际算法,而无需更改 API 调用。

我可以相对容易地创建一个密钥对:

// dumbed down, no error checking for brevity
EVP_PKEY * pkey;
// can change EVP_PKEY_RSA to something else here
EVP_PKEY_CTX * context = EVP_PKEY_CTX_new_id( EVP_PKEY_RSA, NULL );
EVP_PKEY_keygen_init( ctx );
// could set parameters here
EVP_PKEY_keygen( context, &pkey );
// ...
EVP_PKEY_CTX_free( context );

pkey现在拥有一个密钥,即密钥和公钥。这对于事物的秘密方面来说很好,但显然我只想提取公钥组件以用于事物的公共方面。

我能够找到RSA-specific functions,但没有使用高级EVP_*()API。

帮助?

4

2 回答 2

5

You could use following methods to separate public key and private key for future use.

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

 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
                    unsigned char *kstr, int klen,
                    pem_password_cb *cb, void *u);
EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
                    pem_password_cb *cb, void *u);

 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
                    pem_password_cb *cb, void *u);

 int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
 int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);

For detailed information, please refer to <openssl/pem.h>.

于 2017-03-22T09:17:58.723 回答
2

可能是,您应该使用 i2d_PUBKEY_bio() 或类似功能。

于 2014-12-30T14:53:33.590 回答