如何使用cryptico.js生成的PHP签名进行验证

时间:2014-05-29 10:22:59

标签: javascript php rsa phpseclib cryptico

我尝试在发送到PHP应用程序之前使用javascript签名消息。 PHP应用程序必须检查签名以确保它不是假的。

在javascript中我使用cryptico.js。

这是用于签名消息的js函数

var sign = function(passphrase, text) {
    signingkey = cryptico.generateRSAKey(passphrase, 2048);
    signString = cryptico.b16to64(signingkey.signString(text, "sha256"));
    return signString;
}

这是获取公钥的功能:

var getPublicKey = function(passphrase) {
 var rsaKey = cryptico.generateRSAKey(passphrase, 2048);
 return = cryptico.publicKeyString(rsaKey);
}

例如,对于消息“message”和密码“test2”,公钥和签名是

  

QH / J3 / GVF / h5U02uPyC9Qzn / hHEV5DzB9nFfqk5zbQqHdInVe4sfL + NPA + 4fjLGrBU30Iuvcr + o9paEjzpH5dY48cq6JHqz1RyJ0CQIc2Jr5 + sS4eL1ZIjxWlyN1pKMR + 4aE2rlDAad56Ad1cytiaHuVvyK / gdtbKiuGroSQhJ1EVfZ60m3NIqnqmpi5Zdsnmzny4VH / d66BcGXxGaGaUaqFn0WTypuwIMZMMtzZEK7peKoaW4H4rfkfdrKcD8AaT + z9v5lLGkTl0NcZZ4LN9sSUzsHNfyAFK6cSXo / 73z0tDAlGb5K + yWV6UHoYW1rcoIsxlNRZM6 / 6FYgMXbbfow ==

     

XbF4O6v6oadEQGtdpQ7d54Q2JB9 / ZEXEUH3S1FMn4E / PSqk7HLXjG4tNfuiUBa5eS8kYV49gwC8Yr + mn6YUAHt + K9lHPSsmltWoiHNOaas4aqai9nlyeft4TYYhP + GYbQfw + 3n2TcO39s6M0vw0m0a8AX9JfF02JwCUhP4bu4dzG6Bl5dj000TbUkric14Jyurp8OHmmMvKW62TvXPhNOW39 + wS1Qkfn9Bxmzi8UEVSVe3wP45JWZNgmgeGnpubDhD05FJEDErfVtZ / DRKD81q5YRd4X4cCkeDPDcJLgKW1jkCsA7yBqESXPDSkkrVUM06A9qMFUwk4mRI88fZ8ryQ ==

我问我如何在php中验证它?

我尝试过类似的东西:

$rsa = new Crypt_RSA();
$rsa->loadKey('qH/J3/gvF/h5U02uPyC9Qzn/hHEV5DzB9nFfqk5zbQqHdInVe4sfL+npa+4fjLGrBU30Iuvcr+o9paEjzpH5dY48cq6JHqz1RyJ0CQIc2Jr5+sS4eL1ZIjxWlyN1pKMR+4aE2rlDAad56Ad1cytiaHuVvyK/gdtbKiuGroSQhJ1EVfZ60m3NIqnqmpi5Zdsnmzny4VH/d66BcGXxGaGaUaqFn0WTypuwIMZMMtzZEK7peKoaW4H4rfkfdrKcD8AaT+z9v5lLGkTl0NcZZ4LN9sSUzsHNfyAFK6cSXo/73z0tDAlGb5K+yWV6UHoYW1rcoIsxlNRZM6/6FYgMXbbfow=='); // public key
echo $rsa->verify('message', 'XbF4O6v6oadEQGtdpQ7d54Q2JB9/ZEXEUH3S1FMn4E/PSqk7HLXjG4tNfuiUBa5eS8kYV49gwC8Yr+mn6YUAHt+K9lHPSsmltWoiHNOaas4aqai9nlyeft4TYYhP+GYbQfw+3n2TcO39s6M0vw0m0a8AX9JfF02JwCUhP4bu4dzG6Bl5dj000TbUkric14Jyurp8OHmmMvKW62TvXPhNOW39+wS1Qkfn9Bxmzi8UEVSVe3wP45JWZNgmgeGnpubDhD05FJEDErfVtZ/DRKD81q5YRd4X4cCkeDPDcJLgKW1jkCsA7yBqESXPDSkkrVUM06A9qMFUwk4mRI88fZ8ryQ==') ? 'verified' : 'unverified';

我认为签名和/或公钥的格式不正确。有什么想法吗?

提前谢谢你,

[编辑] 我不确定签名是否正确。如果我使用js函数cryptico.b64to16(signature),签名将会是一些思考:

  

5db1783babfaa1a744406b5da50edde78436241f7f6445c4507dd2d45327e04fcf4aa93b1cb5e31b8b4d7ee89405ae5e4bc918578f60c02f18afe9a7e985001edf8af651cf4ac9a5b56a221cd39a6ace1aa9a8bd9e5c9e7ede1361884ff8661b41fc3ede7d9370edfdb3a334bf0d26d1af005fd25f174d89c025213f86eee1dcc6e81979763d34d136d492b89cd78272baba7c3879a632f296eb64ef5cf84d396dfdfb04b54247e7f41c66ce2f141154957b7c0fe3925664d82681e1a7a6e6c3843d3914910312b7d5b59fc344a0fcd6ae5845de17e1c0a47833c37092e0296d63902b00ef206a1125cf0d2924ad550cd3a03da8c154c24e26448f3c7d9f2bc9

我不确定$ rsa->验证的param键的格式。我试着添加前缀ssh-rsa。但它效果不佳。

所以我尝试了签名格式和密钥。该消息每次都“未经验证”

1 个答案:

答案 0 :(得分:3)

谢谢@neubert,这是PSS签名。

所以这是解决方案。

我用:

  • phpseclib:用于验证消息的PHP lib
  • jsrsasign:JS lib用于签署消息
  • jsencrypt:JS lib创建私钥和公钥

首先,在JS中生成密钥:

crypt = new JSEncrypt({default_key_size: 512});
var key = crypt.getKey();

var publicKey = key.getPublicKey();
var privateKey = key.getPrivateKey();

其次,创建签名:

var rsa = new RSAKey();
rsa.readPrivateKeyFromPEMString(privateKey);
var hSig = rsa.signStringPSS('message', 'sha1');
var signature = linebrk(hSig, 64);
console.log(signature);

默认情况下,签名的格式不正确。我们必须使用函数base64Chars

在base 64中编码hSig
var base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
b16to64 = function(h) {
    var i;
    var c;
    var ret = "";
    if(h.length % 2 == 1)
    {
        h = "0" + h;
    }
    for (i = 0; i + 3 <= h.length; i += 3)
    {
        c = parseInt(h.substring(i, i + 3), 16);
        ret += base64Chars.charAt(c >> 6) + base64Chars.charAt(c & 63);
    }
    if (i + 1 == h.length)
    {
        c = parseInt(h.substring(i, i + 1), 16);
        ret += base64Chars.charAt(c << 2);
    }
    else if (i + 2 == h.length)
    {
        c = parseInt(h.substring(i, i + 2), 16);
        ret += base64Chars.charAt(c >> 2) + base64Chars.charAt((c & 3) << 4);
    }
    while ((ret.length & 3) > 0) ret += "=";
    return ret;
}

完成我们在PHP中验证。我们假设签名和公钥存储在具有相同名称的变量中:

$rsa = new Crypt_RSA();
$rsa->loadKey($publickey); // public key;
echo $rsa->verify('message', base64_decode($signature)) ? 'verified' : 'unverified';