Click here to Skip to main content
11,433,486 members (59,996 online)
   

C#

 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
WidmarkRob12-May-13 11:40
memberWidmarkRob12-May-13 11:40 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
Richard MacCutchan12-May-13 22:09
mvpRichard MacCutchan12-May-13 22:09 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
WidmarkRob12-May-13 22:40
memberWidmarkRob12-May-13 22:40 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
Richard MacCutchan13-May-13 0:16
mvpRichard MacCutchan13-May-13 0:16 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
WidmarkRob13-May-13 3:21
memberWidmarkRob13-May-13 3:21 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
Keith Barrow13-May-13 0:38
mentorKeith Barrow13-May-13 0:38 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
Richard MacCutchan13-May-13 0:56
mvpRichard MacCutchan13-May-13 0:56 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
Pete O'Hanlon13-May-13 1:36
protectorPete O'Hanlon13-May-13 1:36 
GeneralRe: Methods, Parameters, Arguments… Oh my! Pin
Richard MacCutchan13-May-13 1:38
mvpRichard MacCutchan13-May-13 1:38 
QuestionJust a simple question Pin
Jesús Frías12-May-13 7:13
memberJesús Frías12-May-13 7:13 
AnswerRe: Just a simple question Pin
Abhinav S12-May-13 7:19
mvpAbhinav S12-May-13 7:19 
QuestionRE: Custom Login to a secure Page on an external site. Pin
khali7811-May-13 20:29
memberkhali7811-May-13 20:29 
AnswerRe: RE: Custom Login to a secure Page on an external site. Pin
Richard MacCutchan11-May-13 22:26
mvpRichard MacCutchan11-May-13 22:26 
Questionsockets + Cryptography Pin
Member 997361610-May-13 15:07
memberMember 997361610-May-13 15:07 
Hi, I want to realise a Cleint/Server chat app that respects this:

1- Server connects to client;
2- server send "Hello" to client;
3- cleint responses with "Hello, ok";
4- server generates RSA Key and send public key to cleint;
5- client select a random algorithm from DES, AES(128, 192, 256) and sends it to the server;
6- server sends "GENERATE FIRST KEY" to client;
7- cleint generate a key with a size that dependse on the algorithm choosen in step 5, and encrypt it using the public key;
8- server decrypts the key;

Now the server and client can communicate using the shared key, and to make it more secure, after 5 min server send "NEXT KEY", to generate another key


Untill now I just can chat between client and server without encrypt messages, and this is my code


Server:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace Server___Tutorial
{
    public partial class Form1 : Form
    {
        int i;
        TcpListener server = new TcpListener(IPAddress.Any, 1980); // Creates a TCP Listener To Listen to Any IPAddress trying to connect to the program with port 1980
        NetworkStream stream; //Creats a NetworkStream (used for sending and receiving data)
        TcpClient client; // Creates a TCP Client
        byte[] datalength = new byte[4]; // creates a new byte with length 4 ( used for receivng data's lenght)
       
        public Form1()
        {
            InitializeComponent();
        }
 
   
        public void ServerReceive()
        {
            stream = client.GetStream(); //Gets The Stream of The Connection
            new Thread(() => // Thread (like Timer)
            {
                while ((i = stream.Read(datalength, 0, 4)) != 0)//Keeps Trying to Receive the Size of the Message or Data
                {
                    // how to make a byte E.X byte[] examlpe = new byte[the size of the byte here] , i used BitConverter.ToInt32(datalength,0) cuz i received the length of the data in byte called datalength :D
                    byte[] data = new byte[BitConverter.ToInt32(datalength, 0)]; // Creates a Byte for the data to be Received On
                    stream.Read(data, 0, data.Length); //Receives The Real Data not the Size
                    this.Invoke((MethodInvoker)delegate // To Write the Received data
                    {
                        txtLog.Text += System.Environment.NewLine + "Client : " + Encoding.Default.GetString(data); // Encoding.Default.GetString(data); Converts Bytes Received to String
                    });
                }
            }).Start(); // Start the Thread
 
        }
       
 
       
        public void ServerSend(string msg)
        {
            stream = client.GetStream(); //Gets The Stream of The Connection
            byte[] data; // creates a new byte without mentioning the size of it cuz its a byte used for sending
            data = Encoding.Default.GetBytes(msg); // put the msg in the byte ( it automaticly uses the size of the msg )
            int length = data.Length; // Gets the length of the byte data
            byte[] datalength = new byte[4]; // Creates a new byte with length of 4
            datalength = BitConverter.GetBytes(length); //put the length in a byte to send it
            stream.Write(datalength, 0, 4); // sends the data's length
            stream.Write(data, 0, data.Length); //Sends the real data
        }
 
        private void btnListen_Click(object sender, EventArgs e)
        {
            server.Start(); // Starts Listening to Any IPAddress trying to connect to the program with port 1980
            MessageBox.Show("Waiting For Connection");
            new Thread(() => // Creates a New Thread (like a timer)
                {
                    client = server.AcceptTcpClient(); //Waits for the Client To Connect
                    MessageBox.Show("Connected To Client");
                    if (client.Connected) // If you are connected
                    {
                        ServerReceive(); //Start Receiving
                    }
                }).Start();
        }
 
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (client.Connected) // if the client is connected
            {
                ServerSend(txtSend.Text); // uses the Function ClientSend and the msg as txtSend.Text
            }
        }
 
       
    }
}

Client:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace Client
{
    public partial class Form1 : Form
    {
        int i;
        TcpClient client; // Creates a TCP Client
        NetworkStream stream; //Creats a NetworkStream (used for sending and receiving data)
        byte[] datalength = new byte[4]; // creates a new byte with length 4 ( used for receivng data's lenght)
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                client = new TcpClient("127.0.0.1", 1980); //Trys to Connect
                ClientReceive(); //Starts Receiving When Connected
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // Error handler :D
            }
        }
 
        public void ClientReceive()
        {
           
            stream = client.GetStream(); //Gets The Stream of The Connection
            new Thread(() => // Thread (like Timer)
            {
                while ((i = stream.Read(datalength, 0, 4)) != 0)//Keeps Trying to Receive the Size of the Message or Data
                {
                    // how to make a byte E.X byte[] examlpe = new byte[the size of the byte here] , i used BitConverter.ToInt32(datalength,0) cuz i received the length of the data in byte called datalength :D
                    byte[] data = new byte[BitConverter.ToInt32(datalength,0)]; // Creates a Byte for the data to be Received On
                    stream.Read(data, 0, data.Length); //Receives The Real Data not the Size
                    this.Invoke((MethodInvoker)delegate // To Write the Received data
                    {
                        txtLog.Text += System.Environment.NewLine + "Server : " + Encoding.Default.GetString(data); // Encoding.Default.GetString(data); Converts Bytes Received to String
                    });  
                }
            }).Start(); // Start the Thread
        }
 
        public void ClientSend(string msg)
        {
            stream = client.GetStream(); //Gets The Stream of The Connection
            byte[] data; // creates a new byte without mentioning the size of it cuz its a byte used for sending
            data = Encoding.Default.GetBytes(msg); // put the msg in the byte ( it automaticly uses the size of the msg )
            int length = data.Length; // Gets the length of the byte data
            byte[] datalength = new byte[4]; // Creates a new byte with length of 4
            datalength = BitConverter.GetBytes(length); //put the length in a byte to send it
            stream.Write(datalength, 0, 4); // sends the data's length
            stream.Write(data, 0, data.Length); //Sends the real data
        }
 
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (client.Connected) // if the client is connected
            {
                ClientSend(txtSend.Text); // uses the Function ClientSend and the msg as txtSend.Text
            }
        }
 
    }
}

I also have AES, DES and RSA classes, but I don't know how to tell if a message is a key or just a normal message...


