04-09-2008, 10:28 PM
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
So, we are going to introduce a bit toggling function. Add this anywhere in your source (client and server) :
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 :
(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
So now you're probably wondering how to add this!
On the client side, look for this line :
This line is where the packets are sent. And change it to :
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 :
This is where the received data gets added to the player buffer. Now change the second line to :
And there you go, now the client will decrypt data that is sent to it.
Now for the server side, look for the line :
and change it to :
This will encrypt the data the server is sending. Now we just have to decrypt the data the server receives. Look for these lines :
and replace the second one with :
and there you go! You now have full encrypting and decrypting, in both directions!
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
Continuation : (Dave's Idea)
This modification will use lookup tables to speed up encryption quite a bit. In modGlobals, go add the following variable :
and in Sub Main (or InitServer) add the following line :
and go modify the sub ToggleBit to make it like this :
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
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

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

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
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!

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

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
