我在服務器上創建一個公鑰/私鑰,將密鑰發送到JavaScript客戶端,在其中加密用戶密碼。客戶端將密碼發送到服務器,服務器使用私鑰對其解密,但密碼返回爲空。我已經驗證了支持這種情況的所有值都是正確的,所以這是專門用於加密/解密的東西。我哪裏錯了?使用Cryptico.js加密,使用OpenSSL進行解密
可能是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 - 服務器臨危加密的密碼和attemps解密失敗
openssl_private_decrypt($encryptedPassword, $decryptedPassword, $row[1]);
您使用在服務器端SHA512。不過,我讀到本機Javascript僅支持AES 256位加密,並且在文檔中提供了與aes 256位密鑰的互操作性示例:http://code.google.com/p/crypto-js/#Interoperability – mondjunge 2013-06-25 14:49:41