AuthorMessage
Corayzon
Regular
Posts: 67

Hey guys,
This is a very simple implementation of how to use a Lock2Key function in VB 8.
Please take note that all Encoding is 'System.Text.Encoding.Default'
I just posted this on another forum for someone, so i thought i maise well post it here aswell:
Code:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' This is a routine to test a lock2key function
        Dim Socket As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        ' Connect to server
        Socket.Connect("dope-shit.420net.org", 420)
        ' Wait for data
        Dim bInput(1024) As Byte
        Dim iBytesIn As Integer = Socket.Receive(bInput)
        ' Check for disconnection
        If iBytesIn = 0 Then
            MsgBox("Disconnected")
        End If
        ' Encode data to string
        Dim sPackets As String = System.Text.Encoding.Default.GetString(bInput, 0, iBytesIn)
        ' Split data into packets
        Dim arPackets() As String = Split(sPackets, "|")
        ' Loop through packets
        Dim iPacketCount As Integer = 0
        For iPacketCount = 0 To arPackets.Length - 2
            ' Get the packet from the array
            Dim sData As String = arPackets(iPacketCount)
            ' Split the packet into a command array for interpreting
            Dim arCommand() As String = Split(sData, " ", 2)
            MsgBox(sData)
            ' Do action on command
            Select Case arCommand(0)
                Case "$Lock"
                    ' Make key output buffer
                    Dim bOutput() As Byte
                    bOutput = System.Text.Encoding.Default.GetBytes("$Key " & Me.LockToKey(arCommand(1)) & "|")
                    ' Send key through socket
                    Socket.Send(bOutput)
                    ' Get next data
                    iBytesIn = Socket.Receive(bInput)
                    ' Check for disconnection
                    If iBytesIn = 0 Then
                        MsgBox("Disconnected")
                    Else
                        MsgBox(System.Text.Encoding.ASCII.GetString(bInput, 0, iBytesIn))
                    End If
            End Select
        Next
        ' Close the socket
        Socket.Close()
        Socket = Nothing
        sPackets = Nothing
        arPackets = Nothing
        bInput = Nothing
    End Sub
    Private Function LockToKey(ByVal strLock As String, Optional ByVal n As Integer = 5) As String
        Dim h As Integer
        Dim ub As Integer
        'n = 5 for hub and client locks
        h = strLock.IndexOf(" Pk=")
        If h > 0 Then
            strLock = strLock.Substring(0, h)
        End If
        'The lock only continues to the first space (Pk= comes after)
        'Make sure it is more than 3 characters
        If strLock.Length > 3 Then
            ub = strLock.Length - 1
            'The first character is handled differently from the others
            'h = Asc(strLock) Xor Asc(strLock.Chars(ub)) Xor Asc(strLock.Chars(ub - 1)) Xor n
            h = Asc(strLock.Chars(0)) Xor Asc(strLock.Chars(ub)) Xor Asc(strLock.Chars(ub - 1)) Xor n
            h = ((h * 16) And 240) Or ((h \ 16) And 15)
            'Equivalent of bit shifting four to the left (* 2^4) and four to the right (\ 2^4)
            'Check for illegal characters
            Select Case h
                Case 0, 5, 36, 96, 124, 126
                    LockToKey = "/%DCN" & StrReverse(StrReverse("00" & h).Substring(0, 3)) & "%/"
                    'LockToKey = "/%DCN" & Right$("00" & h, 3) & "%/"
                Case Else
                    LockToKey = Chr(h)
            End Select
            'Now the rest of the characaters in the lock are handled the same
            For n = 1 To ub
                h = Asc(strLock.Chars(n)) Xor Asc(strLock.Chars(n - 1))
                h = ((h * 16) And 240) Or ((h \ 16) And 15)
                Select Case h
                    Case 0, 5, 36, 96, 124, 126
                        LockToKey = LockToKey & "/%DCN" & StrReverse(StrReverse("00" & h).Substring(0, 3)) & "%/"
                    Case Else
                        LockToKey = LockToKey & Chr(h)
                End Select
            Next
        Else
            LockToKey = "Lock string length must be greater than 3 characters."
        End If
        Exit Function
    End Function
End Class

Feel free to ask a question about the DC protocol in here