使用cryptico.js中的密文的openssl_private_decrypt()返回false
我正在服务器上创建一个公钥/私钥,将公钥发送到JavaScript客户端,在那里它使用cryptico.js库加密用户密码: 遵循这篇老文章的一些建议 在cryptico.js中做了两个修改,如同一篇文章中所建议的使用cryptico.js中的密文的openssl_private_decrypt()返回false,openssl,cryptico,Openssl,Cryptico,我正在服务器上创建一个公钥/私钥,将公钥发送到JavaScript客户端,在那里它使用cryptico.js库加密用户密码: 遵循这篇老文章的一些建议 在cryptico.js中做了两个修改,如同一篇文章中所建议的 my.publicKeyFromString = function(string) { var tokens = string.split("|"); var N = my.b64to16(tokens[0]); var E = tokens.length > 1
my.publicKeyFromString = function(string)
{
var tokens = string.split("|");
var N = my.b64to16(tokens[0]);
var E = tokens.length > 1 ? tokens[1] : "03";
var rsa = new RSAKey();
rsa.setPublic(N, E);
return rsa
}
my.encrypt = function(plaintext, publickeystring, signingkey)
{
var cipherblock = "";
try
{
var publickey = my.publicKeyFromString(publickeystring);
cipherblock += my.b16to64(publickey.encrypt(plaintext));
}
catch(err)
{
return {status: "Invalid public key"};
}
return {status: "success", cipher: cipherblock};
}
现在在客户端js中:
var publicKey = '<php echo file_get_contents("public_key")?>';
var encrypted = cryptico.encrypt("plain text", publicKey);
var data = 'encrypted_post_var='+encrypted.chiper;
已解决 最后我找到了一种让它工作的方法:) 在my.encrypt()函数中,我更改了以下行:
cipherblock += my.b16to64(publickey.encrypt(plaintext));
到
在php中,我改变了这一点:
openssl_private_decrypt(base64_decode($_POST["encrypted_post_var"]), $decrypted, $privateKey);
致:
现在工作很好
$private = openssl_pkey_get_private(file_get_contents("private_key"));
//!!! THAT is the problem because here openssl_private_decrypt() return FALSE !!!
openssl_private_decrypt(base64_decode($_POST["encrypted_post_var"]), $decrypted, $privateKey);
cipherblock += my.b16to64(publickey.encrypt(plaintext));
cipherblock = publickey.encrypt(plaintext);
//that return the cipherblock (ciphertext) in hex format instead base64
openssl_private_decrypt(base64_decode($_POST["encrypted_post_var"]), $decrypted, $privateKey);
openssl_private_decrypt(pack('H*',$_POST["encrypted_post_var"]), $decrypted, $privateKey);