Real Software Forums
http://forums.realsoftware.com/

TCP Socket: read encrypted data
http://forums.realsoftware.com/viewtopic.php?f=2&t=46979
Page 1 of 1

Author:  ciri [ Sun Feb 17, 2013 9:32 am ]
Post subject:  TCP Socket: read encrypted data

Hi,

the client is developed using HTML5 WebSocket (ws://), the server is developed using realbasic using TCPSocket class.

Client has established a connection with server (I correctly send key and accept-key) but when client send a message to server, the message arrives encrypted. So, how I can decrypt this message with RB to read it correctly?

Thanks.

Author:  swort [ Sun Feb 17, 2013 2:22 pm ]
Post subject:  Re: TCP Socket: read encrypted data

to decode
http://docs.realsoftware.com/index.php/DecodeBase64

To encode
http://docs.realsoftware.com/index.php/EncodeBase64

WebSockets use Base64 on ws:// protocol see here (a request by the client to the server):
Quote:
GET ws://echo.websocket.org/?encoding=text HTTP/1.1
Origin: http://websocket.org
Cookie: __utma=99as Connection:
Upgrade Host: echo.websocket.org
Sec-WebSocket-Key: uRovscZjNol/umbTt5uKmw==
Upgrade: websocket
Sec-WebSocket-Version: 13

Author:  ciri [ Sun Feb 17, 2013 3:17 pm ]
Post subject:  Re: TCP Socket: read encrypted data

I use encode to make Accept-Key, the connection between client and server is established correctly.
The problems come when client and server must exchange messages. This messages are encrypted and I don't know how to read them with RB.

From websocket.org:

Quote:
Once established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode. Both text and binary frames can be sent in either direction at the same time. The data is minimally framed with just two bytes. In the case of text frames, each frame starts with a 0x00 byte, ends with a 0xFF byte, and contains UTF-8 data in between. WebSocket text frames use a terminator, while binary frames use a length prefix.
Image


This is my problem.

Author:  swort [ Sun Feb 17, 2013 3:34 pm ]
Post subject:  Re: TCP Socket: read encrypted data

Do you use TCPSocket or HTTPSocket?
- Use TCPSocket for it and handle it yourself.

read this, it might be more clear:
http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/#p2

Author:  wpurvis [ Sun Feb 17, 2013 3:43 pm ]
Post subject:  Re: TCP Socket: read encrypted data

ciri wrote:
I use encode to make Accept-Key, the connection between client and server is established correctly.
The problems come when client and server must exchange messages. This messages are encrypted and I don't know how to read them with RB.

From websocket.org:

Quote:
Once established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode. Both text and binary frames can be sent in either direction at the same time. The data is minimally framed with just two bytes. In the case of text frames, each frame starts with a 0x00 byte, ends with a 0xFF byte, and contains UTF-8 data in between. WebSocket text frames use a terminator, while binary frames use a length prefix.
Image


This is my problem.


You're going to have to implement the WebSocket protocol yourself. A TCPSocket doesn't know anything about particular protocols. That's why there are subclasses of TCPSocket for protocols like HTTP and SMTP. There's no built-in subclass for the WebSocket protocol; you will have to develop one.

Author:  ciri [ Mon Feb 18, 2013 7:16 am ]
Post subject:  Re: TCP Socket: read encrypted data

swort wrote:

Yes, I'm following this tutorial, but maybe I have some problems with this code:
/**
* Unmask a received payload
* @param $payload
*/
private function unmask($payload) {
$length = ord($payload[1]) & 127;

if($length == 126) {
$masks = substr($payload, 4, 4);
$data = substr($payload, 8);
}
elseif($length == 127) {
$masks = substr($payload, 10, 4);
$data = substr($payload, 14);
}
else {
$masks = substr($payload, 2, 4);
$data = substr($payload, 6);
}

$text = '';
for ($i = 0; $i < strlen($data); ++$i) {
$text .= $data[$i] ^ $masks[$i%4];
}
return $text;
}


I've write this in RB:

Dim carattere As String = payload.Mid(2,1)
Dim length As integer = BitwiseAnd(Asc(carattere), 127)
Dim masks As String
Dim data As String

if length = 126 then
masks = payload.Mid(4,4)
data = payload.Left(8)
elseif(length = 127) then
masks = payload.Mid(10, 4)
data = payload.Left(14)
else
masks = payload.Mid(2, 4)
data = payload.Left(6)
end if

Dim text As string = ""
Dim i As Integer
Dim ris As Integer
for i = 1 To data.len()
ris = BitwiseXor(cdbl(data.Mid(i+1, 1)), cdbl(masks.Mid(i+1 Mod 4, 1)))
text = text + Str(ris)
next
return text


Is it correct?

Author:  timhare [ Mon Feb 18, 2013 8:17 pm ]
Post subject:  Re: TCP Socket: read encrypted data

Use MidB instead of Mid. Encoding shouldn't be an issue, but you never know.
Substr() is zero-based. MidB is one-based. Increment all your offsets.
Substr($payload, 8) is not the same as Left(). Use Payload.MidB(9).

Author:  ciri [ Tue Feb 19, 2013 4:03 pm ]
Post subject:  Re: TCP Socket: read encrypted data

I followed your advice:

Dim carattere As String = payload.MidB(2,1)
Dim length As integer = BitwiseAnd(Asc(carattere), 127)
Dim masks As String
Dim data As String

if length = 126 then
masks = payload.MidB(5,4)
data = payload.MidB(9)
elseif(length = 127) then
masks = payload.MidB(11, 4)
data = payload.Midb(15)
else
masks = payload.MidB(3, 4)
data = payload.MidB(7)
end if

Dim text As string = ""
Dim i As Integer
Dim ris As Integer
for i = 1 To data.len()
ris = BitwiseXor(cdbl(data.MidB(i+1, 1)), cdbl(masks.MidB((i+1) Mod 4, 1)))
text = text + Str(ris)
next
return text


Client sends a string like "hello". I write this code:

var connection = new WebSocket('ws://localhost:8855/');

connection.onopen = function () {
connection.send("hello");
};

connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};

connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};


To the server arrives an encrypted string like this: …ç-yj‚Am

and the decrypted string is: "00000" (should be "hello")

:(

Author:  timhare [ Tue Feb 19, 2013 11:37 pm ]
Post subject:  Re: TCP Socket: read encrypted data

Cdbl and Str are the wrong functions to use. Use AscB() and ChrB(), respectively. Also, I think you should use i, not i+1 in your XOR.

Author:  ciri [ Wed Feb 20, 2013 7:45 am ]
Post subject:  Re: TCP Socket: read encrypted data

We're almost there...

The decrypted string is almost correct, but is always wrong the fourth character and its multiples (eighth character, sixteenth character).

For example, if client send the string "12345678", server return: 123±567½

This is the code:

Dim carattere As String = payload.MidB(2,1)
Dim length As integer = BitwiseAnd(Asc(carattere), 127)
Dim masks As String
Dim data As String

if length = 126 then
masks = payload.MidB(5,4)
data = payload.MidB(9)
elseif(length = 127) then
masks = payload.MidB(11, 4)
data = payload.MidB(15)
else
masks = payload.MidB(3, 4)
data = payload.MidB(7)
end if

Dim text As string = ""
Dim i As Integer
Dim ris As Integer
for i = 1 To data.len()
ris = BitwiseXor(AscB(data.MidB(i, 1)), AscB(masks.MidB((i Mod 4), 1)))
text = text + ChrB(ris)
next
return text


Thanks a lot.

Author:  ciri [ Wed Feb 20, 2013 3:43 pm ]
Post subject:  Re: TCP Socket: read encrypted data

Now it finally works!

I applied this fix:

operando = i Mod 4
if operando = 0 then
operando = 4
end if


So, the entire function is:

Dim carattere As String = payload.MidB(2,1)
Dim length As integer = BitwiseAnd(Asc(carattere), 127)
Dim masks As String
Dim data As String

if length = 126 then
masks = payload.MidB(5,4)
data = payload.MidB(9)
elseif(length = 127) then
masks = payload.MidB(11, 4)
data = payload.MidB(15)
else
masks = payload.MidB(3, 4)
data = payload.MidB(7)
end if

Dim text As string = ""
Dim i As Integer
Dim ris As Integer
Dim operando As Double
for i = 1 To data.len()
operando = i Mod 4
if operando = 0 then
operando = 4
end if
ris = BitwiseXor(AscB(data.MidB(i, 1)), AscB(masks.MidB(operando, 1)))
text = text + ChrB(ris)
next
return text


Thanks to everybody that helped me :)

Author:  octalmage [ Mon Apr 08, 2013 9:23 pm ]
Post subject:  Re: TCP Socket: read encrypted data

Is there anyway you could share your code for the websocket? I need this for my application.

Thanks,
Jason.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/