简体   繁体   English

使用openssl从x509字符数组中提取RSA公钥

[英]Extract RSA public key from a x509 char array with openssl

Here is a certificate in x509 format that stores the public key and the modulo: 这是x509格式的证书,用于存储公钥和模数:

const unsigned char *certificateDataBytes = {/*data*/};

Using OpenSSL and C, how can I convert it into an RSA object? 使用OpenSSL和C,如何将其转换为RSA对象? I've tried several methods but I can't get it to work in RSA_public_encrypt 我尝试了几种方法,但无法在RSA_public_encrypt工作

I think you mean public key into RSA * structure. 我认为您是指将公钥转换为RSA *结构。

Since, you have certificate in bytes, if it is in DER encoded bytes, then you need to first convert it into X509 * structure. 因为,您具有以字节为单位的证书,所以如果以DER编码的字节为单位,则需要首先将其转换为X509 *结构。

 X509 * cert;
 EVP_PKEY * pubkey;
 //length is the length of the certificateDataBytes in terms of bytes.
 cert = d2i_x509 (NULL, certificateDataBytes, length);
 pubkey = X509_get_pubkey (cert);

Please note that if certificate has RSA public key, then you can get RSA public key as follows: 请注意,如果证书具有RSA公钥,则可以按以下方式获取RSA公钥:

 RSA * rsa
 rsa = EVP_PKEY_get1_RSA(pubkey);

 //Now rsa contains RSA public key. Use it.

 //After use, free the pubkey
 EVP_PKEY_free (pubkey);

I hope this must solve your purpose. 我希望这必须解决您的目的。 If certificate encoding is different, use different function. 如果证书编码不同,请使用不同的功能。 Once, you get X509 *, rest step is same. 一旦得到X509 *,其余步骤相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM