Click here to Skip to main content
14,493,374 members

Tiny Encryption Algorithm

Rate this:
2.14 (4 votes)
Please Sign up or sign in to vote.
2.14 (4 votes)
18 Jul 2007CPOL
XTEA was designed by David Wheeler and Roger Needham of the Cambridge Computer Laboratory. This implementation is written in VB.NET, but the core algorithm is in C# (referred from

Screenshot - text.jpg


Inspired by this article, I tried to port the code to VB.NET. However, VB.NET does not have any implementation of arithmetic or bit operations, so it is difficult to implement the code / decode function of TEA. Readers can refer to the detailed explanation of XTEA on Wikipedia. I also took the advice from the discussion board to send 8 bytes at a time rather than 2 bytes at a time for encoding. System.bitConverter is used for conversion between UINT32 and String.

Using the Code

The solution contains two projects. ALgoXTEA is written in C# to implement the basic code / decode function:

public static 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];
    v[0]=y; v[1]=z;

In the VB project XTEACryptoVB, you need to add a reference to AlgoXTEA and then declare the function to code / decode with the same signatures as:

Public Declare Sub code Lib "algoXtea.dll" _
    Alias "code" (ByRef v As UInt32(), ByVal k As UInt32())
Public Declare Sub decode Lib "algoXtea.dll" (ByRef v As UInt32(), _
               ByVal k As UInt32()) 

The encryption algorithm is shown below. An instance of the algoXtea class is created, and the code / decode function can be called. 8 bytes of data is sent each time, and the formatKey() function ensures that the key is 16 bits in length:

Public Function encrypt(ByVal Data As String, ByVal key As String) As String
    If Data.Length = 0 Then
        Throw New ArgumentException("Data must be at least 1 characater in length.")
    End If

    Dim formattedKey() As UInt32 = FormatKey(key)
    'make sure data is in length of multiples of 8
    If Data.Length Mod 8 <> 0 Then
        For i As Integer = 0 To (8 - Data.Length Mod 8) - 1
            Data = Data + Chr(0)
        Next i
    End If

    Dim dataBytes As ArrayList
    dataBytes = New ArrayList(Data.Length)
    Dim cipher As String = String.Empty
    Dim tempData(2) As UInt32
    Dim fourBytes(4) As Byte

    For i As Integer = 0 To Data.Length - 1 Step 8
        For k As Integer = 0 To 3
            fourBytes(k) = dataBytes(i + k)
        Next k

       tempData(0) = BitConverter.ToUInt32(fourBytes, 0)

       For k As Integer = 0 To 3
         fourBytes(k) = dataBytes(i + 4 + k)
       Next k

       tempData(1) = BitConverter.ToUInt32(fourBytes, 0)
       Dim al As New AlgoXTEA.algo
       al.code(tempData, formattedKey)
       cipher = cipher + Util.ConvertUintToString(tempData(0))
       cipher = cipher + Util.ConvertUintToString(tempData(1))
    Next i
    Return cipher
End Function

The helper class is in Util.vb, which contains the method to convert between String and UINT32:

Public Shared Function ConvertStringToUint(ByVal input As String) As UInt32
    If input.Length <> 4 Then
        Throw New Exception("String length must be 4 in order to be converted!")
    End If
    Dim byteArray(4) As Byte
    Dim inputArray(4) As Char
    Dim output As UInt32

    inputArray = input.ToCharArray
    'convert to Bytes
    For i As Integer = 0 To 3
        byteArray(i) = Convert.ToByte(inputArray(i))
    Next i

    'convert to uint32
    output = BitConverter.ToUInt32(byteArray, 0)
    Return output

End Function

However, there is a problem with sending encrypted text as a string. If the encrypted character happens to be chr(0), which is a terminating character, it will not work. A solution is to send the hexadecimal representation of the decoded numbers instead of the string.


The file contains the implementation of converting the encoded unsigned integers to a hexadecimal string. Each byte is converted to a hex number and is represented as two characters (0-F) - this will avoid the problem of chr(0).


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


About the Author

United States United States
MS in Computer Science, Novice programmer in .net

Comments and Discussions

GeneralMS version Pin
fatcat77712-Jul-12 1:52
Memberfatcat77712-Jul-12 1:52 
Generaliwant tea ALGORITH IN VS 6.0,VC++ Pin
manucng18-Nov-09 17:04
Membermanucng18-Nov-09 17:04 
Generalcode for c++ language Pin
sweety7825-May-08 7:35
Membersweety7825-May-08 7:35 
GeneralBase 64 Pin
carloscmv23-Jul-07 14:49
Membercarloscmv23-Jul-07 14:49 
You can convert the string to base64 to send through lines. Base 64 use non control ascii characters.

I think than you can use C# or any assembly with a encryption algorith. But for educational purpose this is correct.
QuestionAre you sure? Pin
Bill Seddon23-Jul-07 10:43
MemberBill Seddon23-Jul-07 10:43 
AnswerRe: Are you sure? Pin
Matthew Turner23-Jul-07 13:14
MemberMatthew Turner23-Jul-07 13:14 
GeneralRe: Are you sure? Pin
meibella23-Jul-07 15:01
Membermeibella23-Jul-07 15:01 
GeneralRe: Are you sure? Pin
Matthew Turner23-Jul-07 15:42
MemberMatthew Turner23-Jul-07 15:42 

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.

Posted 18 Jul 2007


30 bookmarked