예제 #1
0
        //choosing file to send >>> Attach button
        private void button1_Click(object sender, EventArgs e)
        {
            if (userToRecieve == "")
            {
                MessageBox.Show("click on the user you want to send him the file !!!");
                return;
            }
            openFileDialog2.ShowDialog();
            foreach (User u in listUsers)
            {
                if (u.userName == userToRecieve)
                {
                    user.userName = userToRecieve;
                    user.ip       = u.ip;
                    user.port     = u.port;
                }
            }
            ArrayList  n    = new ArrayList();
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(user.ip), Int32.Parse(user.port));

            n.Add(ipep);
            string hmacfile = TheHelperFiles.fileHmac(openFileDialog2.FileName);

            userToRecieve = "SENDFILE-" + tbMyIp.Text + "-" + nubPort.Value + "-" + userNameTB.Text + "-" + openFileDialog2.FileName + "-" + hmacfile;
            send(userToRecieve, n);
        }
        public static String recieve(String file, TcpListener listener, string savePath, string sourceHmac)
        {
            byte[] RecData = new byte[1024];
            int    RecBytes;

            string[] filetosendarray = savePath.Split('\\');
            string   theFile         = filetosendarray[filetosendarray.Length - 1];

            while (true)
            {
                try
                {
                    using (var client = listener.AcceptTcpClient())
                    {
                        if (client == null)
                        {
                            continue;
                        }
                        using (NetworkStream stream = client.GetStream())
                            using (FileStream fileStream = File.Create(savePath + "\\" + file + ".encrypted"))
                            {
                                int totalrecbytes = 0;
                                while ((RecBytes = stream.Read(RecData, 0, RecData.Length)) > 0)
                                {
                                    fileStream.Write(RecData, 0, RecBytes);
                                    totalrecbytes += RecBytes;
                                }
                                fileStream.Flush();
                                fileStream.Close();
                                TheHelperFiles.decryptFile(savePath + "\\" + file + ".encrypted");
                                AutoClosingMessageBox.Show("the file: " + theFile + " finished downloading ", "downloading file", 2000);

                                stream.Close();
                                var  destHmac = TheHelperFiles.fileHmac(savePath + "\\" + file);
                                bool result   = TheHelperFiles.fileCheckHmac(destHmac, sourceHmac);
                                if (!result)
                                {
                                    new Thread(() =>
                                    {
                                        LogClass.Log("not valid file HMAC \r\n " +
                                                     "filename: " + file +
                                                     "\r\n source HMAC: " + sourceHmac +
                                                     "\r\n recieved file HMAC: " + destHmac +
                                                     "\r\n from: " + IPAddress.Parse(((IPEndPoint)listener.LocalEndpoint).Address.ToString()) +
                                                     "\r\n on port: " + ((IPEndPoint)listener.LocalEndpoint).Port.ToString());
                                    }).Start();
                                    MessageBox.Show("the sent file HMAC didnt match with the recieved file HMAC ");
                                }
                            }
                        client.Close();
                        File.Delete(savePath + "\\" + file + ".encrypted");
                    }
                }
                catch (Exception ex) { Console.WriteLine("error in FileRecieve -> " + ex.Message); }
            }
            return("done");
        }
예제 #3
0
        public static String send(IPEndPoint ipEnd, String fileToSend, String port)
        {
            string[] filetosendarray = fileToSend.Split('\\');
            string   theFile         = filetosendarray[filetosendarray.Length - 1];

            byte[] SendingBuffer = null;

            TcpClient client = new TcpClient();

            client.Connect(ipEnd);
            TheHelperFiles.encryptFile(fileToSend);

            using (NetworkStream stream = client.GetStream())
            {
                using (var fileStream = new FileStream(fileToSend + ".encrypted", FileMode.Open, FileAccess.Read))
                {
                    try
                    {
                        int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(fileStream.Length) / Convert.ToDouble(1024)));
                        Console.WriteLine("send: number of packets: " + NoOfPackets);
                        int TotalLength = (int)fileStream.Length, CurrentPacketLength, counter = 0;
                        for (int i = 0; i < NoOfPackets; i++)
                        {
                            if (TotalLength > 1024)
                            {
                                CurrentPacketLength = 1024;
                                TotalLength         = TotalLength - CurrentPacketLength;
                            }
                            else
                            {
                                CurrentPacketLength = TotalLength;
                            }
                            SendingBuffer = new byte[CurrentPacketLength];
                            fileStream.Read(SendingBuffer, 0, CurrentPacketLength);
                            stream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
                        }
                        AutoClosingMessageBox.Show("the file: " + theFile + " has been sent ", "done sending file", 2000);
                        fileStream.Close();
                        File.Delete(fileToSend + ".encrypted");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        stream.Close();
                        client.Close();
                    }
                }
            }
            return(" ");
        }