Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class TZCipher
- Private _Rotors() As Rotor
- Private _keySize As Integer
- Private _KeyLevel As Integer
- Private _KeyLength As Integer
- Public ReadOnly Property KeySize() As Integer
- Get
- Return _keySize
- End Get
- End Property
- Public ReadOnly Property Key() As Byte()
- Get
- Dim KL As New List(Of Byte)
- For i = 0 To KeyLevel - 1
- KL.AddRange(_Rotors(i).Key)
- Next
- Return KL.ToArray
- End Get
- End Property
- Public ReadOnly Property KeyLevel() As Integer
- Get
- Return _KeyLevel
- End Get
- End Property
- Public Sub New(ByVal Key() As Byte)
- If Key.Length > 0 AndAlso Key.Length Mod 256 = 0 Then
- _keySize = Key.Length * 8
- _KeyLevel = Key.Length / 256
- Dim KL As List(Of Byte) = Key.ToList
- ReDim _Rotors(KeyLevel - 1)
- For i = 0 To _KeyLevel - 1
- _Rotors(i) = New Rotor(KL)
- Next
- _KeyLength = _Rotors.Length - 1
- Else
- Throw New Exception("Key must be multiple of 256 bytes greater than 0")
- End If
- End Sub
- Public Sub New(ByVal Level As UShort)
- If Level = 0 Then Throw New Exception("Encryption Level must be greater than 0")
- _KeyLevel = Level
- _keySize = Level * 256
- Level -= 1
- ReDim _Rotors(Level)
- For i = 0 To Level
- _Rotors(i) = New Rotor
- Next
- _KeyLength = _Rotors.Length - 1
- End Sub
- Public Function Process(ByVal [Byte] As Byte) As Byte
- For i = 0 To _KeyLength
- [Byte] = [Byte] Xor _Rotors(i).GetValue
- Next
- For i = 0 To _KeyLength
- If _Rotors(i).Rotate Then Exit For
- Next
- Return [Byte]
- End Function
- Public Function Process(ByVal Bytes() As Byte) As Byte()
- For i = 0 To Bytes.Length - 1
- For b = 0 To _KeyLength
- Bytes(i) = Bytes(i) Xor _Rotors(b).GetValue
- Next
- For r = 0 To _KeyLength
- If _Rotors(r).Rotate Then Exit For
- Next
- Next
- Return Bytes
- End Function
- Public Class Rotor
- Private Shared Rnd As New Random
- Private Pairs(255) As Byte
- Private Indexer As Byte = 0
- Public ReadOnly Property Key() As Byte()
- Get
- Return Pairs
- End Get
- End Property
- Public Function GetValue() As Byte
- Return Pairs(Indexer)
- End Function
- Public Function Rotate() As Boolean
- If Indexer < 255 Then
- Indexer += 1
- Return True
- Else
- Indexer = 0
- Return False
- End If
- End Function
- Public Sub New(ByRef Key As List(Of Byte))
- For i = 0 To 255
- Pairs(i) = Key(0)
- Key.RemoveAt(0)
- Next
- For i = 0 To 255
- Dim F As Boolean = False
- For x = 0 To 255
- If Pairs(x) = i Then
- F = True
- Exit For
- End If
- Next
- If Not F Then Throw New Exception("Invalid Key (missing values)")
- Next
- End Sub
- Public Sub New()
- For i = 0 To 255
- Pairs(i) = i
- Next
- Dim V As New List(Of Byte)
- For i = 0 To 255
- V.Add(i)
- Next
- For i = 0 To 255
- Dim R As Integer = Rnd.Next(0, 256)
- Dim Prev As Byte = V(R)
- V(R) = V(i)
- V(i) = Prev
- Next
- For i = 0 To 255
- Dim M As Integer = Rnd.Next(0, V.Count)
- Dim R As Integer = V(M)
- Dim P As Byte = Pairs(R)
- Pairs(R) = Pairs(i)
- Pairs(i) = P
- V.RemoveAt(M)
- Next
- End Sub
- End Class
- End Class
Advertisement
Add Comment
Please, Sign In to add comment