Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

tiny encryption using matlab

Status
Not open for further replies.

usulkar

Newbie level 4
Newbie level 4
Joined
Feb 24, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
50
i want matlab code for tiny encryption. if anyone helps me i would be greatful.
 

what type of encryption do u want and to which type of signal??....u need to specify some more details..
 

thank you for ur reply .. actually tiny encryption code is available in c language on net .and i want matlab code for it .

- - - Updated - - -

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 ;
}

 
Last edited by a moderator:

well I am not aware of the tiny algorithm..
after looking at your code it looks like u need to initialize arrays first.
what does the teacode and tea_decode do??they are the functions defined...u need to show me the program of these functions.
it looks like a simple code after seeing the while loops.
for example
Code:
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;

you need to do the same thing for other loop and convert it.you need to check the teacode and teadecode function and convert it.its a simple process.
 
hello,thank u for the reply. i have pasted the tea_code and decode routine at the end of that code only . pls could u write a matlab code for it as i m new to matlab. i m trying to write it but getting errors.
 

Instead of asking others to do your work you should show the Matlab code you wrote so far.
 

well if you can show the matlab code as fvm has pointed...i can point out the error.its a simple program...if u can do it in C...i wonder why u cannot do it in matlab...send me the code...the code i have sent u is the tea_code program..
 

hi.. i m showing u the matlab code i have written pls help me to correct the errors.. i have debugged the errors in c .. of the same prog but the program is not running i m not getting wat the prob is..

Code:
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 ;
}
 

To use Matlab you should learn the syntax. '0x12345678' is a string, not a hexadecimal literal. Use hex2dec() in case of doubt.
 
This code will be help full.
This is encryption code of 8 bit
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
 
why are u giving v(1) and v(2)...you are indexing them...if they are varaible names give them as v1 and v2....I have shown a sample program...write it as like this

Code:
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;
 

Sorry, TEA does not work in MATLAB, i attached a jave file in which this code is working.... Hardware designing of TEA in VHDL is our semester project. We tried many times to first test it in MATLAB but all in vain, so don't waste your time in MATLAB coding of TEA, it will gives you constant output.
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

- - - Updated - - -
 

Attachments

  • TEA.zip
    822 bytes · Views: 110
Sorry, TEA does not work in MATLAB.
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.
 
The MATLAB code i posted is working but it will give you constant values. If you change the v1 and v2 values the results will be same, but java code is working.
 
thank you very much for ur help and reply .i also tried it many times in matlab but as u said all in vain, i was thinking whether i m only not able to code it so far. thank you very much once again.
 

The problem is about reading Matlab documentation and understanding Matlab integer operations.

Standard Matlab integer arithmetic uses saturation instead of wrap around (modulo 2^32 operation). You can e.g. use fix-point package functions accumpos() and accumneg() to perform add and subtract with wrap around.
Code:
% 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
 
Now I have MATLAB TEA code if you need it i will upload it for you.
 

You also find a working Matlab TEA code in my previous post.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top