好吧,这是我第一次在一个项目上使用加密技术。我正在使用我的托管服务提供商提供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
你检查过函数的返回值吗? – doptimusprime