Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用openssl从x509字符数组中提取RSA公钥_C_Openssl - Fatal编程技术网

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

C 使用openssl从x509字符数组中提取RSA公钥,c,openssl,C,Openssl,以下是x509格式的证书,它存储公钥和模: const unsigned char *certificateDataBytes = {/*data*/}; 使用OpenSSL和C,如何将其转换为RSA对象?我尝试过几种方法,但在RSA_public_encrypt中无法使用,我想您指的是RSA*结构中的公钥 X509 * cert; EVP_PKEY * pubkey; //length is the length of the certificateDataBytes in terms

以下是x509格式的证书,它存储公钥和模:

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

使用OpenSSL和C,如何将其转换为RSA对象?我尝试过几种方法,但在RSA_public_encrypt

中无法使用,我想您指的是RSA*结构中的公钥

 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);
因为,您有以字节为单位的证书,如果它是以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*,rest步骤是相同的。

结果是cert变为null。我有ASN.1格式的。那和你的一样,对吗?什么happening@blake305:通常,ASN.1是DER编码的。但是,也可以使用其他编码。有关DER编码示例,请参见。证书变为空表示它可能无效。使用ERR\u print\u errors\u fp(文件*fp)将最后一个错误写入文件。它可能会给出有意义的描述。不要忘记通过调用
RSA\u free
释放
RSA*