public void sendUserData(SomeData infoe) { try { byte[] b = Helper.Serilize(infoe); writer.Write(1); writer.Write(b.Length); writer.Write(b); writer.Flush(); } catch { } }
public void SendFile(string M, string IPA, Int32 PortN) { TcpClient mclient = this.client.socket; lblStatus.Text = ""; NetworkStream netstream = mclient.GetStream(); try { String key = clientList[recieverId].info.publicKey; lblStatus.Text = "Connected to the Server...\n"; String data = File.ReadAllText(SendingFilePath, Encoding.GetEncoding(20127)); int keySize = Int32.Parse(numericUpDown1.Value.ToString()); byte[] encrypted; byte[] signedMsg = AsymmetricEncryption.signData(rsaProvider, out encrypted, data, keySize, key); Message myMsg = new Message(encrypted, signedMsg, Path.GetFileName(SendingFilePath)); BinaryWriter writer = new BinaryWriter(netstream); byte[] msg = Helper.Serilize(myMsg); writer.Write(3); writer.Write(recieverId); writer.Write(msg.Length); writer.Write(msg); writer.Flush(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void btn_SendCA_Click(object sender, EventArgs e) { string path = @"D://" + tBox_SiteName.Text + ".cer"; certificate = new Certificate() { siteName = tBox_SiteName.Text, country = tBox_Country.Text, city = tBox_City.Text, publicKey = clientPublicKey }; client = new TcpClient(); try { client.Connect(System.Net.IPAddress.Parse("127.0.0.1"), 9999); BinaryWriter writer = new BinaryWriter(client.GetStream()); BinaryReader reader = new BinaryReader(client.GetStream()); byte[] msg = Helper.Serilize(certificate); writer.Write(msg.Length); writer.Write(msg); writer.Flush(); s.info = new SomeData(); int l = reader.ReadInt32(); s.info.certificate = reader.ReadBytes(l); s.CA = reader.ReadString(); s.info.info = certificate; } catch { } this.Close(); }
void clientReceiver() { TcpClient socket = this.client.socket; int reqLen = Helper.getBytes("msg").Length; byte[] req = new byte[reqLen]; NetworkStream netStream = socket.GetStream(); while (socket.Connected) { socket.Client.Receive(req); BinaryReader streamR = new BinaryReader(netStream); if (Helper.getString(req).Equals("msg")) { string sender = streamR.ReadString(); DialogResult result = MessageBox.Show( "Accept the Incoming File from " + sender, "Incoming Connection", MessageBoxButtons.YesNo ); if (result == DialogResult.Yes) { int dataLen = streamR.ReadInt32(); byte[] RecievedData = streamR.ReadBytes(dataLen); //SaveFileName = streamR.ReadString(); Message m = (Message)Helper.deSerilize(RecievedData); string sender_public_key = null; foreach (SomeData item in clientList) { if (item.username.Equals(sender)) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(CA); bool autinticated = rsa.VerifyHash( new SHA1Managed().ComputeHash(Helper.Serilize(item.info)), CryptoConfig.MapNameToOID("SHA1"), item.certificate ); if (autinticated) { sender_public_key = item.info.publicKey; } else { string msg = "not secure connection with " + sender + "\n do you want to complete process?"; if (MessageBox.Show(msg, "warning", MessageBoxButtons.YesNo) == DialogResult.Yes) { sender_public_key = item.info.publicKey; } else { return; } } break; } } if (AsymmetricEncryption.verifyMsg(sender_public_key, m)) { String res = AsymmetricEncryption.PGPDecrypt(m.msg, rsaProvider.ToXmlString(true)); client.saveFile(res, "D://" + info.username + "/" + m.file_name); FileStream stream = new FileStream("D://" + info.username + "/" + Path.GetFileNameWithoutExtension(m.file_name) + ".sign", FileMode.OpenOrCreate); stream.WriteByte(0); stream.Write(Helper.getBytes(sender_public_key), 0, sender_public_key.Length); stream.WriteByte(1); stream.Write(m.signature, 0, m.signature.Length); stream.Close(); } else { MessageBox.Show("not reliable message"); } } } else if (Helper.getString(req).Equals("lst")) { clientList.Clear(); int len = streamR.ReadInt32(); for (int i = 0; i < len; i++) { SomeData itm = new SomeData(); int l = streamR.ReadInt32(); itm = (SomeData)Helper.deSerilize(streamR.ReadBytes(l)); clientList.Add(itm); } this.Invoke((MethodInvoker)(() => ShowData())); } } }