/*
Multicast chat with DES Packet Encryption. (C) Copyright Joshua Duncan 2012
Version 1.0 - 28/01/2012
This code is released under the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0
This Copyright Block must remain in all versions of the source code.
Also any use of any part of my code must give due credit to me (Joshua Duncan).
*/
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Security.Cryptography;
class MulticastChat : Form
{
TextBox newText;
RichTextBox results;
Socket sock;
Thread receiver;
/*
You can change the IP to anything between 224.3.0.0 to 224.252.0.0
Also feel free to change the port. Just remember to change the IP
at both Line 31 and at Line 66, And the port at Line 31 and 63.
*/
IPEndPoint multiep = new IPEndPoint(IPAddress.Parse("224.100.23.196"), 80);
public MulticastChat()
{
Text = "Multicast Remote Chat";
Size = new Size(400, 380);
Label label1 = new Label();
label1.Parent = this;
label1.Text = "Enter text to send:";
label1.AutoSize = true;
label1.Location = new Point(10, 30);
newText = new TextBox();
newText.Parent = this;
newText.Size = new Size(200, 2 * Font.Height);
newText.Location = new Point(10, 55);
results = new RichTextBox();
results.Parent = this;
results.Location = new Point(10, 85);
results.Size = new Size(360, 18 * Font.Height);
Button sendit = new Button();
sendit.Parent = this;
sendit.Text = "Send";
sendit.Location = new Point(220,52);
sendit.Size = new Size(5 * Font.Height, 2 * Font.Height);
sendit.Click += new EventHandler(ButtonSendOnClick);
Button closeit = new Button();
closeit.Parent = this;
closeit.Text = "Close";
closeit.Location = new Point(290, 52);
closeit.Size = new Size(5 * Font.Height, 2 * Font.Height);
closeit.Click += new EventHandler(ButtonCloseOnClick);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 80);
sock.Bind(iep);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse("224.100.23.196")));
receiver = new Thread(new ThreadStart(packetReceive));
receiver.IsBackground = true;
receiver.Start();
}
void ButtonSendOnClick(object obj, EventArgs ea)
{
string encode = Encrypt(newText.Text);
byte[] message = Encoding.UTF8.GetBytes(encode);
int memsize = (int)message.Length;
byte[] size = BitConverter.GetBytes(memsize);
byte[] send = new byte[4+memsize];
Array.Copy(size, 0, send, 0, 4);
Array.Copy(message, 0, send, 4, message.Length);
newText.Clear();
sock.SendTo(send, SocketFlags.None, multiep);
}
void ButtonCloseOnClick(object obj, EventArgs ea)
{
receiver.Abort();
sock.Close();
Close();
}
void packetReceive()
{
EndPoint ep = (EndPoint)multiep;
byte[] data = new byte[2048];
string stringData;
int recv;
while (true)
{
recv = sock.ReceiveFrom(data, ref ep);
byte[] memsize = new byte[4];
Array.Copy(data, 0, memsize, 0, 4);
int size = BitConverter.ToInt32(memsize, 0);
byte[] message = new byte[size];
Array.Copy(data, 4, message, 0, size);
stringData = Encoding.UTF8.GetString(message, 0, size);
stringData = Decrypt(stringData);
results.AppendText("from " + ep.ToString() + ": " + stringData + Environment.NewLine);
}
}
public static string Encrypt(string originalString)
{
byte[] Key = ASCIIEncoding.ASCII.GetBytes("password");
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(Key, Key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
public static string Decrypt(string cryptedString)
{
byte[] Key = ASCIIEncoding.ASCII.GetBytes("password");
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream
(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(Key, Key), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
public static void Main()
{
Application.Run(new MulticastChat());
}
}