【问题标题】:Encrypt with Cryptico.js, Decrypt with OpenSSL使用 Cryptico.js 加密,使用 OpenSSL 解密
【发布时间】:2013-05-12 09:29:43
【问题描述】:

我正在服务器上创建一个公钥/私钥,将密钥发送到 JavaScript 客户端,在那里它对用户密码进行加密。客户端将密码发送到服务器,服务器使用私钥对其进行解密,但密码返回为空。我已经验证了所有支持这种情况的值都是正确的,所以这与加密/解密有关。我哪里错了?

可能是cryptico.js不兼容php openssl?

图书馆信息:

https://github.com/wwwtyro/cryptico

http://www.php.net/manual/en/function.openssl-pkey-new.php

这里是相关代码sn-ps:

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]);

【问题讨论】:

标签: javascript encryption openssl rsa cryptico


【解决方案1】:

cryptico.js 可以与 openssl 一起使用,但我们必须对其进行一些修改。

它不直接识别 pem 格式的公钥(openssl 使用)。我们必须在 php 端提取公钥的 'n' 和 'e' 部分:

$key = openssl_pkey_new(array( 
  'private_key_bits' => 1024,
  'private_key_type' => OPENSSL_KEYTYPE_RSA,
  'digest_alg' => 'sha256'
));

$detail = openssl_pkey_get_details($key);
$n = base64_encode($detail['rsa']['n']);
$e = bin2hex($detail['rsa']['e']);

另外,cryptico.js 硬编码了公钥的“e”部分(参见 api.js 中 publicKeyFromString 的定义),所以我们需要解决这个问题:

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
}

现在我们可以加密字符串了:

var publicKey = "{$n}|{$e}",
    encrypted = cryptico.encrypt("plain text", publicKey);

工作还没有完成。 cryptico.encrypt 的结果不是简单地由 RSA 加密的。实际上,它由两部分组合而成:由 RSA 加密的 aes 密钥,以及由 AES 用该 aes 密钥加密的纯文本密码。如果我们只需要 RSA,我们可以修改 my.encrypt:

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};
}

现在我们可以使用 openssl 解密密码了:

$private = openssl_pkey_get_private("YOUR PRIVATE KEY STRING IN PEM");
// $encrypted is the result of cryptico.encrypt() in javascript side
openssl_private_decrypt(base64_decode($encrypted), $decrypted, $private);
// now $decrypted holds the decrypted plain text

【讨论】:

  • 我得到了一个空字符串 $e = bin2hex($detail['rsa']['e']);。这完全可以预料吗?
  • @JVE999 也许你的 openssl 扩展没有正确安装?如果 php 找不到 openssl.conf,它就无法生成密钥,但仍然能够进行加密/解密。检查us1.php.net/manual/en/openssl.installation.php
  • 我正在尝试自动激活 IP 摄像机 - 我无法修改任何 javascript 代码,因此无法修改加密功能。有什么办法只改变服务器端的东西吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 2019-04-13
  • 2014-04-01
  • 1970-01-01
  • 2022-11-25
  • 2015-02-18
相关资源
最近更新 更多