Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
QuestionJust a simple question Pin
Jesús Frías12-May-13 6:13
Jesús Frías12-May-13 6:13 
AnswerRe: Just a simple question Pin
Abhinav S12-May-13 6:19
Abhinav S12-May-13 6:19 
QuestionRE: Custom Login to a secure Page on an external site. Pin
khali7811-May-13 19:29
khali7811-May-13 19:29 
AnswerRe: RE: Custom Login to a secure Page on an external site. Pin
Richard MacCutchan11-May-13 21:26
mveRichard MacCutchan11-May-13 21:26 
Questionsockets + Cryptography Pin
Member 997361610-May-13 14:07
Member 997361610-May-13 14:07 
SuggestionRe: sockets + Cryptography Pin
AlphaDeltaTheta10-May-13 15:35
AlphaDeltaTheta10-May-13 15:35 
GeneralRe: sockets + Cryptography Pin
Member 997361610-May-13 22:50
Member 997361610-May-13 22:50 
GeneralRe: sockets + Cryptography Pin
AlphaDeltaTheta11-May-13 16:38
AlphaDeltaTheta11-May-13 16:38 
I'm not that good in programming sockets in c# but in past I've done quite a lot in Java so some out of java base and parts of your code Big Grin | :-D

The protocol:
Contains 4 keywords:

MESSAGE - Generic message <message length="">
KEY - Cipher key <key alg="" length="">
NEXTKEY - Request to generate next key <nextkey>
END - End communication <end>

Each message is terminated by a LF pair (taken from http) and data by CRLF

My idea is read every line, whenever a CRLF is detected, prepare for command parsing.
If MESSAGE command is present, parse the length and display the text that follows.
If NEXTKEY is detected send a KEY message.
If key is detected, read the key and re-initialize the cipher.

C#
public void ServerSendMessage(string msg)
        {
            stream = client.GetStream(); //Gets The Stream of The Connection

            StreamWriter writer = new StreamWriter(stream);


            int length = data.Length; // Gets the length of the byte data

            /* send a command to prepare client to receive a message */
            writer.Write("MESSAGE "+length+"\n");

            stream.Write(msg); //Sends the real data

            stream.Write("\r\n");
        }

/* send a KEY ALG LEN \n
   KEY_IN_BASE64 ENCODING\r\n message*/

public void ServerSendKey(byte[] key, string alogrithm)
        {
            stream = client.GetStream(); //Gets The Stream of The Connection

            StreamWriter writer = new StreamWriter(stream);


            int length = key.Length; // Gets the length of the byte data

            /* send a command to prepare client to receive a message */
            writer.Write("KEY "+ALGORITHM+" "+length+"\n");

            writer.Write(Convert.ToBase64String(key)); //send base64 encoded key


            writer.Write("\r\n");
        }


At Client

C#
public void ClientReceive()
        {

            stream = client.GetStream(); //Gets The Stream of The Connection
            StreamReader reader = new StreamReader(stream)
            string message = reader.ReadLine();
            while(reader.peek() > -1 || !message.Equals("END"))
            {
                 if(message.StartsWith("MESSAGE") {
                    ParseMsg(message,reader);
                 } else if(message.StartsWith("KEY") {
                    ParseKey(message,reader);
                 }
            }
            message = reader.ReadLine();
        }

        private void ParseMsg(string cmd,StreamReader r)
        {
              int length = Integer.ParseInt(cmd.SubString(8));

              char[] msg = new char[length];
              r.ReadBlock(msg,0,length);

              string message = new String(msg);
              //do something with your message...
        }

        private void ParseKey(string cmd,StreamReader r)
        {
              string alg = cmd.SubString(3,3);
              int length = Integer.ParseInt(cmd.SubString(9));

              byte[] key = Convert.FromBase64String(r.ReadLine());

              //do something with your key...
        }

NOTE: Not ready for Production Code... Run tests!

Questionwhats the best practice to save service data? Pin
neodeaths10-May-13 9:18
neodeaths10-May-13 9:18 
AnswerRe: whats the best practice to save service data? Pin
Dave Kreskowiak10-May-13 10:49
mveDave Kreskowiak10-May-13 10:49 
AnswerRe: whats the best practice to save service data? Pin
Eddy Vluggen11-May-13 3:30
professionalEddy Vluggen11-May-13 3:30 
QuestionProperties vs Methods Opinions sought Pin
Keith Barrow10-May-13 4:22
professionalKeith Barrow10-May-13 4:22 
SuggestionRe: Properties vs Methods Opinions sought Pin
Richard Deeming10-May-13 5:16
mveRichard Deeming10-May-13 5:16 
GeneralRe: Properties vs Methods Opinions sought Pin
Keith Barrow10-May-13 5:37
professionalKeith Barrow10-May-13 5:37 
GeneralRe: Properties vs Methods Opinions sought Pin
jschell10-May-13 7:58
jschell10-May-13 7:58 
AnswerRe: Properties vs Methods Opinions sought Pin
jschell10-May-13 7:50
jschell10-May-13 7:50 
GeneralRe: Properties vs Methods Opinions sought Pin
Ennis Ray Lynch, Jr.10-May-13 8:41
Ennis Ray Lynch, Jr.10-May-13 8:41 
GeneralRe: Properties vs Methods Opinions sought Pin
jschell13-May-13 7:58
jschell13-May-13 7:58 
AnswerRe: Properties vs Methods Opinions sought Pin
PIEBALDconsult10-May-13 18:32
mvePIEBALDconsult10-May-13 18:32 
GeneralPracticing Code Logic Pin
N8tiv10-May-13 3:24
N8tiv10-May-13 3:24 
GeneralRe: Practicing Code Logic Pin
Richard MacCutchan10-May-13 3:48
mveRichard MacCutchan10-May-13 3:48 
Questionxmpp Pin
Member 1004242210-May-13 2:20
Member 1004242210-May-13 2:20 
AnswerRe: xmpp Pin
Pete O'Hanlon10-May-13 2:34
mvePete O'Hanlon10-May-13 2:34 
AnswerRe: xmpp Pin
Richard MacCutchan10-May-13 3:44
mveRichard MacCutchan10-May-13 3:44 
QuestionAbout xmpp n bosh server Pin
Member 1004242210-May-13 1:46
Member 1004242210-May-13 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.