Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include <stdio.h> void main (int artgc, char **argv, char **env) { unsigned long v[2]; /* Plaintext */ unsigned long k[4]; /* Key */ unsigned long w[2]; /* cipher text */ /* Input to Plain Text */ v[0] = 0x12345678; v[1] = 0x33333333; printf("\n\n\nInput Data: "); printf (" v = 0x%x %x\n\n", v[0], v[1]); /* Key */ k[0] = 0x00000000; k[1] = 0x80000000; k[2] = 0x80000000; k[3] = 0x80000000; printf ("Key = 0x%x %x %x %x\n\n", k[0], k[1], k[2], k[3]); /* Now call Encode Routine */ tea_code (v, k); printf ("\nEncoded data = 0x%x %x\n\n", v[0], v[1]); /* Now call Decode Routine */ tea_decode (v, k);printf ("\nDecoded data = 0x%x %x\n", v[0], v[1]); } tea_code(long* v, long* k) { /* long is 4 bytes. */ unsigned long v0=v[0], v1=v[1]; unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3]; unsigned long sum=0; unsigned long delta = 0x9e3779b9, n=32 ; while (n-- > 0) { sum += delta ; v0 += (v1<<4)+k0 ^ v1+sum ^ (v1>>5)+k1 ; v1 += (v0<<4)+k2 ^ v0+sum ^ (v0>>5)+k3 ; } v[0]=v0 ; v[1]=v1 ; } tea_decode(long* v, long* k) { unsigned long v0=v[0], v1=v[1]; unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3]; unsigned long n=32, sum, delta=0x9e3779b9 ; sum=delta<<5 ; while (n-- > 0) { v1 -= (v0<<4)+k2 ^ v0+sum ^ (v0>>5)+k3 ; v0 -= (v1<<4)+k0 ^ v1+sum ^ (v1>>5)+k1 ; sum -= delta ; } v[0]=v0 ; v[1]=v1 ; }
sum = 0;
v1_lshift4 = bitsll(v1,4);
v0_rshift4 = bitsrl(v0,4);
v1_lshift5 = bitsll(v1,5);
v0_rshift5 = bitsrl(v0,5);
while n < 33 do
sum = sum + delta;
v0 = v0 + v1_lshift4 + (k0^v1) + (sum^v1_rshift5)+k1;
v1 = v1 + v0_lshift4 + (k0^v1) + (sum^v0_rshift5)+k3;
end_while
v[0]=v0;
v[1]=v1;
clc;
uint32 v[2]; % Plaintext
uint32 k[4]; % Key
uint32 w[2]; % cipher text
% Input to Plain Text */
v(1)='0x12345678'; v(2) ='0x33333333';
display('Input Data:');
display('v = ');display(v(1),v(2));
% Key
k(1) = '0x00000000'; k(2) = '0x80000000';
k(3) = '0x80000000'; k(4) = '0x80000000';
display('key= ');display(k(1), k(2), k(3), k(4));
% Now call Encode Routine */
tea_code (v, k);
display ('\nEncoded data = 0x%x %x\n\n', v(1), v(2)');
% Now call Decode Routine
tea_decode (v, k);
display ('\nDecoded data = 0x%x %x\n', v(1), v(2));
tea_code(ulong* v, ulong* k)
{
% long is 4 bytes. */
uint32 y = v (1); z = v(2);
uint32 sum = '0';
uint32 delta =' 0x9e3779b9';
n=32 ;
while (n-- > 0)
sum += delta ;
y += ((z<<4)+k(1)) ^ (z+sum) ^ ((z>>5)+k(1)) ;
z += ((y<<4)+k(3)) ^ (y+sum) ^ ((y>>5)+k(4)) ;
end
v(1)=y ;
v(2)=z ;
}
tea_decode(ulong* v, ulong* k)
{
uint32 y = v(1); z =v(2) ;
uint32 sum; delta='0x9e3779b9' ;n=32;
sum=delta << 5 ;
while n-- > 0
z -= ((y<<4)+k[3]) ^ (y+sum) ^ ((y>>5)+k[4]) ;
y -= ((z<<4)+k[1]) ^ (z+sum) ^ ((z>>5)+k[2]) ;
sum -= delta ;
end
v(1)=y ;
v(2)=z ;
}
clc
k1 = uint8(11);
k2 = uint8(12);
k3 = uint8(13);
k4 = uint8(14);
sum = uint8(0);
v1 = uint8(4);
v2 = uint8(5);
y = v1;
z = v2;
temp1 = uint8(0);
temp2 = uint8(0);
d=hex2dec('6a');
delta = uint8(d);
x=hex2dec('1f');
y=hex2dec('8e');
n=0;
for n=1:8
sum = sum + delta;
temp1 = bitxor((bitshift(z,4) + k0), (z + sum));
temp2 = (bitshift(z, -5) + k1);
y = y + bitxor(temp1, temp2);
temp1 = bitxor((bitshift(y,4) + k2), (y + sum));
temp2 = (bitshift(y, -5) + k3);
z = z + bitxor(temp1, temp2);
sum = sum - delta;
if(y-z == 0)
1
end
end
y
z
sum = 0;
v1_lshift4 = bitsll(v1,4);
v0_rshift4 = bitsrl(v0,4);
v1_lshift5 = bitsll(v1,5);
v0_rshift5 = bitsrl(v0,5);
while n < 33 do
sum = sum + delta;
v0 = v0 + v1_lshift4 + (k0^v1) + (sum^v1_rshift5)+k1;
v1 = v1 + v0_lshift4 + (k0^v1) + (sum^v0_rshift5)+k3;
end_while
v[0]=v0;
v[1]=v1;
clc
k1 = uint32(11);
k2 = uint32(12);
k3 = uint32(13);
k4 = uint32(14);
sum = uint32(0);
v1 = uint32(14);
v2 = uint32(28);
y = v1;
z = v2;
temp1 = uint32(0);
temp2 = uint32(0);
d=hex2dec('9e3779b9');
g = dec2bin('d');
delta = uint32(d)
x=hex2dec('1f');
y=hex2dec('8e');
n=0;
for n=1:8
sum = sum + delta;
temp1 = bitxor((bitshift(z,4) + k0), (z + sum));
temp2 = (bitshift(z, -5) + k1);
y = y + bitxor(temp1, temp2);
temp1 = bitxor((bitshift(y,4) + k2), (y + sum));
temp2 = (bitshift(y, -5) + k3);
z = z + bitxor(temp1, temp2);
sum = sum - delta;
if(y-z == 0)
1
end
end
y
z
Means: your code doesn't work. (Probably because it doesn't use appropriate 32-Bit integer types). But I agree in so far that there are one hundred ways to code TEA more comfortable than in MATLAB.Sorry, TEA does not work in MATLAB.
% setup key
k0 = uint32(11);
k1 = uint32(12);
k2 = uint32(13);
k3 = uint32(14);
sum = uint32(0);
% test data to encrypt
v0 = uint32(14);
v1 = uint32(28);
delta = uint32(hex2dec('9e3779b9'));
for n=1:32
sum = accumpos(sum,delta);
v0 = accumpos(v0,bitxor(bitxor(accumpos(bitshift(v1,4),k0),accumpos(v1,sum)),accumpos(bitshift(v1, -5),k1)));
v1 = accumpos(v1,bitxor(bitxor(accumpos(bitshift(v0,4),k2),accumpos(v0,sum)),accumpos(bitshift(v0, -5),k3)));
end
% setup sum for decryption
sum=uint32(hex2dec('C6EF3720'));
% decrypt again
for n=1:32
v1 = accumneg(v1,bitxor(bitxor(accumpos(bitshift(v0,4),k2),accumpos(v0,sum)),accumpos(bitshift(v0, -5),k3)));
v0 = accumneg(v0,bitxor(bitxor(accumpos(bitshift(v1,4),k0),accumpos(v1,sum)),accumpos(bitshift(v1, -5),k1)));
sum = accumneg(sum,delta);
end