[英]convert .key to .pem
我需要使用私鑰進行加密,並且嘗試使用以下代碼進行加密:
$content = file_get_contents("file.key");
$plaintext = "14d9df79-8c4c-4380-8444-d31e1fd3f78a";
openssl_private_encrypt($plaintext, $encrypted, $content);
$transfer = base64_encode($encrypted);
echo "text encrypted is:" . $transfer; //encrypted string
我收到錯誤消息:openssl_private_encrypt():密鑰參數在其中不是有效的私鑰
我需要對密鑰文件做些什么嗎? 它是二進制的。
首先,嘗試使用openssl將密鑰轉換為PEM格式:
openssl rsa -inform der -in file.key -outform pem -out filekey.pem
接下來,使用openssl_pkey_get_private從PEM文件中提取私鑰。
$key = openssl_pkey_get_private(file_get_contents("filekey.pem"));
$plaintext = "14d9df79-8c4c-4380-8444-d31e1fd3f78a";
openssl_private_encrypt($plaintext, $encrypted, $key);
$transfer = base64_encode($encrypted);
echo "text encrypted is:" . $transfer; //encrypted string
我不知道任何使用.key擴展名的x509文件格式。 有pem(這是您需要加載到openssl中的格式),DER(文件可能具有.der,.crt或.cer擴展名)或PKCS#12(.pfx .p12)。 可能是 DER或PKCS#12:
openssl pkcs12 -in file.key -out file.pem -nodes
...將PKCS#12轉換為file.pem,
openssl x509 -inform der -in file.key -out file.pem
...轉換DER文件。 如果文件格式錯誤,他們將報告錯誤。 OTOH,您可以只使用'file'命令來查找文件類型。 (這是假設您具有Posix / Linux系統-您沒有說)。
或者,您也可以問問給它的人哪種格式。
解決了我只是用
openssl pkcs8 .....
命令
希望這對其他人有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.