2017-09-19 128 views
2

我想知道如何在php中籤署文件。我需要簽署一個XML文件。當我執行這段代碼,它給了我下面的錯誤:在PHP中籤名公共x509證書

Warning: openssl_sign(): supplied key param cannot be coerced into a private key in C:\xampp\htdocs\test\index.php on line 35

Warning: openssl_free_key() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\index.php on line 38

的代碼,這是一個:

$data->save("test.xml"); 

$signature; 
$pkeyid = openssl_pkey_get_public("./public.cer"); 
$path = 'test.xml'; 
openssl_sign($path, $signature, $pkeyid); 
openssl_free_key($pkeyid); 

修正: 好了,我有一個誤解的事實我應該使用公共證書籤署數據,並且我沒有正確加載數據。我改變了它,它工作:) 我使用的功能是這一個:

$ pkeyid = openssl_pkey_get_private(file_get_contents('./ private.key'));

+0

手冊中有一些很好的例子,http://php.net/manual/en/function.openssl-sign.php#example-967 –

+0

你的概念錯了。您*使用私鑰簽名*數據,您*使用公鑰驗證*數據 –

回答

1

路徑「./public.cer」很可能是錯誤。嘗試使用絕對路徑。你的第二個警告是告訴你$ pkeyid是布爾值,這正是openssl_pkey_get_public()在錯誤時返回的值。

+0

如果我的證書位於C:/ xampp/htdocs/test中,應如何編寫它? –

+0

file:// C:/xampp/htdocs/test/public.cer – pucky124

+0

歡迎來到StackOverflow,請查看此鏈接以瞭解有關我們社區的更多信息:https://meta.stackexchange.com/questions/126180/is-它可接受到寫-A-感謝您-IN-A-評論 – pucky124