Hack Forums
[.NET] [VB.NET] XTEA Encryption [Source] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET] XTEA Encryption [Source] (/showthread.php?tid=815420)

Pages: Pages 1 2


[VB.NET] XTEA Encryption [Source] - d3c0mpil3r - Nov 13, 2010

XTEA Encryption


I am back :)

XTEA is TEA's precursor. This is based on TEA Encryption will little improvements. More info can be found here!

Source :

Code
Public Class XTea
    'Leave a thanks....

    '- decompiler

    Private Shared f_key As UInteger()

    Public Sub New(ByVal password As String)
  f_key = FormatKey(password)
    End Sub

    Public Function Encrypt(ByVal input As String) As String
  If input.Length = 0 Then
    Throw New ArgumentException("Invalid input length!")
  End If

  If input.Length Mod 2 <> 0 Then
    input += ControlChars.NullChar
  End If

  Dim dataBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)

  Dim cipher As String = String.Empty
  Dim tempData As UInteger() = New UInteger(1) {}
  For i As Integer = 0 To dataBytes.Length - 1 Step 2
    tempData(0) = dataBytes(i)
    tempData(1) = dataBytes(i + 1)
    code(tempData, f_key)
    cipher += ConvertUIntToString(tempData(0)) + ConvertUIntToString(tempData(1))
  Next

  Return cipher
    End Function

    Public Function Decrypt(ByVal Data As String) As String
  Dim x As Integer = 0
  Dim tempData As UInteger() = New UInteger(1) {}
  Dim dataBytes As Byte() = New Byte((Data.Length \ 8) * 2 - 1) {}
  For i As Integer = 0 To Data.Length - 1 Step 8
    tempData(0) = ConvertStringToUInt(Data.Substring(i, 4))
    tempData(1) = ConvertStringToUInt(Data.Substring(i + 4, 4))
    decode(tempData, f_key)
    dataBytes(x) = CByte(tempData(0))
    x += 1
    dataBytes(x) = CByte(tempData(1))
    x += 1
  Next
  Dim decipheredString As String = System.Text.ASCIIEncoding.ASCII.GetString(dataBytes, 0, dataBytes.Length)
  If decipheredString(decipheredString.Length - 1) = ControlChars.NullChar Then
    decipheredString = decipheredString.Substring(0, decipheredString.Length - 1)
  End If
  Return decipheredString
    End Function

    Private Shared Function FormatKey(ByVal Key As String) As UInteger()
  If Key.Length <= 5 Then
    Throw New ArgumentException("Key must be between 6 and 16 characters in length")
  End If

  Key = Key.PadRight(16, " "c).Substring(0, 16)

  Dim formattedKey As UInteger() = New UInteger(4) {}

  Dim j As Integer = 0
  For i As Integer = 0 To Key.Length - 1 Step 4
    formattedKey(j) = ConvertStringToUInt(Key.Substring(i, 4))
    j += 1
  Next
  Return formattedKey
    End Function

    Private Shared Sub code(ByVal 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 = &H9E3779B9UI

  For i = 0 To 31
    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

    Private Shared Sub decode(ByVal v As UInteger(), ByVal k As UInteger())
  Dim y As UInteger = v(0)
  Dim z As UInteger = v(1)
  Dim sum As UInteger = &HC6EF3720UI
  Dim delta As UInteger = &H9E3779B9UI

  For i = 0 To 31
    z -= (y << 4 Xor y >> 5) + y Xor sum + k(sum >> 11 And 3)
    sum -= delta
    y -= (z << 4 Xor z >> 5) + z Xor sum + k(sum And 3)
  Next

  v(0) = y
  v(1) = z
    End Sub

    Private Shared Function ConvertStringToUInt(ByVal Input As String) As System.UInt32
  Dim output As UInt32
  output = Convert.ToUInt32(Input(0))
  output += (Convert.ToUInt32(Input(1)) << 8)
  output += (Convert.ToUInt32(Input(2)) << 16)
  output += (Convert.ToUInt32(Input(3)) << 24)
  Return output
    End Function
    Private Shared Function ConvertUIntToString(ByVal Input As System.UInt32) As String
  Dim output As New System.Text.StringBuilder()
  output.Append(Convert.ToChar(Input And &HFF))
  output.Append(Convert.ToChar((Input >> 8) And &HFF))
  output.Append(Convert.ToChar((Input >> 16) And &HFF))
  output.Append(Convert.ToChar((Input >> 24) And &HFF))
  Return output.ToString()
    End Function

End Class

Output :

[img]http://ploader.net/files/743101b5744d2c56f961988109d931e8.gif[/img]


Note : You need to input a password between 6 (or 7 char min.) to 16 (max.) for this to work.

Enjoy!🤝



Big mess there! Sorry guys...


* - DarkShadowz - Nov 13, 2010

Hm, could you try to decrease the amount of code? Just wondering if that was possible as people would like to use this in their crypter, and they would like small stubs xD.


* - tang77 - Nov 13, 2010

Nice... but it's create a lot of chars...


* - This Girl Is On Fire - Nov 13, 2010

How hard would this be to crack? Bruteforce etc..


* - Grieve™ - Nov 13, 2010

very nice decompiler thanks for this!


* - Mr. Anderson - Nov 13, 2010

oh wow this is epic! thanks!


* - d3c0mpil3r - Nov 13, 2010

Thanks everyone, RC6 is what I am working. Hope I can get it working :)

