-
encryption source
Can someone explain this small encryption source to me? It's the TEA encryption algorithm which I got from: http://vader.brad.ac.uk/tea/tea.shtml
What does: y += (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
and: z += (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
do?? Also why: delta=0x9E3779B9 ?? Thx!
void encipher(const unsigned long *const v, unsigned long *const w, const unsigned long * const k)
{
register unsigned long y=v[0],z=v[1],sum=0,delta=0x9E3779B9,n=32;
while(n-->0)
{
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
sum += delta;
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
}
w[0]=y; w[1]=z;
}
-
I can't say I understood this algoritm well, I could though explain what it does in detail. That is answers to What, not why.
delta is golden ratio ~0.618034*2^32, I am not sure why.
^ stands for bitwise exclusive or (not equal), takes two 32 bit operands and XOR them against each other, each bit independetly according to this scheme:
Xor| 0 1
----------
0 | 0 1
1 | 1 0
& stands for bitwise AND, takes two 32 bit operands and AND each bit independetly against the other. according to this scheme:
And| 0 1
----------
0 | 0 0
1 | 0 1
>> stands for bitshift right, it moves all bits y steps right on x if x>>y; the y hi bits (leftmost) will be filled with 0'es
<< stands for bitshift left, it moves all bits y steps left on x if x<<y; the y low bits (rightmost) will be filled with 0'es
+= assigns and returns sum of y and x to y if y+=x;
+ returns sum of y and x to y if y+x;
z << 4 ^ z >> 5
The 32 bits will be xored like this:
Code:
Left operand
[****00000011111111112222222222333]----
[****45678901234567890123456789012]----
---------[0000000000111111111122222222*****]
---------[0123456789012345678901234567*****]
Right operand
k[sum&3];
k[sum>>11 & 3];
first will take the 3 lowbits (valued 0-7) and retrieve the data at that offset, the second will do the same at bit 12-14 (from right)
sum seems to add up with delta in each loop, so you probably get mixed values at k but there seems to be a point with having the golden rate. Everything is summed up to y resp z, but both affect each other according to second term.
register unsigned long makes sure the compiler allocates the variables in the cpu registers, for optimal performance.
-
Thanx alot! By the way, whats 0x9E3779B9 in decimal?
-
-
Why is this number important and used in this algorithm? When I change it the encryption doesnt work.