简体   繁体   English

将.key转换为.pem

[英]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.

相关问题 将RSA公钥从XML转换为PEM(PHP) - Convert RSA public key, from XML to PEM (PHP) 将pem rsa密钥转换为x#,用于C#RSACryptoServiceProvider.FromXmlString - Convert pem rsa key to xml for C# RSACryptoServiceProvider.FromXmlString PHP:将 DER 私钥转换为 PEM 格式(椭圆曲线) - PHP: Convert DER private key to PEM format (Elliptic Curve) 如何使用 PHP 将 PEM 格式的 RSA 密钥转换为 XML 格式 - How to convert PEM format RSA key into XML format using PHP 将.NET XML格式的DSA非对称密钥转换为PEM格式 - Convert DSA asymmetric key in .NET XML format to PEM format 如何获取Android开发者控制台公钥并将其转换为PEM - How to take Android Developer Console Public Key and convert it to a PEM 是否可以仅使用php openssl函数将.cer和.key文件转换为.pem? - Is it possible to convert .cer and a .key files to .pem by only using php openssl functions? ECDSA:将 60 字节的二进制公钥转换为 PEM 格式,以便与 PHP openssl_verify() 一起使用 - ECDSA: Convert a binary public key of 60 bytes into PEM format for using it with PHP openssl_verify() 将 rsa 密钥转换为 pem 格式 - Convert rsa keys into pem format 使用 PHP 在 Curl 中将.PEM 和.KEY 作为字符串传递 - Passing .PEM and .KEY as string in Curl using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM