Advertisement
ZeekoSec

TEA Encryption

Apr 9th, 2015
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.69 KB | None | 0 0
  1. 'Encryption
  2.  
  3. Public Function Encrypt(Data As String, Key As String) As String
  4.     Dim formattedKey As UInteger() = FormatKey(Key)
  5.  
  6.     If Data.Length Mod 2 <> 0 Then
  7.         Data += ControlChars.NullChar
  8.     End If
  9.     ' Make sure array is even in length.
  10.     Dim dataBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
  11.  
  12.     Dim cipher As String = String.Empty
  13.     Dim tempData As UInteger() = New UInteger(1) {}
  14.     For i As Integer = 0 To dataBytes.Length - 1 Step 2
  15.         tempData(0) = dataBytes(i)
  16.         tempData(1) = dataBytes(i + 1)
  17.         code(tempData, formattedKey)
  18.         cipher += ConvertUIntToString(tempData(0)) + ConvertUIntToString(tempData(1))
  19.     Next
  20.  
  21.     Return cipher
  22. End Function
  23.  
  24. 'Decryption
  25.  
  26. Public Function Decrypt(Data As String, Key As String) As String
  27.     Dim formattedKey As UInteger() = FormatKey(Key)
  28.  
  29.     Dim x As Integer = 0
  30.     Dim tempData As UInteger() = New UInteger(1) {}
  31.     Dim dataBytes As Byte() = New Byte(Data.Length / 8 * 2 - 1) {}
  32.     For i As Integer = 0 To Data.Length - 1 Step 8
  33.         tempData(0) = ConvertStringToUInt(Data.Substring(i, 4))
  34.         tempData(1) = ConvertStringToUInt(Data.Substring(i + 4, 4))
  35.         decode(tempData, formattedKey)
  36.         dataBytes(System.Math.Max(System.Threading.Interlocked.Increment(x),x - 1)) = CByte(tempData(0))
  37.         dataBytes(System.Math.Max(System.Threading.Interlocked.Increment(x),x - 1)) = CByte(tempData(1))
  38.     Next
  39.  
  40.     Dim decipheredString As String = System.Text.ASCIIEncoding.ASCII.GetString(dataBytes, 0, dataBytes.Length)
  41.  
  42.     ' Strip the null char if it was added.
  43.     If decipheredString(decipheredString.Length - 1) = ControlChars.NullChar Then
  44.         decipheredString = decipheredString.Substring(0, decipheredString.Length - 1)
  45.     End If
  46.     Return decipheredString
  47. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement