[英]convert .key to .pem
I need to encrypt with private key and I'm attempting to do so with the following code: 我需要使用私钥进行加密,并且尝试使用以下代码进行加密:
$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
I'm getting the error: openssl_private_encrypt(): key param is not a valid private key in 我收到错误消息:openssl_private_encrypt():密钥参数在其中不是有效的私钥
Do I have to do something to the key file? 我需要对密钥文件做些什么吗? It is binary.
它是二进制的。
First, try converting a key into PEM format using openssl: 首先,尝试使用openssl将密钥转换为PEM格式:
openssl rsa -inform der -in file.key -outform pem -out filekey.pem
Next, extract the private key from the PEM file using openssl_pkey_get_private 接下来,使用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
I don't know of any x509 file format using a .key extension. 我不知道任何使用.key扩展名的x509文件格式。 There's pem (which is the format you need to load into openssl), DER (files may have .der, .crt or .cer extension) or PKCS#12 (.pfx .p12).
有pem(这是您需要加载到openssl中的格式),DER(文件可能具有.der,.crt或.cer扩展名)或PKCS#12(.pfx .p12)。 It's probably DER or PKCS#12:
可能是 DER或PKCS#12:
openssl pkcs12 -in file.key -out file.pem -nodes
...converts a PKCS#12 to file.pem, ...将PKCS#12转换为file.pem,
openssl x509 -inform der -in file.key -out file.pem
...converts a DER file. ...转换DER文件。 They'll report an error if it's the wrong file format.
如果文件格式错误,他们将报告错误。 OTOH you could just use the 'file' command to find out the file type.
OTOH,您可以只使用'file'命令来查找文件类型。 (this is assuming that you've got a Posix / Linux system - you didn't say).
(这是假设您具有Posix / Linux系统-您没有说)。
Or you could just ask the person who gave it to you what format it is. 或者,您也可以问问给它的人哪种格式。
Solved I just use the 解决了我只是用
openssl pkcs8 ..... openssl pkcs8 .....
command 命令
hope this is useful to someone else 希望这对其他人有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.