Skip to content

Instantly share code, notes, and snippets.

@zhezheng
Created October 11, 2013 05:00
Show Gist options
  • Save zhezheng/6929794 to your computer and use it in GitHub Desktop.
Save zhezheng/6929794 to your computer and use it in GitHub Desktop.
Privacy Preserving Record Matching Among Multiple Providers
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.Security.Cryptography;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace FinalProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket sock;
Thread th;
private void BeginListen() //Socket
{
IPAddress serverIp = IPAddress.Parse(tbTargetIp.Text);
IPEndPoint iep = new IPEndPoint(serverIp, Convert.ToInt32(tbPort.Text));
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.lbIep.Text = iep.ToString();
sock.Bind(iep);
while (true)
{
Byte[] byteMessage = new Byte[100];
sock.Listen(100); //100times
Socket newSock = sock.Accept();
newSock.Receive(byteMessage);
string msg = System.Text.Encoding.UTF8.GetString(byteMessage); //ByteToString
rtbTalk.AppendText(msg);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button4.Enabled = true;
string str_name = textBox1.Text;
string str_ssn = textBox2.Text;
textBox3.Text = str_name + "-" + str_ssn + "\r\n" + textBox3.Text;
}
public static string ToHexString(byte[] bytes) //ByteToHexString
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
}
public static string Hash(string str) //Hash Function
{
ASCIIEncoding enc = new ASCIIEncoding();
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] shaHash = sha.ComputeHash(enc.GetBytes(str));
string str_1 = ToHexString(shaHash);
return str_1;
}
private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = Hash(textBox3.Lines[0]);
}
private void button3_Click(object sender, EventArgs e)
{
textBox3.Text = "";
textBox4.Text = "";
rtbTalk.Text = "";
button4.Enabled = false;
}
private void button4_Click(object sender, EventArgs e)
{
int start = textBox3.GetFirstCharIndexFromLine(0);
int end = textBox3.GetFirstCharIndexFromLine(1);
textBox3.Select(start, end);
textBox3.SelectedText = "";
if (textBox3.Text == "")
{
button4.Enabled = false;
}
}
private void btConnect_Click(object sender, EventArgs e)
{
btConnect.Enabled = false;
btStopConnect.Enabled = true;
btSend.Enabled = true;
th = new Thread(new ThreadStart(BeginListen));
th.Start();
lbState.Text = "Listenning...";
}
private void btStopConnect_Click(object sender, EventArgs e)
{
btStopConnect.Enabled = false;
btConnect.Enabled = true;
btSend.Enabled = false;
sock.Close();
th.Abort();
lbState.Text = "Listenning stopped";
}
private void btSend_Click(object sender, EventArgs e)
{
IPAddress clientIp = IPAddress.Parse(tbTargetIp.Text);
int clientPort = Convert.ToInt32(tbPort1.Text);
IPEndPoint clientIep = new IPEndPoint(clientIp, clientPort);
Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket1.Connect(clientIep);
Byte[] byte_Message1;
byte_Message1 = System.Text.Encoding.UTF8.GetBytes("-------------------------\r\n" + "The Hash value of patients' name and SSN.....\r\n" + "-------------------------\r\n");
socket1.Send(byte_Message1);
socket1.Shutdown(SocketShutdown.Both);
socket1.Close();
int count_i = 0;
while (count_i < this.textBox3.Lines.Count() - 1)
{
Byte[] byte_Message;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(clientIep);
byte_Message = System.Text.Encoding.UTF8.GetBytes(Hash(textBox3.Lines[count_i]) + "\n");
socket.Send(byte_Message);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
count_i ++;
}
}
private void rtbTalk_TextChanged(object sender, EventArgs e)
{
}
private void tbPort_TextChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
File.WriteAllText(textBox5.Text,textBox3.Text);
}
private void button6_Click(object sender, EventArgs e)
{
textBox3.Text = File.ReadAllText(textBox5.Text);
button4.Enabled = true;
}
private void lbIep_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
IPAddress clientIp = IPAddress.Parse(tbTargetIp.Text);
int clientPort = Convert.ToInt32(tbPort1.Text);
IPEndPoint clientIep = new IPEndPoint(clientIp, clientPort);
int i = 0; //this.textBox3.Lines.Count() - 1
int j = 0; //this.rtbTalk.Lines.Count() - 1
Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket1.Connect(clientIep);
Byte[] byte_Message1;
byte_Message1 = System.Text.Encoding.UTF8.GetBytes("------------------------------\r\n" + "Here are the common ones.....\r\n" + "------------------------------\r\n");
socket1.Send(byte_Message1);
socket1.Shutdown(SocketShutdown.Both);
socket1.Close();
byte_Message1 = System.Text.Encoding.UTF8.GetBytes(textBox3.Lines[i] + "\n");
for (j = 0; j < this.rtbTalk.Lines.Count() - 1; j++)
{
for (i = 0; i < this.textBox3.Lines.Count() - 1; i++)
{
if (rtbTalk.Lines[j] == Hash(textBox3.Lines[i]))
{
Byte[] byte_Message;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(clientIep);
byte_Message = System.Text.Encoding.UTF8.GetBytes(textBox3.Lines[i] + "\n");
socket.Send(byte_Message);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (btStopConnect.Enabled == true)
{
sock.Close();
th.Abort();
}
}
private void button8_Click(object sender, EventArgs e)
{
IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
rtbWords.Text = ieh.AddressList[0].ToString ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment