13,041,923 members (64,125 online)
alternative version

#### Stats

229.4K views
65 bookmarked
Posted 18 Feb 2004

# Tiny Encryption Algorithm (TEA) for the Compact Framework

, 29 Feb 2004
 Rate this:
Learn how to secure sensitive data using TEA encryption.

## Introduction

The Compact Framework omits the main encryption features in the Cryptography namespace to make room for more important features. Fortunately, it is not terribly difficult to implement some sort of cryptography to hide your sensitive data. I wanted to find a small algorithm that was secure and portable. After doing a little searching, I ran across the Tiny Encryption Algorithm (TEA). This algorithm was developed in 1994 by David Wheeler and Roger Needham of Cambridge University. This algorithm is extremely portable, and fast. There has been a successful cryptanalysis performed on the original TEA algorithm which caused the original authors to modify the TEA algorithm. The revised algorithm is called XTEA. There is not much information on this algorithm so there is no guarantee that the XTEA algorithm has not been broken as well. However, this algorithm could still be useful for applications that do not require the highest of security. The original algorithm was developed in C, but constructed in such a way that it is easy to port to other languages, like C#. I was able to port the original C algorithm to C# with minimal changes. I tested the algorithm on the full .NET Framework as well as the .NET Compact Framework and it works great on both platforms with no changes.

## Background

The Tiny Encryption Algorithm works on the principle of paired blocks of data. This makes it a little more challenging to prepare strings for encryption because you need to pass pairs of unsigned integers to the algorithm and then store them in some manner so the data can be recovered at a later point in time. I use some bit shifting to convert between integers and strings, so a little knowledge of number systems will help you out.

## Using the code

Porting the code to C# was the easy part. After porting the C algorithm to C#, I ended up with the following function for encryption:

```private void code(uint[] v, uint[] k)
{
uint y = v[0];
uint z = v[1];
uint sum = 0;
uint delta=0x9e3779b9;
uint 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];
}

v[0]=y;
v[1]=z;
}```

Simple huh? They don't call it tiny for nothing! Here is the decrypt function:

```private void decode(uint[] v, uint[] k)
{
uint n=32;
uint sum;
uint y=v[0];
uint z=v[1];
uint delta=0x9e3779b9;

sum = delta << 5 ;

while(n-->0)
{
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
}

v[0]=y;
v[1]=z;
}```

Note: I only modified what was necessary to get the code to compile. I also formatted the code to make it a little more readable. In the original algorithm, they used an unsigned long for the variables. In C, an unsigned is a 32-bit unsigned integer. In .NET land, the equivalent is an unsigned integer.

Now we have reached the challenging part. To use the algorithm with strings, we have to convert the strings into an acceptable format. Here is a basic run-through of what I did to make use of the algorithm:

• Make the string an even length by adding a space to the end of it if necessary. We need to do this because the algorithm expects pairs of data.
• Convert the string to an array of bytes.
• Loop through the array and pass a pair of values to the encrypt function.
• Convert the two cipher values to strings and append to one long string.

My `Encrypt` function looks something like the following:

```public string Encrypt(string Data, string Key)
{
uint[] formattedKey = FormatKey(Key);

if(Data.Length%2!=0) Data += '\0'; // Make sure array is even in length.
byte[] dataBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Data);

string cipher = string.Empty;
uint[] tempData = new uint[2];
for(int i=0; i<dataBytes.Length; i+=2)
{
tempData[0] = dataBytes[i];
tempData[1] = dataBytes[i+1];
code(tempData, formattedKey);
cipher += ConvertUIntToString(tempData[0]) +
ConvertUIntToString(tempData[1]);
}

return cipher;
}```

The `Decrypt` function basically is just the reverse of the encrypt function:

```public string Decrypt(string Data, string Key)
{
uint[] formattedKey = FormatKey(Key);

int x = 0;
uint[] tempData = new uint[2];
byte[] dataBytes = new byte[Data.Length / 8 * 2];
for(int i=0; i<Data.Length; i+=8)
{
tempData[0] = ConvertStringToUInt(Data.Substring(i, 4));
tempData[1] = ConvertStringToUInt(Data.Substring(i+4, 4));
decode(tempData, formattedKey);
dataBytes[x++] = (byte)tempData[0];
dataBytes[x++] = (byte)tempData[1];
}

string decipheredString =
System.Text.ASCIIEncoding.ASCII.GetString(dataBytes,
0, dataBytes.Length);

// Strip the null char if it was added.
if(decipheredString[decipheredString.Length - 1] == '\0')
decipheredString = decipheredString.Substring(0,
decipheredString.Length - 1);
return decipheredString;
}```
The `ConvertUIntToString` function takes advantage of some shifting and bitwise-anding (&) to convert a 32-bit unsigned integer to a string of length 4. Since a character is 1 byte in length, we can combine 4 characters to make 4 bytes or 32 bits. Gee, that would hold a 32-bit unsigned integer (`uint`) nicely! Wow!
```private string ConvertUIntToString(uint Input)
{
System.Text.StringBuilder output = new System.Text.StringBuilder();
output.Append((char)((Input & 0xFF)));
output.Append((char)((Input >> 8) & 0xFF));
output.Append((char)((Input >> 16) & 0xFF));
output.Append((char)((Input >> 24) & 0xFF));
return output.ToString();
}```

Here is the function to undo what `ConvertUIntToString` does:

```private uint ConvertStringToUInt(string Input)
{
uint output;
output =  ((uint)Input[0]);
output += ((uint)Input[1] << 8);
output += ((uint)Input[2] << 16);
output += ((uint)Input[3] << 24);
return output;
}```

Anding the shifted Input with `0xFF` will cause only 1 byte to be returned.
The sample code includes a sample application for the .NET Framework and the .NET Compact Framework.

## Points of Interest

• The original Tiny Encryption Algorithm can be found here.
• Another site with some information on this algorithm can be found here.
• In depth article on TEA framework

## History

• 29 Feb 2004 - Added XTEA Algorithm to the article and source code.
• 19 Feb 2004 - Original Article.

## Share

 Software Developer (Senior) United States
Check out my blog! http://www.pagebrooks.com

## You may also be interested in...

 First PrevNext
 Not TEA algorithm Lord Veovis28-May-17 4:02 Lord Veovis 28-May-17 4:02
 Doesn't work at all masiton21-Sep-14 5:40 masiton 21-Sep-14 5:40
 My vote of 5 BlckHwk19-Jun-13 12:13 BlckHwk 19-Jun-13 12:13
 Arbitrary decode utility Bug Me Not23-Nov-09 6:36 Bug Me Not 23-Nov-09 6:36
 FormatKey ciu221-Oct-09 3:45 ciu2 21-Oct-09 3:45
 ÄÜÖ == ? MixxerY6-Feb-09 3:21 MixxerY 6-Feb-09 3:21
 Re: ÄÜÖ == ? Member 603819613-Aug-09 23:25 Member 6038196 13-Aug-09 23:25
 very importatnt::: tea encryption with sql server rashad61212-Dec-07 0:59 rashad612 12-Dec-07 0:59
 very importatnt::: tea encryption with sql server rashad61212-Dec-07 0:58 rashad612 12-Dec-07 0:58
 save to XML? tonypigram17-Nov-06 7:08 tonypigram 17-Nov-06 7:08
 Re: save to XML? sandhyas30-Jul-09 0:59 sandhyas 30-Jul-09 0:59
 Major Flaw in the algorythm [modified] jkcode24-Jul-06 8:48 jkcode 24-Jul-06 8:48
 Re: Major Flaw in the algorythm rhinodude3-Dec-06 4:30 rhinodude 3-Dec-06 4:30
 Re: Major Flaw in the algorythm jkcode4-Dec-06 7:38 jkcode 4-Dec-06 7:38
 Re: Major Flaw in the algorythm [modified] Tom Calloway3-Feb-09 3:29 Tom Calloway 3-Feb-09 3:29
 C++ TEA Implementation? nemrac986-Jul-06 4:18 nemrac98 6-Jul-06 4:18
 Endianness D. Emilio Grimaldo Tuñon6-Jul-06 4:08 D. Emilio Grimaldo Tuñon 6-Jul-06 4:08
 A suggestion D. Emilio Grimaldo Tuñon6-Jul-06 2:32 D. Emilio Grimaldo Tuñon 6-Jul-06 2:32
 problem Cyber2217-Apr-06 5:31 Cyber22 17-Apr-06 5:31
 i tried implementing this application in a far more complex application for encrypting and decrypting data... unfortunatelly, a problem occures every time i try decryptiong data from a file (btw, the file contains encrypted data -- obviously, with the same algorithm). there would be one more thing to say... this only occures to specific keys (e.g.: "cheietest1002345") the problem consists in an ArgumentOutOfRangeException in one of this lines: tempData[0] = Util.ConvertStringToUInt(Data.Substring(i, 4)); tempData[1] = Util.ConvertStringToUInt(Data.Substring(i+4, 4)); decode(tempData, formattedKey); dataBytes[x++] = (byte)tempData[0]; dataBytes[x++] = (byte)tempData[1]; any suggestions? please... Man muss die Welt nicht verstehen. Man muss sich darin nur zurecht finden!
 Re: problem Jens S4-Feb-08 6:52 Jens S 4-Feb-08 6:52
 Re: problem Jens S4-Feb-08 8:50 Jens S 4-Feb-08 8:50
 VB.NET Implementation santakdas24-Oct-05 12:37 santakdas 24-Oct-05 12:37
 Re: VB.NET Implementation [modified] meibella17-Jul-07 17:35 meibella 17-Jul-07 17:35
 Re: VB.NET Implementation MatthiasBroschk19-Nov-09 8:21 MatthiasBroschk 19-Nov-09 8:21
 VB.NET Implementation santakdas24-Oct-05 12:35 santakdas 24-Oct-05 12:35
 Re: VB.NET Implementation MatthiasBroschk19-Nov-09 8:22 MatthiasBroschk 19-Nov-09 8:22
 Make the functions static DecksTerrorT23-Feb-05 22:47 DecksTerrorT 23-Feb-05 22:47
 WebServices renatokato24-Nov-04 8:38 renatokato 24-Nov-04 8:38
 Visual Basic Melanie Jenkins3-Jun-04 19:05 Melanie Jenkins 3-Jun-04 19:05
 Help needed sagiflenn21-Apr-04 20:06 sagiflenn 21-Apr-04 20:06
 Windows Desktop <-->PocketPC pscode2-Apr-04 9:24 pscode 2-Apr-04 9:24
 Re: Windows Desktop <-->PocketPC WaveRebel9-Jun-04 8:22 WaveRebel 9-Jun-04 8:22
 Simpler string & byte encryption ? Super Lloyd21-Mar-04 17:43 Super Lloyd 21-Mar-04 17:43
 Re: Simpler string & byte encryption ? SHADYYY29-Apr-07 21:03 SHADYYY 29-Apr-07 21:03
 Re: Simpler string & byte encryption ? SHADYYY6-May-07 8:52 SHADYYY 6-May-07 8:52
 Characters are 2, not 1, bytes Anonymous9-Mar-04 19:27 Anonymous 9-Mar-04 19:27
 Re: Characters are 2, not 1, bytes pbrooks16-Mar-04 5:51 pbrooks 16-Mar-04 5:51
 Re: Characters are 2, not 1, bytes David Piepgrass27-Sep-04 11:16 David Piepgrass 27-Sep-04 11:16
 TEA is vulnerable to crypto analysis... Lars W27-Feb-04 0:01 Lars W 27-Feb-04 0:01
 Re: TEA is vulnerable to crypto analysis... pbrooks27-Feb-04 2:25 pbrooks 27-Feb-04 2:25
 Re: TEA is vulnerable to crypto analysis... Anonymous28-Feb-04 12:29 Anonymous 28-Feb-04 12:29
 Re: TEA is vulnerable to crypto analysis... pbrooks29-Feb-04 7:19 pbrooks 29-Feb-04 7:19
 Re: TEA is vulnerable to crypto analysis... Anonymous10-Mar-04 4:28 Anonymous 10-Mar-04 4:28
 Re: TEA is vulnerable to crypto analysis... Anonymous25-Feb-05 4:31 Anonymous 25-Feb-05 4:31
 Re: TEA is vulnerable to crypto analysis... rhinodude3-Dec-06 5:19 rhinodude 3-Dec-06 5:19
 Re: TEA is vulnerable to crypto analysis... rmikaelian30-Jun-05 12:04 rmikaelian 30-Jun-05 12:04
 Forgot? Cap'n Code26-Feb-04 3:25 Cap'n Code 26-Feb-04 3:25
 Re: Forgot? pbrooks26-Feb-04 16:01 pbrooks 26-Feb-04 16:01
 Nice, but... Mitchell2.24v26-Feb-04 2:29 Mitchell2.24v 26-Feb-04 2:29
 Last Visit: 31-Dec-99 18:00     Last Update: 20-Jul-17 5:51 Refresh 12 Next »