Tiny Encryption Algorithm (TEA) in Visual Basic.NET

by Matthias Broschk April 04, 2009 12:08

I was recently looking for a simple, but efficient encryption algorithm, which I could use for a .NET Compact Framework application on my mobile phone. TEA (Tiny Encryption Algorithm) seemed to be a good choice and I even found a C# implementation on CodeProject.

Since my project was written in VB.NET, I decided to rewrite the code. Since nowadays VB.NET supports unsigned data types and bitwise shifting operators the translation was the rather easy part. The difficult part was understanding the difference in code behavior. The C# code works perfectly whereas the VB.NET implementation raises an integer overflow exception (see the following code).

Private Sub code(ByRef v As System.UInt32(), ByVal k As System.UInt32())
	Dim v0 As System.UInt32 = v(0)
    Dim v1 As System.UInt32 = v(1)
    Dim delta As System.UInt32 = 2654435769
    Dim sum As System.UInt32 = 0

    Dim k0 As System.UInt32 = k(0)
    Dim k1 As System.UInt32 = k(1)
    Dim k2 As System.UInt32 = k(2)
    Dim k3 As System.UInt32 = k(3)
 
    ' This loop causes the integer overflow exception
    For i As Integer = 1 To 32
       sum += delta
       v0 += ((v1 << 4) + k0) Xor (v1 + sum) Xor ((v1 >> 5) + k1)
       v1 += ((v0 << 4) + k2) Xor (v0 + sum) Xor ((v0 >> 5) + k3)
    Next

    v(0) = v0
    v(1) = v1
End Sub

First I tried to use larger data types (System.Int64), but it didn’t work either. Then I remembered the good old times in C++ and the (rare but existing) problems of hidden integer overflows and the mystery of negative numbers.

Basically this is how the overflow works: The largest unsigned 32bit integer is max = 232 – 1 = 4294967295. If you add 1 to max you will get the result 0, which is the smallest unsigned 32bit integer. This is the overflow (starting at the beginning). In C# (like other C languages) this happens without any warning whereas VB.NET has integer-overflow checks by default. Although they are most useful, in this example they prevent the code from working.  Fortunately there is a way to deactivate them.

One more thing: C# is really more advanced regarding handling of overflow checks. There are even the special keywords checked/unchecked which allow to individually (de-)activate integer overflow checking for code fragments.

The full source code class file can be found here.

Tags: , , , ,

Compact Framework | Encryption | Visual Basic.NET

Comments

7/22/2009 1:57:10 AM #

Acai

Thanks - Just the info I was looking for.. My search ends here..

Acai

11/25/2009 2:22:55 AM #

mani

verynice

mani India

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant

About the author

Matthias Broschk from Hamburg (Germany) wrote his first goto statements in QuickBasic at the age of fifteen, switched to Visual Studio (i.e. Visual Basic / Visual C++) at version 5.0 and has been an addict of .NET since its early beginnings. There have been many other languages and frameworks (Java, PHP, ... ), but none about which he has been as enthusiastic as .NET. These days you can find more information in blogs rather than anywhere else. Therefore he has decided to share his experiences and start yet another .NET blog.