...

# Where does increment happen?

fujii13
03-23-2005, 04:34 PM
new to the forums. i'm kinda confused here. and could use your guys' help.

this is a section of code from a base64 decoding/3DES decrypting script i'm working w/. the first lines of this for statement "left" and "right" is what i'm having problems with.

when does incrementation happen in these statements?
for (var j=0; j<iterations; j++) { //either 1 or 3 iterations
left = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++);
right = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++);

temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
temp = ((left >>> 2) ^ right) & 0x33333333; right ^= temp; left ^= (temp << 2);
temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);

//the right side needs to be shifted and to get the last four bits of the left side
temp = (left << 8) | ((right >>> 20) & 0x000000f0);
//left needs to be put upside down
left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
right = temp;

//now go through and perform these shifts on the left and right keys
for (i=0; i < shifts.length; i++) {
//shift the keys either one or two bits to the left
if (shifts[i]) {left = (left << 2) | (left >>> 26); right = (right << 2) | (right >>> 26);}
else {left = (left << 1) | (left >>> 27); right = (right << 1) | (right >>> 27);}
left &= -0xf; right &= -0xf;

//now apply PC-2, in such a way that E is easier when encrypting or decrypting
//this conversion will look like PC-2 except only the last 6 bits of each byte are used
//rather than 48 consecutive bits and the order of lines will be according to
//how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7
lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf]
| pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf]
| pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf]
| pc2bytes6[(left >>> 4) & 0xf];
righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf]
| pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf]
| pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf]
| pc2bytes13[(right >>> 4) & 0xf];
temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff;
keys[n++] = lefttemp ^ temp; keys[n++] = righttemp ^ (temp << 16);
}
} //for each iterations
eg. let's say m=0.

does the incrementation happen at each instance of "m++", so that at the end of left and right, m=8?

does the incrementation happen at the end of each statement only once (for each "left" and "right") making m=2?

does incrementation only happen once per for loop? making m=1?

i have no way of putting an alreat in the middle of these statements, so i haven't been able to find out on my own. any help would be greatly appreciated.

TIA.

joh6nn
03-23-2005, 05:11 PM
so reduce it to its simplest form.

javascript:var key = "this is a test"; var m = 0; left = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++)); void alert(m);

that reveals that m == 4, so incrementation takes place at each step, so after both left and right, m == 8.

out of curiosity, why can't you insert an alert?

fujii13
03-23-2005, 05:31 PM
out of curiosity, why can't you insert an alert?
i meant, i can't insert an alert after each m++, not just at the end of the statement.

so, isn't m technically 3 at the end of the statement, considering calling m in the alert is a "use" (refering to post-incrementation)?

here's my next question. how is this possible?
"temp = ((right >>> -16)"

i understand shifting right 16 or left 16, but how can you shift right in the negative direction? isn't that just shifting to the left?

(trying to convert this script to LotusScript, fyi)

TIA.

joh6nn
03-23-2005, 08:13 PM
so, isn't m technically 3 at the end of the statement, considering calling m in the alert is a "use" (refering to post-incrementation)?

depends on how you want to argue the semantics. in the last charCodeAt(), m==3, but immediately afterwards, and before the alert(), m==4.

math, and particularly this sort of thing, is not my strong suit, so i can't answer your other question. sorry.