沃梦达 / 编程问答 / php问题 / 正文

OpenSSL 不会创建私钥?

OpenSSL won#39;t create private keys?(OpenSSL 不会创建私钥?)

本文介绍了OpenSSL 不会创建私钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我第一次在项目中使用加密.我将我的托管服务提供商用于 SSL,但我也想加密敏感的数据库部分.为此,我被告知使用 OpenSSL.我正在我的本地主机 (WAMP) 上测试它,并且已经安装了 OpenSSL 并打开了 PHP 和 Apache SSL mods.好的,所以我一直在关注教程,并且使用几种建议的方法,已经能够生成公钥并将其存储为文件.出于某种原因,我似乎无法生成私钥.我将发布我尝试过的两个版本的代码:

Okay, well, this is my first time working with encryption on a project. I am using my hosting provider for SSL, but I also want to encrypt portions of the database that are sensitive. For this, I was told to use OpenSSL. I am testing it on my localhost (WAMP), and have installed OpenSSL and turned on the PHP and Apache SSL mods. Okay, so i've been following tutorials and, using several suggested methods, have been able to generate the public key and store it as a file. For some reason, I can't seem to generate the private key. I will post two versions of code that i've tried:

// generate private key
$privateKey = openssl_pkey_new(array(
    'private_key_bits' => 1024,
    'private_key_type' => OPENSSL_KEYTYPE_RSA,
));
// write private key to file
openssl_pkey_export_to_file($privateKey, 'private.key');
// generate public key from private key
$publicKey = openssl_pkey_get_details($privateKey);
// write public key to file
file_put_contents('public.key', $publicKey['key']);
// clear key
echo $privateKey;

?>

这会生成一个 public.key 文件,但为我提供了警告openssl_pkey_export_to_file():无法从参数 1 获取密钥:"和openssl_pkey_get_details() 期望参数 1 为资源,布尔值."

This generates a public.key file, but provides me the warnings "openssl_pkey_export_to_file(): cannot get key from parameter 1:" and " openssl_pkey_get_details() expects parameter 1 to be resource, boolean."

我也尝试了另一种方法:

I also tried an alternative method:

$config = array(
    "config" => "E:/wamp/bin/apache/apache2.2.22/conf/openssl.cnf",
    "digest_alg" => "sha512",
    "private_key_bits" => 1024,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
);

// Create the private and public key
$res = openssl_pkey_new($config);

// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey, NULL);
echo "Private Key: ".$privKey;
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
echo "Public Key: ".$pubKey;
$data = 'plaintext data goes here';
echo "Data: ".$data;
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);
echo "Encrypted: ".$encrypted;
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);

echo "Decrypted: ".$decrypted;

这应该会回显所有内容,不幸的是我的结果是一个空白的私钥,一个很好的公钥,明文和加密文本,并且在尝试解密时出错:openssl_private_decrypt(): key parameter is not a valid private键"

This was supposed to echo everything, unfortunately my result was a blank private key, a fine public key, plaintext, and encrypted text, and an error when trying to decrypt: "openssl_private_decrypt(): key parameter is not a valid private key"

显然,我在创建私钥时遇到了问题.我已经在互联网上进行了彻底的搜索,但仍然无法修复它,即使我已经实现了似乎对其他人都适用的简单代码.

Clearly, i'm having a problem with private key creation. I've searched the internet thoroughly and haven't been able to fix it, even though I've implemented simple code that seems to work for everyone else.

提前致谢,埃利·泽图尼

Thanks in advance, Elie Zeitouni

推荐答案

我认为您可能会更轻松地使用 phpseclib,一个纯 PHP RSA 实现.以下示例将创建一个 1024 位 RSA 私钥/公钥:

I think you might have an easier time with phpseclib, a pure PHP RSA implementation. The following example will create a 1024-bit RSA private / public key:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();

extract($rsa->createKey());

echo $privatekey . '<br/>' . $publickey;
?>

这篇关于OpenSSL 不会创建私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:OpenSSL 不会创建私钥?

基础教程推荐