Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Jun 29, 2017 11:03 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: TCP Socket: read encrypted data
PostPosted: Sun Feb 17, 2013 9:32 am 
Offline

Joined: Sat Jan 01, 2011 10:29 am
Posts: 19
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Sun Feb 17, 2013 2:22 pm 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
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

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Sun Feb 17, 2013 3:17 pm 
Offline

Joined: Sat Jan 01, 2011 10:29 am
Posts: 19
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Sun Feb 17, 2013 3:34 pm 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
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

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Sun Feb 17, 2013 3:43 pm 
Offline

Joined: Tue Oct 04, 2005 10:55 am
Posts: 43
Location: Fort Myers, FL
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Mon Feb 18, 2013 7:16 am 
Offline

Joined: Sat Jan 01, 2011 10:29 am
Posts: 19
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Mon Feb 18, 2013 8:17 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
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).


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Tue Feb 19, 2013 4:03 pm 
Offline

Joined: Sat Jan 01, 2011 10:29 am
Posts: 19
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")

:(


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Tue Feb 19, 2013 11:37 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Wed Feb 20, 2013 7:45 am 
Offline

Joined: Sat Jan 01, 2011 10:29 am
Posts: 19
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Wed Feb 20, 2013 3:43 pm 
Offline

Joined: Sat Jan 01, 2011 10:29 am
Posts: 19
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 :)


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket: read encrypted data
PostPosted: Mon Apr 08, 2013 9:23 pm 
Offline

Joined: Thu May 24, 2012 11:22 am
Posts: 6
Is there anyway you could share your code for the websocket? I need this for my application.

Thanks,
Jason.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group