我在服务器上创建一个公钥/私钥,将密钥发送到 JavaScript 客户端,在那里它加密用户密码。客户端将密码发送到服务器,服务器使用私钥对其进行解密,但密码返回为空。我已经验证了所有支持这种情况的值都是正确的,所以这与加密/解密有关。我哪里错了?
可能是 cryptico.js 与 php openssl 不兼容吗?
图书馆信息:
https://github.com/wwwtyro/cryptico
http://www.php.net/manual/en/function.openssl-pkey-new.php
以下是相关的代码片段:
PHP - 创建公钥/私钥
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$res = openssl_pkey_new($config);
// Extract the private key from $res to $privateKey
openssl_pkey_export($res, $privateKey);
// Extract the public key from $res to $publicKey
$publicKey = openssl_pkey_get_details($res);
$publicKey = $publicKey["key"];
JavaScript - 客户端使用公钥加密数据。
var xhr = new XMLHttpRequest();
var data = new FormData();
xhr.open('POST', '/signUp2.php');
data.append('user', User);
var encryptedPassword = cryptico.encrypt(password, localStorage["publicKey"]);
data.append('password', encryptedPassword.cipher);
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4 && xhr.status == 200)
{
var jsonArray = JSON.parse(xhr.responseText);
if(jsonArray[0] == "0")
{
alert("Account created. You may now sign in.");
}
else
alert("Error Code: " + jsonArray[0]);
}
}
xhr.send(data);
PHP - 服务器收到加密密码并尝试解密失败
openssl_private_decrypt($encryptedPassword, $decryptedPassword, $row[1]);