Thank you
SuggestionRe: sockets + Cryptography Pin
Amitosh Swain10-May-13 16:35
memberAmitosh Swain10-May-13 16:35 
GeneralRe: sockets + Cryptography Pin
Member 997361610-May-13 23:50
memberMember 997361610-May-13 23:50 
GeneralRe: sockets + Cryptography Pin
Amitosh Swain11-May-13 17:38
memberAmitosh Swain11-May-13 17:38 
Questionwhats the best practice to save service data? Pin
neodeaths10-May-13 10:18
memberneodeaths10-May-13 10:18 
AnswerRe: whats the best practice to save service data? Pin
Dave Kreskowiak10-May-13 11:49
mvpDave Kreskowiak10-May-13 11:49 
AnswerRe: whats the best practice to save service data? Pin
Eddy Vluggen11-May-13 4:30
mvpEddy Vluggen11-May-13 4:30 
QuestionProperties vs Methods Opinions sought Pin
Keith Barrow10-May-13 5:22
mentorKeith Barrow10-May-13 5:22 
SuggestionRe: Properties vs Methods Opinions sought Pin
Richard Deeming10-May-13 6:16
professionalRichard Deeming10-May-13 6:16 
GeneralRe: Properties vs Methods Opinions sought Pin
Keith Barrow10-May-13 6:37
mentorKeith Barrow10-May-13 6:37 
GeneralRe: Properties vs Methods Opinions sought Pin
jschell10-May-13 8:58
memberjschell10-May-13 8:58 
AnswerRe: Properties vs Methods Opinions sought Pin
jschell10-May-13 8:50
memberjschell10-May-13 8:50 
GeneralRe: Properties vs Methods Opinions sought Pin
Ennis Ray Lynch, Jr.10-May-13 9:41
memberEnnis Ray Lynch, Jr.10-May-13 9:41 
GeneralRe: Properties vs Methods Opinions sought Pin
jschell13-May-13 8:58
memberjschell13-May-13 8:58 
AnswerRe: Properties vs Methods Opinions sought Pin
PIEBALDconsult10-May-13 19:32
professionalPIEBALDconsult10-May-13 19:32 
GeneralPracticing Code Logic Pin
WidmarkRob10-May-13 4:24
memberWidmarkRob10-May-13 4:24 
GeneralRe: Practicing Code Logic Pin
Richard MacCutchan10-May-13 4:48
mvpRichard MacCutchan10-May-13 4:48 
Questionxmpp Pin
Member 1004242210-May-13 3:20
memberMember 1004242210-May-13 3:20 
AnswerRe: xmpp Pin
Pete O'Hanlon10-May-13 3:34
protectorPete O'Hanlon10-May-13 3:34 
AnswerRe: xmpp Pin
Richard MacCutchan10-May-13 4:44
mvpRichard MacCutchan10-May-13 4:44 
QuestionAbout xmpp n bosh server Pin
Member 1004242210-May-13 2:46
memberMember 1004242210-May-13 2:46 
AnswerRe: About xmpp n bosh server Pin
Pete O'Hanlon10-May-13 3:13
protectorPete O'Hanlon10-May-13 3:13 
QuestionExport to PST Pin
JD869-May-13 15:29
memberJD869-May-13 15:29 
QuestionProper Use of a Semaphore Pin
Richard Andrew x649-May-13 15:14
memberRichard Andrew x649-May-13 15:14 
AnswerRe: Proper Use of a Semaphore Pin
SledgeHammer019-May-13 18:05
memberSledgeHammer019-May-13 18:05 
GeneralRe: Proper Use of a Semaphore Pin
Richard Andrew x6410-May-13 7:10
memberRichard Andrew x6410-May-13 7:10 
Questionhow do i display http fields Pin
mais irreem9-May-13 4:35
membermais irreem9-May-13 4:35 
AnswerRe: how do i display http fields Pin
OriginalGriff9-May-13 4:54
mvpOriginalGriff9-May-13 4:54 
QuestionHTML, RTF or WordML? Pin
Jassim Rahma9-May-13 0:47
memberJassim Rahma9-May-13 0:47 
AnswerRe: HTML, RTF or WordML? Pin
Abhinav S9-May-13 1:33
mvpAbhinav S9-May-13 1:33 
AnswerRe: HTML, RTF or WordML? Pin
Dave Kreskowiak9-May-13 2:40
mvpDave Kreskowiak9-May-13 2:40 
QuestionLogin Form in Visual studio 2008 c# Pin
Akash Bharatvala9-May-13 0:18
memberAkash Bharatvala9-May-13 0:18 
AnswerRe: Login Form in Visual studio 2008 c# Pin
Abhinav S9-May-13 1:23
mvpAbhinav S9-May-13 1:23 
AnswerRe: Login Form in Visual studio 2008 c# Pin
Bikash Prakash Dash12-May-13 22:48
memberBikash Prakash Dash12-May-13 22:48 
QuestionPerformance issue using Multi threading in Net 3.5 Pin
KIDYA8-May-13 21:35
memberKIDYA8-May-13 21:35 
AnswerRe: Performance issue using Multi threading in Net 3.5 Pin
Abhinav S8-May-13 22:10
mvpAbhinav S8-May-13 22:10 
GeneralRe: Performance issue using Multi threading in Net 3.5 Pin
KIDYA8-May-13 22:37
memberKIDYA8-May-13 22:37 

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

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


Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 5 May 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid