<VS2005 Lock2Key>

Problems compiling? Don't understand the source code? Don't know how to code your feature? Post here.

Moderator: Moderators

Locked
StormDev
Posts: 7
Joined: 2006-01-07 15:56

<VS2005 Lock2Key>

Post by StormDev » 2006-01-07 16:02

Hello
Could somebody show me a working lock2key function for Visual Basic 2005 ? I have compiled some vb 2003 opensource bots, but the lock2key doesnt work :( . Is there a change in system.encoding.default in .net frmwrk 2.0 ?
THX

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2006-01-07 17:31

http://www.dcpp.net/wiki/index.php/LockToKey

Check the above link for examples in a number of languages.
The world is coming to an end. Please log off.

DC++ Guide | Words

Corayzon
Posts: 31
Joined: 2006-01-07 08:13
Location: Melbourne, Australia
Contact:

Post by Corayzon » 2006-01-08 21:42

Also remember that u dont encode the string as ASCII when u send it through the socket.

System.Text.Encoding.ACSII.GetBytes.

Use the system default encoding:

System.Text.Encoding.Default(or System).GetBytes

:roll:

Carraya
Posts: 112
Joined: 2004-09-21 11:43

Post by Carraya » 2006-01-09 03:32

What language do you program in...
<random funny comment>

bastya_elvtars
Posts: 164
Joined: 2005-01-06 08:39
Location: HU
Contact:

Re: <VS2005 Lock2Key>

Post by bastya_elvtars » 2006-01-09 07:32

StormDev wrote:Visual Basic 2005
Hey you, / Don't help them to bury the light... / Don't give in / Without a fight. (Pink Floyd)

Carraya
Posts: 112
Joined: 2004-09-21 11:43

Re: <VS2005 Lock2Key>

Post by Carraya » 2006-01-09 09:19

bastya_elvtars wrote:
StormDev wrote:Visual Basic 2005
UPS...

Here's mine :) For vb.net 2005 thanks todi and tasman for providing initial code...

Code: Select all

    ' Fixed the one from chimera source to work in strict
    'New Lock -> Key function borrowed from the ShadowDC client
    'and adapted to VB.NET with the help of Tasman (Thanks!)
    Private Function LockToKey(ByVal strLock As String, ByVal n As Integer) 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
<random funny comment>

Corayzon
Posts: 31
Joined: 2006-01-07 08:13
Location: Melbourne, Australia
Contact:

Post by Corayzon » 2006-01-09 17:26

Here is an example on how to use the lock2key function posted above:

Please take note that all encoding is Default 'System.Text.Encoding.Default'

Code: Select all

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

Carraya
Posts: 112
Joined: 2004-09-21 11:43

Post by Carraya » 2006-01-10 03:05

Wow why would you do all that work... :)
<random funny comment>

Corayzon
Posts: 31
Joined: 2006-01-07 08:13
Location: Melbourne, Australia
Contact:

Post by Corayzon » 2006-01-10 09:27

Only took 5 mins, and its nothing that special.

StormDev
Posts: 7
Joined: 2006-01-07 15:56

THX

Post by StormDev » 2006-01-10 12:12

Many thanks ! Gonna try it

StormDev
Posts: 7
Joined: 2006-01-07 15:56

hmmm...

Post by StormDev » 2006-01-10 13:22

for me it says that my client has provided invalid key :(
again.... :shock:
(I copy-pasted your code but didnt work)
maybe there is something wrong with my system encoding ?

Corayzon
Posts: 31
Joined: 2006-01-07 08:13
Location: Melbourne, Australia
Contact:

Post by Corayzon » 2006-01-10 20:10

Well, i done a little app with that code i posted before so you can test ur encoding or even find what encoding you need to use.

I want you to try using the Default encoding (Leave checkbox checked) and if this doesnt work, please reply here telling us so.

download

If you find some encoding that works, this is how you implement it:

Code: Select all

Dim Encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(sEncodingName)
Also tell us the name of the encoding you might find that works.

Cheers

Corayzon
Posts: 31
Joined: 2006-01-07 08:13
Location: Melbourne, Australia
Contact:

Post by Corayzon » 2006-01-10 20:20

I just found some encoding that works :D

Windows-1252
Windows-1254

Usage for both encoding types:

Code: Select all

Dim Encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Windows-1252")

Code: Select all

Dim Encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Windows-1254")

StormDev
Posts: 7
Joined: 2006-01-07 15:56

Post by StormDev » 2006-01-11 08:09

so with the default checkbox checked:

*** Connecting to: Blazed.420NET.org:420
*** Connected
Data In: $Lock EXTENDEDPROTOCOL::This_hub_was_written_by_Yoshi::CTRL[¨³uæ] Pk=YnHub
Data Out: $Key zNR° A N±±RR0g/%DCN000%/cA?ÂsNqÓ‚a!‚P±N/%DCN000%/°Ó±b/%DCN096%/cÃ

StormDev
Posts: 7
Joined: 2006-01-07 15:56

Post by StormDev » 2006-01-11 08:12

nor does chimera work on my system... maybe the vs 2005 beta 2 installed previously messed up something ? now i use vb 2005 express, and the new framework 2 was also installed, and everything beta was removed

Carraya
Posts: 112
Joined: 2004-09-21 11:43

Post by Carraya » 2006-01-11 08:18

well I guess you will have to provide some code so we can see where you messed up...
<random funny comment>

StormDev
Posts: 7
Joined: 2006-01-07 15:56

Post by StormDev » 2006-01-11 08:43

no i used Corayzon's app which is good

Corayzon
Posts: 31
Joined: 2006-01-07 08:13
Location: Melbourne, Australia
Contact:

Post by Corayzon » 2006-01-11 09:41

all my code is done with,

Mircosoft Visual Studio 2005 Professional.

And the framework that came with it.

Maybe some other ppl might wanna try that download listed above and check if the encoding works in their installed framework.

joakim_tosteberg
Forum Moderator
Posts: 587
Joined: 2003-05-07 02:38
Location: Sweden, Linkoping

Post by joakim_tosteberg » 2006-01-11 09:47

I did just do a test here and for me it works just fine with window 1252 and 1254.
StormDev: You did untick the default box before trying the windows encodings?

Carraya
Posts: 112
Joined: 2004-09-21 11:43

Post by Carraya » 2006-01-12 04:06

The code I provided is actually in use in SpyBot and I think Todi also implemented my small changes to Chimera after updating it to VS 2005, but I'm not sure...
<random funny comment>

StormDev
Posts: 7
Joined: 2006-01-07 15:56

Post by StormDev » 2006-01-12 09:29

yea I unticked... i think a system reinstall would solve it ;)

Locked