14,451,179 members

# Tiny Encryption Algorithm

Rate this:
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 http://www.codeproject.com/KB/windows/teaencryption.aspx).

## Introduction

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

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.

## Update

The XTeaHex.zip 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)`.

## About the Author

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

## Comments and Discussions

 First Prev Next
 MS version fatcat77712-Jul-12 2:52 fatcat777 12-Jul-12 2:52
 iwant tea ALGORITH IN VS 6.0,VC++ manucng18-Nov-09 18:04 manucng 18-Nov-09 18:04
 code for c++ language sweety7825-May-08 8:35 sweety78 25-May-08 8:35
 Base 64 carloscmv23-Jul-07 15:49 carloscmv 23-Jul-07 15:49
 Are you sure? Bill Seddon23-Jul-07 11:43 Bill Seddon 23-Jul-07 11:43
 Re: Are you sure? Matthew Turner23-Jul-07 14:14 Matthew Turner 23-Jul-07 14:14
 Re: Are you sure? meibella23-Jul-07 16:01 meibella 23-Jul-07 16:01
 Thank you for comments, as I am a novice, this code is not very well written... It is true that in .Net 2.0 Uinteger has implementations of arithmetic operations. I was using .net 1.1 when I wrote the code http://msdn2.microsoft.com/en-us/library/b6ex274z(vs.71).aspx so there was no bit operation supported for unsigned integer. However, I tried to use.net 2.0 today to modify the code as follow, it gives arithmetic overflow exception... no clue ... Public Sub Code(ByRef v As UInteger(), ByVal k As UInteger()) Dim Y As UInteger = v(0) Dim Z As UInteger = v(1) Dim Sum As UInteger = 0 Dim Delta As UInteger = UInteger.Parse("9E3779B9", Globalization.NumberStyles.AllowHexSpecifier) For N As Integer = 32 To 1 Step -1 Y += (Z << 4 Xor Z >> 5) + Z Xor Sum + k(Sum And 3) Sum += Delta Z += (Y << 4 Xor Y >> 5) + Y Xor Sum + k(Sum >> 11 And 3) Next v(0) = Y v(1) = Z End Sub
 Re: Are you sure? Matthew Turner23-Jul-07 16:42 Matthew Turner 23-Jul-07 16:42
 Last Visit: 20-Feb-20 3:46     Last Update: 20-Feb-20 3:46 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

Article
Posted 18 Jul 2007

56.8K views