Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bit Packet Encryption
#1
Alright, so this is a basic, easy method of encrypting. What this does is just toggles a few pre-set bits in each character. This can be a useful method to replace XOR because XOR encryption does have some problems such as encrypting a character with the same one.

Nowadays, security is important in everything, even little simple ORPGs. Sure, this can be debunked quite easily, but it should ward off at least the little script kiddies, if not more Smile

So, we are going to introduce a bit toggling function. Add this anywhere in your source (client and server) :
Code:
Public Sub ToggleBit(ByRef initialByte As Byte, ByVal bytLocation As Byte)
initialByte = initialByte Xor (2 ^ bytLocation)
End Sub

Now, what this function does is takes a byte (by reference, so no need to do a call like bytTemporary = togglebit(bytTemporary,5)) and it toggles any bit between 0-7 (basically 1-8).

So how would we use this an encryption method? Well, make a sub (in both the client and server) along these lines :
Code:
Public Function EnDecrypt(ByVal oldData As String) As String
Dim tmpByte() As Byte
Dim I As Byte
tmpByte = StrConv((oldData), vbFromUnicode)

For I = 0 To UBound(tmpByte)
        ToggleBit tmpByte(I), 2
        ToggleBit tmpByte(I), 5

Next I

EncryptData = StrConv(tmpByte, vbUnicode)
Erase tmpByte
End Function

(Note : This is for string packets. If you use byte arrays, don't use StrConv as this just converts a string to a byte array and back to a string.)

What this does is converts the string to a byte array, loops through each byte and toggles bit 2 and 5 (change these bits to different nujmbers, for example, change it to ToggleBit tmpByte(1),4 and Togglebit tmpByte(i),3, or you can toggle more bits, etc.). And, you do not need a seperate function for decrypting, and you can just use the same function for encrypting and decrypting. And it erases the array to prevent memory leaks once it's done with it Smile

So now you're probably wondering how to add this!

On the client side, look for this line :
Code:
frmMirage.Socket.SendData Data

This line is where the packets are sent. And change it to :

Code:
frmMirage.Socket.SendData endecrypt(Data)

This will encrypt the data sent. Now, later we will be encrypting the data the server sends so while we are in the client's source, look for :

Code:
frmMirage.Socket.GetData Buffer, vbString, DataLength
    PlayerBuffer = PlayerBuffer & Buffer
This is where the received data gets added to the player buffer. Now change the second line to :

Code:
PlayerBuffer = PlayerBuffer & enDecrypt(Buffer)

And there you go, now the client will decrypt data that is sent to it.

Now for the server side, look for the line :

Code:
frmServer.Socket(Index).SendData Data

and change it to :

Code:
frmServer.Socket(Index).SendData endecrypt(Data)

This will encrypt the data the server is sending. Now we just have to decrypt the data the server receives. Look for these lines :

Code:
frmServer.Socket(Index).GetData Buffer, vbString, DataLength
            
        Player(Index).Buffer = Player(Index).Buffer & Buffer

and replace the second one with :

Code:
Player(Index).Buffer = Player(Index).Buffer & enDecrypt(Buffer)

and there you go! You now have full encrypting and decrypting, in both directions! Smile Now this isn't going to stop every hacker out there but it's some much needed basic protection to help prevent packet sniffing.

Challenge :
Make it so it encrypts every second letter by toggling different bits Wink

Continuation : (Dave's Idea)

This modification will use lookup tables to speed up encryption quite a bit. In modGlobals, go add the following variable :

Code:
Public BitTable(7) as byte

and in Sub Main (or InitServer) add the following line :

Code:
For i = 0 to 7
    BitTable(i) = 2 ^ i
Next I

and go modify the sub ToggleBit to make it like this :

Code:
Public Sub ToggleBit(ByRef initialByte As Byte, ByVal bytLocation As Byte)
initialByte = initialByte Xor BitTable(bytLocation)
End Sub

This will make encryption must faster as it doesn't always have to raise to the power of the location, because exponents are very slow in VB6 and it is much quicker and more efficient to use a lookup table for this Smile
Reply
#2
Dave Wrote:
Code:
(2 ^ bytLocation)

Raising to the power of something can be very slow. Most sources that do this often will use a lookup table with the values predefined. Might consider that...

Ah, good idea Smile I'll modify the tut with a lookup table Smile Thanks Smile
Reply
#3
Nice Wink

You've got "Public BitTable(7) as byte". If you don't use a base without using the Option Base what happens?
Reply
#4
GIAKEN Wrote:Nice Wink

You've got "Public BitTable(7) as byte". If you don't use a base without using the Option Base what happens?

Well, using 7 without a base starts it at 0, thus making it 0-7, which is 8 values Wink Plus raising 2 to the 0 is 1 and raising 2 to the 1 is two, so they have to go from 0-7 not 1-8 Wink
Reply
#5
I understand that it should start at 0, I was just saying I didn't know what the default base value was if you don't give one. Because you can do "Option Base 0" or "Option Base 1" to set / change the default...
Reply
#6
GIAKEN Wrote:I understand that it should start at 0, I was just saying I didn't know what the default base value was if you don't give one. Because you can do "Option Base 0" or "Option Base 1" to set / change the default...

Unless specified as otherwise, the base of an array is always 0, thus arrays always start at 0 unless option base 1 is set or an array is declared like so :

Code:
Dim bytArr(2 to 10) as byte
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)