(Nov 13, 2010 07:40 AM)DarkShadowz Wrote:  Hm, could you try to decrease the amount of code? Just wondering if that was possible as people would like to use this in their crypter, and they would like small stubs xD.

I gave the source. Now its up to you how you optimize the code :)

(Nov 13, 2010 09:43 AM)exprezzed Wrote:  How hard would this be to crack? Bruteforce etc..

You can salt the encryption. That will make it even stronger.
But choose a random password or something and you are good to go.


* - D0T. - Nov 13, 2010

(Nov 13, 2010 07:40 AM)DarkShadowz Wrote:  Hm, could you try to decrease the amount of code? Just wondering if that was possible as people would like to use this in their crypter, and they would like small stubs xD.

Just create a DLL, would drastically decrease the amount of code, and would make it easier to feature this in crypters and such.


* - Mr. Anderson - Nov 13, 2010

Can I port this to C#? I will give credits.


* - d3c0mpil3r - Nov 14, 2010

(Nov 13, 2010 11:32 AM)Mr. Anderson Wrote:  Can I port this to C#? I will give credits.

Yes you can.
Convert it in C# or use a DLL.


* - memesbro - Nov 18, 2010

nicee, is this polymorphic?


* - d3c0mpil3r - Nov 19, 2010

(Nov 18, 2010 08:57 PM)Chity Snipa Wrote:  nicee, is this polymorphic?

This isn't a polymorphic encryption.

You can modify it to get a different output every time and call it polymorphic 😝


* - klusbert - Dec 24, 2010

trying to use this to encrypt and decrypt tibia packets.

I set up a test app to just test the functions.
I do Like this.

Key = New UInteger(5) {}
Packet = New UInteger(10) {}

For i As Integer = 0 To 5
Key(i) = i
Next
For i As Integer = 5 To 10
Packet(i) = i
Next
XTea.code(Packet, Key)

This get me overflowed everytime.

EDIT:
Same thing with only string. and I use encrypt.


* - GodWall - Dec 24, 2010

Very nice work as always bro :)


* - d3c0mpil3r - Dec 25, 2010

(Dec 24, 2010 03:05 PM)klusbert Wrote:  trying to use this to encrypt and decrypt tibia packets.
Spoiler (Click to View)
This get me overflowed everytime.

EDIT:
Same thing with only string. and I use encrypt.

Disable overflow checking :
[img]http://ploader.net/files/49d66822732cf18423e5095f9fdc94a5.gif[/img]