Click here to Skip to main content
13,559,785 members
Click here to Skip to main content
Add your own
alternative version


66 bookmarked
Posted 18 Feb 2004
Licenced CPOL

Tiny Encryption Algorithm (TEA) for the Compact Framework

, 29 Feb 2004
Rate this:
Please Sign up or sign in to vote.
Learn how to secure sensitive data using TEA encryption.

Sample Image - teaencryption.jpg


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.
For more information on how TEA encryption works, refer to the links at the bottom of this article.


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;

        y += (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
        sum += delta;
        z += (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];


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 ;

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


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]) + 

    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 = 
                                                      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


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
Check out my blog!

You may also be interested in...


Comments and Discussions

GeneralRe: TEA is vulnerable to crypto analysis... Pin
Anonymous28-Feb-04 12:29
sussAnonymous28-Feb-04 12:29 
GeneralRe: TEA is vulnerable to crypto analysis... Pin
pbrooks29-Feb-04 7:19
memberpbrooks29-Feb-04 7:19 
GeneralRe: TEA is vulnerable to crypto analysis... Pin
Anonymous10-Mar-04 4:28
sussAnonymous10-Mar-04 4:28 
GeneralRe: TEA is vulnerable to crypto analysis... Pin
Anonymous25-Feb-05 4:31
sussAnonymous25-Feb-05 4:31 
GeneralRe: TEA is vulnerable to crypto analysis... Pin
rhinodude3-Dec-06 5:19
memberrhinodude3-Dec-06 5:19 
GeneralRe: TEA is vulnerable to crypto analysis... Pin
rmikaelian30-Jun-05 12:04
memberrmikaelian30-Jun-05 12:04 
QuestionForgot? Pin
Cap'n Code26-Feb-04 3:25
memberCap'n Code26-Feb-04 3:25 
AnswerRe: Forgot? Pin
pbrooks26-Feb-04 16:01
memberpbrooks26-Feb-04 16:01 
That's because the decrypt message is actually decrypting the contents of the cipher string variable. Textboxes are not capable of storing all characters in a string and therefore will be omitted when you set certain strings to textboxes. The encrypted textbox is just a "visual" of the data. This is why are you able to change the encrypted message and it still decrypts the same message. Sorry for the confusion.

Page Brooks
GeneralNice, but... Pin
Mitchell2.24v26-Feb-04 2:29
memberMitchell2.24v26-Feb-04 2:29 
GeneralRe: Nice, but... Pin
pbrooks26-Feb-04 16:22
memberpbrooks26-Feb-04 16:22 
GeneralRe: Nice, but... Pin
r.j.pieper20-Mar-06 11:30
memberr.j.pieper20-Mar-06 11:30 
GeneralSuperb! Pin
DaNiko25-Feb-04 23:23
memberDaNiko25-Feb-04 23:23 
GeneralRe: Superb! Pin
pbrooks26-Feb-04 16:23
memberpbrooks26-Feb-04 16:23 
GeneralCritic Pin
Super Lloyd25-Feb-04 11:11
memberSuper Lloyd25-Feb-04 11:11 
GeneralRe: Critic Pin
pbrooks25-Feb-04 13:55
memberpbrooks25-Feb-04 13:55 
GeneralRe: Critic Pin
Super Lloyd25-Feb-04 14:28
memberSuper Lloyd25-Feb-04 14:28 
GeneralRe: Critic Pin
D. Emilio Grimaldo Tuñon6-Jul-06 2:26
memberD. Emilio Grimaldo Tuñon6-Jul-06 2:26 
GeneralRe: Critic Pin
Super Lloyd6-Jul-06 2:32
memberSuper Lloyd6-Jul-06 2:32 
GeneralRe: Critic Pin
D. Emilio Grimaldo Tuñon6-Jul-06 3:56
memberD. Emilio Grimaldo Tuñon6-Jul-06 3:56 
GeneralRe: Critic [modified] Pin
Super Lloyd6-Jul-06 4:25
memberSuper Lloyd6-Jul-06 4:25 
GeneralRe: Critic Pin
Super Lloyd6-Jul-06 4:26
memberSuper Lloyd6-Jul-06 4:26 
GeneralGreat ! Pin
Super Lloyd25-Feb-04 10:57
sussSuper Lloyd25-Feb-04 10:57 
GeneralVery good, but how about... Pin
Simon Hughes24-Feb-04 21:52
memberSimon Hughes24-Feb-04 21:52 
GeneralRe: Very good, but how about... Pin
pbrooks25-Feb-04 13:57
memberpbrooks25-Feb-04 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180527.1 | Last Updated 1 Mar 2004
Article Copyright 2004 by pbrooks
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid