Пример #1
0
        private static void SendMsgPart(Socket s, string msgPart)
        {
            msgPart = RijndaelSimple.Encrypt(msgPart,
                                             passPhrase,
                                             saltValue,
                                             hashAlgorithm,
                                             passwordIterations,
                                             initVector,
                                             keySize);


            byte[] _msgPart = new byte[msgPart.Length];
            _msgPart = Convert.FromBase64String(msgPart);
            //_msgPart = Encoding.ASCII.GetBytes(msgPart);

            string msgLen = "" + _msgPart.Length;

            msgLen = RijndaelSimple.Encrypt(msgLen,
                                            passPhrase,
                                            saltValue,
                                            hashAlgorithm,
                                            passwordIterations,
                                            initVector,
                                            keySize);
            //MessageBox.Show(msgLen);

            byte[] _msgLen = new byte[1024];
            _msgLen = Convert.FromBase64String(msgLen);
            s.Send(_msgLen, 0, _msgLen.Length, SocketFlags.None);
            //MessageBox.Show("rbytes = " + _msgPart.Length + " length = " + msgPart.Length);
            // Receive an ack

            string sresp = getResponse64(s);

            sresp = sresp.Substring(0, 3);
            if (!sresp.Equals("000"))
            {
                MessageBox.Show("something is wrong");
            }
            //   MessageBox.Show("length acked");


            s.Send(_msgPart, 0, _msgPart.Length, SocketFlags.None);

            // Receive an ack
            sresp = getResponse64(s);
            sresp = sresp.Substring(0, 3);
            if (!sresp.Equals("000"))
            {
                MessageBox.Show("something is wrong");
            }
        }
Пример #2
0
        private static string recvMsgPart(Socket sockin)
        {
            byte[] req = new byte[1024];
            // Receive length
            while (sockin.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }
            int    rbytes = sockin.Receive(req, 0, sockin.Available, SocketFlags.None);
            string sreq   = Convert.ToBase64String(req, 0, rbytes);

            //MessageBox.Show(sreq);

            sreq = RijndaelSimple.Decrypt(sreq,
                                          passPhrase,
                                          saltValue,
                                          hashAlgorithm,
                                          passwordIterations,
                                          initVector,
                                          keySize);
            int msgLen = Int32.Parse(sreq);

            //MessageBox.Show("length = " + msgLen);
            // send ack for length

            byte[] resp  = new byte[2048];
            string sresp = "000";

            //resp = enc.GetBytes(sresp);
            sresp = RijndaelSimple.Encrypt(sresp,
                                           passPhrase,
                                           saltValue,
                                           hashAlgorithm,
                                           passwordIterations,
                                           initVector,
                                           keySize);

            resp = Convert.FromBase64String(sresp);
            sockin.Send(resp, 0, resp.Length, SocketFlags.None);
            // MessageBox.Show("Length ack sent");

            // receive message
            int    rcvd             = 0;
            string encryptedMsgBody = "";

            req = null;
            req = new byte[msgLen];
            while (rcvd < msgLen)
            {
                while (sockin.Available == 0)
                {
                    System.Threading.Thread.Sleep(100);
                }
                rbytes = sockin.Receive(req, rcvd, sockin.Available, SocketFlags.None);
                rcvd   = rcvd + rbytes;
            }
            encryptedMsgBody = Convert.ToBase64String(req);
            //MessageBox.Show("rbytes = " + req.Length + " length = " + encryptedMsgBody.Length);
            sreq = RijndaelSimple.Decrypt(encryptedMsgBody,
                                          passPhrase,
                                          saltValue,
                                          hashAlgorithm,
                                          passwordIterations,
                                          initVector,
                                          keySize);

            //MessageBox.Show(sreq.Substring(0,3) + "  " + sreq.Substring(sreq.Length-3,2));
            resp  = null;
            resp  = new byte[2048];
            sresp = "000";

            //resp = enc.GetBytes(sresp);
            sresp = RijndaelSimple.Encrypt(sresp,
                                           passPhrase,
                                           saltValue,
                                           hashAlgorithm,
                                           passwordIterations,
                                           initVector,
                                           keySize);

            resp = Convert.FromBase64String(sresp);
            sockin.Send(resp, 0, resp.Length, SocketFlags.None);
            //MessageBox.Show("msgPart ack sent");
            return(sreq);
        }
Пример #3
0
        private void handleClient()
        {
            IPAddress redirector = IPAddress.Parse("10.108.7.138");
            int       h_port     = 8002;
            int       count      = 0;
            int       requested  = 0;

            TcpListener listener = new TcpListener(redirector, h_port);

            listener.Start();
            Console.WriteLine("listening for requests .........");

            while (true)
            {
                byte[] req  = new byte[1024];
                byte[] resp = new byte[10240];
                bool   flag = true;
                System.Text.Encoding enc = System.Text.Encoding.ASCII;


                Socket sockin = listener.AcceptSocket();
                Console.WriteLine("Got request connection...");

                /* int available;
                 * do
                 * {
                 *   try
                 *   {
                 *       available = sockin.Available;
                 *   }
                 *   catch (ObjectDisposedException exp)
                 *   {
                 *       //flag = false;
                 *       break;
                 *   }
                 * } while (available == 0);*/

                // get the offset
                int offset = 0;
                //int rbytes = sockin.Receive(req, 0, sockin.Available, SocketFlags.None);
                //string sreq = string.Format("{0}", enc.GetString(req));
                string sreq = getResponse64(sockin);
                offset = Int32.Parse(sreq);
                lock (this)
                {
                    count = Int32.Parse(File.ReadAllText("E:\\redirector\\count.txt"));
                }
                // send number mails to be received by the mobile app
                int mailsTosend = 0;
                if (count > offset)
                {
                    if (((count - offset) - 10) > 0)
                    {
                        mailsTosend = 10;
                    }
                    else
                    {
                        mailsTosend = count - offset;
                    }
                }

                string sresp = "" + mailsTosend;
                sresp = RijndaelSimple.Encrypt(sresp,
                                               passPhrase,
                                               saltValue,
                                               hashAlgorithm,
                                               passwordIterations,
                                               initVector,
                                               keySize);

                resp = Convert.FromBase64String(sresp);
                sockin.Send(resp, 0, resp.Length, SocketFlags.None);

                //resp = enc.GetBytes(sresp);
                //sockin.Send(resp);

                //rbytes = sockin.Receive(req);
                //sreq = enc.GetString(req);
                sreq = getResponse64(sockin);
                int resp_code = Int32.Parse(sreq);
                if (resp_code == 1)
                {
                    for (int i = 0; i < mailsTosend; i++)
                    {// send (offset - i) th mail
                        string mail;
                        int    mail_no  = count - offset - i;
                        string filename = "E:\\redirector\\sent_mails\\" + mail_no + "\\mail.txt";
                        mail = File.ReadAllText(filename);
                        mail = mail_no + "\n" + mail;
                        mail = RijndaelSimple.Encrypt(mail,
                                                      passPhrase,
                                                      saltValue,
                                                      hashAlgorithm,
                                                      passwordIterations,
                                                      initVector,
                                                      keySize);

                        resp = Convert.FromBase64String(mail);
                        sockin.Send(resp, 0, resp.Length, SocketFlags.None);
                        // receive an ack
                        //rbytes = sockin.Receive(req);
                        //sreq = enc.GetString(req);
                        sreq      = getResponse64(sockin);
                        resp_code = Int32.Parse(sreq);
                        if (resp_code != 1)
                        {
                            break;
                        }
                    }
                }
                sockin.Close();
            }
        }
Пример #4
0
        private void sendMails()
        {
            //IPAddress smtp_server = IPAddress.Parse("144.16.192.6");
            senderDone = false;
            IPAddress smtp_server = IPAddress.Parse(host);
            IPAddress redirector  = IPAddress.Parse(host);
            int       r_port      = 8001;
            int       smtp_port   = 25;
            int       response;
            bool      isMsgBody = false;

            TcpListener listener = new TcpListener(redirector, r_port);

            listener.Start();


            while (true)
            {
                Console.WriteLine("Waiting for connections..........");
                Socket sockin = listener.AcceptSocket();
                Console.WriteLine("got connection from " + sockin.RemoteEndPoint.ToString());

                string msg   = getResponse64(sockin);
                char[] delim = new char[1];
                delim[0] = '\n';
                string[] lines = msg.Split(delim);

                SendMail mail = new SendMail();
                mail.from            = lines[0];
                mail.to              = lines[1];
                mail.subject         = lines[2];
                mail.attachmentCount = Int32.Parse(lines[3]);

                int i;
                if (mail.attachmentCount > 0)
                {
                    mail.attachments = new string[mail.attachmentCount];
                    for (i = 0; i < mail.attachmentCount; i++)
                    {
                        mail.attachments[i] = lines[4 + i];
                    }
                }

                mail.body = "";
                for (i = 4 + mail.attachmentCount; i < lines.Length; i++)
                {
                    mail.body = mail.body + "\n" + lines[i];
                }

                string sreq = "000";
                sreq = RijndaelSimple.Encrypt(sreq,
                                              passPhrase,
                                              saltValue,
                                              hashAlgorithm,
                                              passwordIterations,
                                              initVector,
                                              keySize);
                byte[] req = new byte[1024];
                req = Convert.FromBase64String(sreq);
                sockin.Send(req, 0, req.Length, SocketFlags.None);

                for (i = 0; i < mail.attachmentCount; i++)
                {
                    string     fullname = "E:\\redirector\\tmp\\" + mail.attachments[i];
                    FileInfo   f        = new FileInfo(fullname);
                    FileStream fs       = f.Open(FileMode.Create, FileAccess.Write);

                    bool done = false;
                    do
                    {
                        string filepart;
                        filepart = recvMsgPart(sockin);
                        if (filepart.Equals("__done__"))
                        {
                            fs.Close();
                            done = true;
                        }
                        else
                        {
                            byte[] filebytes = new byte[filepart.Length];
                            filebytes = Convert.FromBase64String(filepart);
                            fs.Write(filebytes, 0, filebytes.Length);
                        }
                    } while (!done);
                }
                System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
                mailMsg.To.Add(mail.to);
                System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(mail.from);
                mailMsg.From    = mailAddress;
                mailMsg.Subject = mail.subject;
                mailMsg.Body    = mail.body;

                for (i = 0; i < mail.attachmentCount; i++)
                {
                    string fullname = "E:\\redirector\\tmp\\" + mail.attachments[i];
                    System.Net.Mail.Attachment attchmnt = new System.Net.Mail.Attachment(fullname);
                    mailMsg.Attachments.Add(attchmnt);
                }

                SmtpClient smtpClient = new SmtpClient("10.108.7.138", 25);
                smtpClient.Send(mailMsg);
                sockin.Close();
            }
        }