4

这是存储公钥和模数的 x509 格式的证书:

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

使用 OpenSSL 和 C,如何将其转换为 RSA 对象?我已经尝试了几种方法,但我无法让它工作RSA_public_encrypt

4

1 回答 1

12

我认为您的意思是公钥进入 RSA * 结构。

因为,你有字节证书,如果它是 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);

请注意,如果证书有 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);

我希望这必须解决您的目的。如果证书编码不同,请使用不同的功能。一次,您得到 X509 *,其余步骤相同。

于 2013-03-21T04:04:56.753 回答