admin管理员组

文章数量:1416629

I would like to convert this PHP code$a ^ $b(where length of a is 16, b is 32) to javascript code. Here is my implementation.

    var xor = "";
    for (var i = 0; i < a.length; i++) {
        xor += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
    }
    xor = b.substr(-16) + xor;

However, the result not the same. Please help me to figure it out. Thanks.

By the way, here's the part of code I'm working around:

    var secret = "secret";
    var password = "password";
    var chal = "2ba5565a539c57c1ce2356e218faa321";
    var hexchal = php.pack('H32', chal);
    var newchal, newpwd, pappassword;

    newchal = php.pack('H*', php.md5(hexchal + secret));
    newpwd = php.pack("a32", password);
    var xor = "";
    for (var i = 0; i < newchal.length; i++) {
        xor += String.fromCharCode(newchal.charCodeAt(i) ^ newpwd.charCodeAt(i));
    }
    xor = newpwd.substr(-16) + xor;

The corresponding PHP code:

    <?php
    $secret = "secret";
    $password = "password";
    $chal = "2ba5565a539c57c1ce2356e218faa321";

    $hexchal = pack ("H32", $chal);
    $newchal = pack ("H*", md5($hexchal . $secret));

    $newpwd = pack("a32", $password);
    $pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));
    echo $pappassword;
    ?>

Where a and b are newchal and newpwd, respectively. And php.func()s e from /. The expected output is "821f984aa1062e56dbdc8f77454e5eb3".

I would like to convert this PHP code$a ^ $b(where length of a is 16, b is 32) to javascript code. Here is my implementation.

    var xor = "";
    for (var i = 0; i < a.length; i++) {
        xor += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
    }
    xor = b.substr(-16) + xor;

However, the result not the same. Please help me to figure it out. Thanks.

By the way, here's the part of code I'm working around:

    var secret = "secret";
    var password = "password";
    var chal = "2ba5565a539c57c1ce2356e218faa321";
    var hexchal = php.pack('H32', chal);
    var newchal, newpwd, pappassword;

    newchal = php.pack('H*', php.md5(hexchal + secret));
    newpwd = php.pack("a32", password);
    var xor = "";
    for (var i = 0; i < newchal.length; i++) {
        xor += String.fromCharCode(newchal.charCodeAt(i) ^ newpwd.charCodeAt(i));
    }
    xor = newpwd.substr(-16) + xor;

The corresponding PHP code:

    <?php
    $secret = "secret";
    $password = "password";
    $chal = "2ba5565a539c57c1ce2356e218faa321";

    $hexchal = pack ("H32", $chal);
    $newchal = pack ("H*", md5($hexchal . $secret));

    $newpwd = pack("a32", $password);
    $pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));
    echo $pappassword;
    ?>

Where a and b are newchal and newpwd, respectively. And php.func()s e from http://phpjs/. The expected output is "821f984aa1062e56dbdc8f77454e5eb3".

Share Improve this question edited Jun 6, 2012 at 6:23 Trantor Liu asked Jun 6, 2012 at 5:25 Trantor LiuTrantor Liu 9,1668 gold badges47 silver badges66 bronze badges 6
  • 2 Can you post sample input and output (both actual and expected)? – David Harkness Commented Jun 6, 2012 at 5:32
  • @trantor so what's the expected output for the above? – Ja͢ck Commented Jun 6, 2012 at 6:06
  • @trantor with PHP I get f27eeb39d6695c32dbdc8f77454e5eb3 xor 7461746573740000000000000000000000000000000000000000000000000000 == 861f9f5ca51d5c32dbdc8f77454e5eb3 - care to also post a working PHP version? :) – Ja͢ck Commented Jun 6, 2012 at 6:14
  • @Jack I added corresponding PHP code and modified the password. Hope no mistake this time. – Trantor Liu Commented Jun 6, 2012 at 6:25
  • @trantor thanks! I've modified my answer, it produces the correct output – Ja͢ck Commented Jun 6, 2012 at 6:34
 |  Show 1 more ment

2 Answers 2

Reset to default 5

I've used your code almost literally:

String.prototype.xor = function(other)
{
  var xor = "";
  for (var i = 0; i < this.length && i < other.length; ++i) {
      xor += String.fromCharCode(this.charCodeAt(i) ^ other.charCodeAt(i));
  }
  return xor;
}

The loop should stop when the end of either string is reached.

Tested with:

String.prototype.toHex = function() {
    var hex = '', tmp;
    for(var i=0; i<this.length; i++) {
        tmp = this.charCodeAt(i).toString(16)
        if (tmp.length == 1) {
            tmp = '0' + tmp;
        }
        hex += tmp
    }
    return hex;
}

var x = "password\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000";
var y = "\u00f2~\u00eb9\u00d6i\\2\u00db\u00dc\u008fwEN^\u00b3";

x.xor(y).toHex();

Output:

821f984aa162e56dbdc8f77454e5eb3

You forgot to add a check if the length of b exceeds:

if(i < b.length){
    xor += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
}else{
    xor += a.charAt(i);
}

本文标签: Implement PHP string xor with JavaScriptStack Overflow