我使用以下命令生成CSR:
openssl req -nodes -newkey rsa:2048 -keyout mytestserver.key -out mytestserver.csr
我尝试使用以下PHP代码解码mytestserver.csr
:
<?php
$mydata="-----BEGIN CERTIFICATE REQUEST-----
MIICyDCCAbACAQAwgYIxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJOSDEQMA4GA1UE
BxMHQ29uY29yZDESMBAGA1UEChMJTXljb21wYW55MQswCQYDVQQLEwJJVDETMBEG
A1UEAxMKbXl0ZXN0LmNvbTEeMBwGCSqGSIb3DQEJARYPdGVzdGVyQHRlc3QuY29t
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3CcljODQKIJQ9xnPFydW
E6fRphmfxtDZF46/J6jT7ctzG3Mv//8p9ene60BCDIWdWiDXvUUUc02nb/qiiaP5
lALl6XeLKOKs6ZEC/lknDfgOBSjqvLz+UzlOXjRdhBpaawaEbVw4rh6idHWIOFGT
lzuHmE8YBmJ7R+tfhmEETk5XtPRZ3RL173HH2R/f2YCh0uhMr4GqXVfHtWrkpiw7
gXjvaXFeQhslTSP7Pru8j39uYKqubwLTyJ1uoqHVVOIqmswz+WfSvn+c86VK44p0
oQQnFL1gHoUWBHSbTmU49ufOWog9oLHOo9GUnMRg9TntGL0s55rfmSTZ+qtyB8Lc
7wIDAQABoAAwDQYJKoZIhvcNAQEEBQADggEBALglJeJUBEmxTy1hE0jG4cPAaJe5
FOqaVCUDm4YeoTpLfYLgE7ZkWuqkhZdw4TR9wu4DkiYo9z94avrKzFj2Kq1JHjUX
weYAHzGxtFO2IUuz7DSGfSvfJ0mMkAH+QtaxNUV2olnrdA8T5hRzgbZT2RTCc0GV
iSyCKXj4O/yRt0lbBCdM/Lja3Yga2BrFtvcsqnaDO6kIj+x1YCpL3cpRsm+3XbvH
dE02+hB/iHC0bE4lQciPCASOiXbMWASWxofFjyRCP+Ha+np/FqoxRF6DIUu10dCS
wV2kkbQQhNsFuMzCJvYudsmG1ULUfjUTvCAgO9kdBweNg3nklzetoSWoE5k=
-----END CERTIFICATE REQUEST-----";
print ("\r\n begin");
print_r(openssl_x509_parse($mydata));
print ("\r\n end");
但它给出了一个空洞的结果。
如何使用PHP的OpenSSL库解码我生成的RSA CSR?
答案 0 :(得分:3)
openssl_x509_parse()
用于“解析X509证书”。要解析证书签名请求,您可以使用openssl_csr_get_subject()
和openssl_csr_get_public_key()
。
要检索公钥详情,您可以使用openssl_pkey_get_details()
:
$keyres = openssl_csr_get_public_key($mydata);
$key = openssl_pkey_get_details($keyres);
echo "$key[bits] bit public key follows:\n$key[key]\n";