0
votes

I have a blowfish class that I found on planet source code I believe. I am encrypting a string that is being sent to my server. Then trying to decrypt the string from the data arrvial and I keep getting an error. I'm not sure what is going on or what I am doing wrong. It works great encrypting and decrypting in a text box but for some reason it's not working through winsock. Maybe someone can help me figure this out.

This is the code I have in my client connect event.

Dim crypt As clsBlowFish
Set crypt = New clsBlowFish
Dim strPrivate As String
Dim strUser As String, strUserEncrypted As String
strPrivate = "aDq&s3kUsvVKNm-dRCF2-XJC%5j&%hL5*S5=bu%ZFLsc2D2"

Call crypt.BlowFish(strPrivate)
Call crypt.StringEncrypt(txtUsername.Text, strUserEncrypted)

sOCk.SendData Chr(&H1) & "ENTER" & strUserEncrypted & "CHAT"

On the server side I have this in my data arrival

Dim crypt As clsBlowFish
Set crypt = New clsBlowFish
Dim strPrivate As String
Dim strEncrypted as string, strDecrypted as string
strPrivate = "aDq&s3kUsvVKNm-dRCF2-XJC%5j&%hL5*S5=bu%ZFLsc2D2"

sOCk.GetData strData, vbString

strEncrypted = strData
Call crypt.BlowFish(strPrivate)
Call crypt.StringDecrypt(strEncrypted, strDecrypted)

As soon as the client connects and the server gets the data it gives me an error saying

Run-time error '-2147218101(80040d4b)':

String to decrypt should be sized divisible by 8

The data being sent seems to be working fine. I've displayed the encrypted text in a messagebox before its being sent and it shows as if its encrypted then I sniff the packet as its being sent and its also encrypted so the problem is on the server side.

I've used this same exact code using textboxes to encrypt and decrypt and it worked perfect but sending over winsock doesn't seem to work. Maybe I'm not receiving the data correctly?

Edit: Thanks Remy Lebeau

I fixed it by encrypting all of the text like so.

Call crypt.StringEncrypt(Chr(&H1) & "ENTER" & txtUsername.Text & "CHAT", strUserEncrypted)

1

1 Answers

2
votes

Your server code is not paying attention to the structure of the data it is receiving. Not all of the data is encrypted. The encrypted data is preceded by Chr(&H1) & "ENTER" and followed by "CHAT". The server is currently reading whatever raw data happens to be available in the socket buffer at that exact moment and then decrypting the whole thing as-is. It needs to instead read until it has received Chr(&H1) & "ENTER", then read until it has received "CHAT", and then finally it can decrypt only the data that is between them.