【发布时间】:2013-03-10 07:43:24
【问题描述】:
这是一个 x509 格式的证书,其中存储了公钥和模数:
const unsigned char *certificateDataBytes = {/*data*/};
使用 OpenSSL 和 C,如何将其转换为 RSA 对象?我尝试了几种方法,但无法在 RSA_public_encrypt 中使用
【问题讨论】:
这是一个 x509 格式的证书,其中存储了公钥和模数:
const unsigned char *certificateDataBytes = {/*data*/};
使用 OpenSSL 和 C,如何将其转换为 RSA 对象?我尝试了几种方法,但无法在 RSA_public_encrypt 中使用
【问题讨论】:
我认为你的意思是公钥进入 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 *,其余步骤相同。
【讨论】:
RSA_free释放RSA*!