Porting AuthProtocolCryptographer to VB.NET

11/14/2010 19:00 vDrag0n#1
Sup guys, as I like to program on VB.NET i'm trying to convert this class to VB but when i start a new instance of it:
Dim crypto as New AuthProtocolCryptographer im getting a b0f right here:

i_key1 = CByte((&HF + CByte(i_key1 * &HFA)) * i_key1 + &H13)

Heres the full source, corrections are appreciated:

Code:
Public Class AuthProtocolCryptographer
    Friend Class CryptCounter
        Private m_Counter As UInt16 = 0

        Public ReadOnly Property Key2() As Byte
            Get
                Return CByte(m_Counter >> 8)
            End Get
        End Property

        Public ReadOnly Property Key1() As Byte
            Get
                Return CByte(m_Counter And &HFF)
            End Get
        End Property

        Public Sub Increment()
            m_Counter += 1
        End Sub
    End Class

    Private _decryptCounter As CryptCounter
    Private _encryptCounter As CryptCounter
    Private _cryptKey1 As Byte()
    Private _cryptKey2 As Byte()

    Public Sub New()
        _decryptCounter = New CryptCounter()
        _encryptCounter = New CryptCounter()
        _cryptKey1 = New Byte(255) {}
        _cryptKey2 = New Byte(255) {}
        Dim i_key1 As Byte = &H9D
        Dim i_key2 As Byte = &H62
        For i As Integer = 0 To 255
            _cryptKey1(i) = i_key1
            _cryptKey2(i) = i_key2
            i_key1 = CByte((&HF + CByte(i_key1 * &HFA)) * i_key1 + &H13)
            i_key2 = CByte((&H79 - CByte(i_key2 * &H5C)) * i_key2 + &H6D)
        Next
    End Sub

    Public Sub Encrypt(ByRef buffer As Byte())
        For i As Integer = 0 To buffer.Length - 1
            buffer(i) = buffer(i) Xor CByte(_cryptKey1(_encryptCounter.Key1) Xor _cryptKey2(_encryptCounter.Key2))
            buffer(i) = CByte(buffer(i) >> 4 Or buffer(i) << 4)
            buffer(i) = buffer(i) Xor CByte(&HAB)
            _encryptCounter.Increment()
        Next
    End Sub

    Public Sub Decrypt(ByRef buffer As Byte())
        For i As Integer = 0 To buffer.Length - 1
            buffer(i) = buffer(i) Xor CByte(&HAB)
            buffer(i) = CByte(buffer(i) >> 4 Or buffer(i) << 4)
            buffer(i) = buffer(i) Xor CByte(_cryptKey2(_decryptCounter.Key2) Xor _cryptKey1(_decryptCounter.Key1))
            _decryptCounter.Increment()
        Next
    End Sub
End Class
11/14/2010 19:54 IAmHawtness#2
Project -> Project Properties -> Compile -> Advanced Compile Options -> Remove integer overflow checks

[Only registered and activated users can see links. Click Here To Register...]

Either that or change all your bytes to SBytes (Dim xxx As Byte -> Dim xxx As SByte)
11/14/2010 19:57 vDrag0n#3
I guess the second option is better? but still thanks for the answer i'll try it.
11/14/2010 20:01 IAmHawtness#4
Quote:
Originally Posted by vDrag0n View Post
I guess the second option is better? but still thanks for the answer i'll try it.
Doesn't matter what you use really. Second option is more programatically correct, but both methods do the same so who cares :)