2013-05-31 57 views
5

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

// 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是资源,布尔值。“当试图

$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():关键参数不是有效的私钥”

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

由于提前, 埃利Zeitouni

+0

你检查过函数的返回值吗? – doptimusprime

回答

2

我想你可能有一个更简单的时间phpseclib, a pure PHP RSA implementation。以下示例将创建一个1024位RSA私钥/公钥:

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

$rsa = new Crypt_RSA(); 

extract($rsa->createKey()); 

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

谢谢!这只是解决了我的问题:) –

5

我知道它已经有一段时间,你可能已经解决了这个问题,但我认为我的回答可以是谁面临着同样的问题,其他人有帮助的(像我一样)。

所以,如果你看看openssl_pkey_export()文档,你会发现它有四个不同的参数。如果我们看看你的第二个实现,我们可以看到你只提供了三个。

openssl_pkey_export($ res,$ privKey,NULL);

如果只有您的服务器具有适用于OPENSSL_CONF的环境变量,这将会一样好。链接到openssl.cnf文件(需要使用XAMPP下载的README-SSL.txt文件中引用的文件)才能使用PHP的CSR和密钥生成函数。

要使用PHP中的CSR和密钥生成功能,您需要安装openssl.cnf文件。我们在本文件夹中包含了一个可用于此目的的示例文件,与此自述文件一起。

因为这个原因,就像您为openssl_pkey_new($config);所做的一样,您必须创建一个包含config =>'/path/to/openssl.cnf'的关联数组。就像这样:

openssl_pkey_export($res, $privKey, NULL, $config); 

我想补充一点,在我的情况openssl.cnf中的路径里面:

C:\ XAMPP \ PHP \演员\ OpenSSL的\ openssl.cnf中

在这里你还可以找到一个README-SSL.txt文件,它可以很好地解释如何配置OPENSSL。

希望它有帮助。 :)

3

在xampp php 5.5中使用windows10工作在我的脑海中徘徊。最后计算出的tabone上面的答案是正确的方向除了路径格式需要被正向削减!

C:/xampp/php/extras/openssl/openssl.cnf 

希望这可以帮助别人。

1

use:openssl_pkey_export_to_file($ result,'privkey.pem',null,$ config);