Problems doing encryption through JavaScript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Feeney

    #1

    Problems doing encryption through JavaScript

    My life was full and happy lately, which just wan't right, so I've ended up
    trying to implement an encryption algorithm through JavaScript! However the
    output I'm getting doesn't match the reference C program I created. The
    input is a sequence of 32-bit integers, a 128 bit key (supplied as four
    32-bit integers) and the number of passes. A separate function takes care of
    converting strings to these integers - four characters per 32-bit integer.
    The issue is NOT null termination of strings in C, I have verified that the
    32-bit integers supplied as input are correct.

    It's the XXTEA algorithm, (see http://www.cix.co.uk/~klockstone/xxtea.pdf
    for the C reference implementation) .

    Can anyone tell me off hand where I'm going wrong? Are there any differences
    in operator precedence between JavaScript and C?

    function xxtea_encrypt (v, k, numPasses)
    { n = v.length;
    var z = v[n - 1];
    var y = v[0];
    var sum = 0;
    var e = 0;
    var DELTA = 0x9e3779b9;

    var m, p, q;

    q = (numPasses == 0 ? (6 + 52 / n):numPasses);

    while (q-- > 0)
    { sum += DELTA;
    e = sum >>> 2 & 3;

    for (p = 0; p < n - 1; p++)
    { y = v[p + 1];
    z = (v[p] += /* MX */ ((z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^
    (sum ^ y) + (k[p & 3 ^ e] ^ z)));
    }
    y = v[0];
    z = (v[n - 1] += /* MX */ ((z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^
    (sum ^ y) + (k[p & 3 ^ e] ^ z)));
    }

    return v;
    }


    Thanks for any help you can offer.
    --
    Bryan


  • Robert

    #2
    Re: Problems doing encryption through JavaScript

    In article <c5jrkh$2g4se$1 @ID-92000.news.uni-berlin.de>,
    "Bryan Feeney" <bfeeney@oceanf ree.net> wrote:
    [color=blue]
    > My life was full and happy lately, which just wan't right, so I've ended up
    > trying to implement an encryption algorithm through JavaScript! However the
    > output I'm getting doesn't match the reference C program I created. The
    > input is a sequence of 32-bit integers, a 128 bit key (supplied as four
    > 32-bit integers) and the number of passes. A separate function takes care of
    > converting strings to these integers - four characters per 32-bit integer.
    > The issue is NOT null termination of strings in C, I have verified that the
    > 32-bit integers supplied as input are correct.[/color]

    Javascript doesn't have 32 bit integer values. All math is done in 64
    bit floating point numbers. This could be a problem for you. I am sure
    others will fill in more details for you.

    Robert

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Problems doing encryption through JavaScript

      Robert <rccharles@my-deja.com> writes:
      [color=blue]
      > Javascript doesn't have 32 bit integer values. All math is done in 64
      > bit floating point numbers. This could be a problem for you. I am sure
      > others will fill in more details for you.[/color]

      It could give some overflow problems, yes. However, it is worth
      noticing that all bit-operations (shifts, and, or, xor) start by
      converting the argument to a 32 bit *signed* integer, and operates on
      it as such.

      I can see a few places where coercion might be in order. The most
      noticable is 52/n, which has a decent chance of not being an
      integer. Use Math.floor to round it down (it should change the number
      of rounds by one!).

      The rest should work (knock wood) except that additions might overflow
      (or underflow) what can be stored in a 32 bit signed integer. Since
      the results are almost all used by bitwise operators later, the result
      should be correct modulo 2^32, but the final results might need to be
      trimmed.

      (I believe the precedence of operators is the same in Javascript as in
      C).

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...