OpenSSL不会创建私钥? [英] OpenSSL won't create private keys?

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

问题描述

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

  //生成私钥
$ privateKey = openssl_pkey_new (array(
'private_key_bits'=> 1024,
'private_key_type'=> OPENSSL_KEYTYPE_RSA,
));
//将私钥写入
openssl_pkey_export_to_file($ privateKey,'private.key');
//从私钥生成公钥
$ publicKey = openssl_pkey_get_details($ privateKey);
//将公钥写入文件
file_put_contents('public.key',$ publicKey ['key']);
//清除键
echo $ privateKey;

?>

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



我还尝试了一种替代方法:

  $ 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,
);

//创建私钥和公钥
$ res = openssl_pkey_new($ config);

//将私钥从$ res提取到$ privKey
openssl_pkey_export($ res,$ privKey,NULL);
echo私钥:$ privKey;
//将公钥从$ res解压到$ pubKey
$ pubKey = openssl_pkey_get_details($ res);
$ pubKey = $ pubKey [key];
echo公钥:$ pubKey;
$ data ='明文数据在这里';
echoData:。$ data;
//使用公钥加密$加密
openssl_public_encrypt($ data,$ encrypted,$ pubKey);
echoEncrypted:。$ encrypted;
//使用私有密钥解密数据,并将结果存储在$ decrypt $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $%

echoDecrypted:$解密;

这应该是回应一切,不幸的是我的结果是一个空白的私钥,一个罚款的公钥,明文和加密文本,尝试解密时出现错误:openssl_private_decrypt():key参数不是有效的私钥



显然,我有一个私钥创建问题。我已经彻底搜索了互联网,并且无法修复它,即使我已经实现了似乎适用于其他人的简单代码。



感谢提前,
Elie Zeitouni

解决方案

我想你可能会更容易的时间与 phpseclib,纯粹的PHP RSA实现。以下示例将创建一个1024位RSA私有/公钥:

 <?php 
include('隐窝/ RSA.php');

$ rsa = new Crypt_RSA();

extract($ rsa-> createKey());

echo $ privatekey。 '< br />'。 $公钥;
?>


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;

?>

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;

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

解决方案

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不会创建私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