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
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
Comment