TizzyT

TZCipher -TizzyT

Sep 28th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.14 KB | None | 0 0
  1. Public Class TZCipher
  2.     Private _Rotors() As Rotor
  3.     Private _keySize As Integer
  4.     Private _KeyLevel As Integer
  5.     Private _KeyLength As Integer
  6.  
  7.     Public ReadOnly Property KeySize() As Integer
  8.         Get
  9.             Return _keySize
  10.         End Get
  11.     End Property
  12.  
  13.     Public ReadOnly Property Key() As Byte()
  14.         Get
  15.             Dim KL As New List(Of Byte)
  16.             For i = 0 To KeyLevel - 1
  17.                 KL.AddRange(_Rotors(i).Key)
  18.             Next
  19.             Return KL.ToArray
  20.         End Get
  21.     End Property
  22.  
  23.     Public ReadOnly Property KeyLevel() As Integer
  24.         Get
  25.             Return _KeyLevel
  26.         End Get
  27.     End Property
  28.  
  29.     Public Sub New(ByVal Key() As Byte)
  30.         If Key.Length > 0 AndAlso Key.Length Mod 256 = 0 Then
  31.             _keySize = Key.Length * 8
  32.             _KeyLevel = Key.Length / 256
  33.             Dim KL As List(Of Byte) = Key.ToList
  34.             ReDim _Rotors(KeyLevel - 1)
  35.             For i = 0 To _KeyLevel - 1
  36.                 _Rotors(i) = New Rotor(KL)
  37.             Next
  38.             _KeyLength = _Rotors.Length - 1
  39.         Else
  40.             Throw New Exception("Key must be multiple of 256 bytes greater than 0")
  41.         End If
  42.     End Sub
  43.  
  44.     Public Sub New(ByVal Level As UShort)
  45.         If Level = 0 Then Throw New Exception("Encryption Level must be greater than 0")
  46.         _KeyLevel = Level
  47.         _keySize = Level * 256
  48.         Level -= 1
  49.         ReDim _Rotors(Level)
  50.         For i = 0 To Level
  51.             _Rotors(i) = New Rotor
  52.         Next
  53.         _KeyLength = _Rotors.Length - 1
  54.     End Sub
  55.  
  56.     Public Function Process(ByVal [Byte] As Byte) As Byte
  57.         For i = 0 To _KeyLength
  58.             [Byte] = [Byte] Xor _Rotors(i).GetValue
  59.         Next
  60.         For i = 0 To _KeyLength
  61.             If _Rotors(i).Rotate Then Exit For
  62.         Next
  63.         Return [Byte]
  64.     End Function
  65.  
  66.     Public Function Process(ByVal Bytes() As Byte) As Byte()
  67.         For i = 0 To Bytes.Length - 1
  68.             For b = 0 To _KeyLength
  69.                 Bytes(i) = Bytes(i) Xor _Rotors(b).GetValue
  70.             Next
  71.             For r = 0 To _KeyLength
  72.                 If _Rotors(r).Rotate Then Exit For
  73.             Next
  74.         Next
  75.         Return Bytes
  76.     End Function
  77.  
  78.     Public Class Rotor
  79.         Private Shared Rnd As New Random
  80.         Private Pairs(255) As Byte
  81.         Private Indexer As Byte = 0
  82.  
  83.         Public ReadOnly Property Key() As Byte()
  84.             Get
  85.                 Return Pairs
  86.             End Get
  87.         End Property
  88.  
  89.         Public Function GetValue() As Byte
  90.             Return Pairs(Indexer)
  91.         End Function
  92.  
  93.         Public Function Rotate() As Boolean
  94.             If Indexer < 255 Then
  95.                 Indexer += 1
  96.                 Return True
  97.             Else
  98.                 Indexer = 0
  99.                 Return False
  100.             End If
  101.         End Function
  102.  
  103.         Public Sub New(ByRef Key As List(Of Byte))
  104.             For i = 0 To 255
  105.                 Pairs(i) = Key(0)
  106.                 Key.RemoveAt(0)
  107.             Next
  108.             For i = 0 To 255
  109.                 Dim F As Boolean = False
  110.                 For x = 0 To 255
  111.                     If Pairs(x) = i Then
  112.                         F = True
  113.                         Exit For
  114.                     End If
  115.                 Next
  116.                 If Not F Then Throw New Exception("Invalid Key (missing values)")
  117.             Next
  118.         End Sub
  119.  
  120.         Public Sub New()
  121.             For i = 0 To 255
  122.                 Pairs(i) = i
  123.             Next
  124.             Dim V As New List(Of Byte)
  125.             For i = 0 To 255
  126.                 V.Add(i)
  127.             Next
  128.             For i = 0 To 255
  129.                 Dim R As Integer = Rnd.Next(0, 256)
  130.                 Dim Prev As Byte = V(R)
  131.                 V(R) = V(i)
  132.                 V(i) = Prev
  133.             Next
  134.             For i = 0 To 255
  135.                 Dim M As Integer = Rnd.Next(0, V.Count)
  136.                 Dim R As Integer = V(M)
  137.                 Dim P As Byte = Pairs(R)
  138.                 Pairs(R) = Pairs(i)
  139.                 Pairs(i) = P
  140.                 V.RemoveAt(M)
  141.             Next
  142.         End Sub
  143.     End Class
  144. End Class
Advertisement
Add Comment
Please, Sign In to add comment