private void SendMessages()
 {
     while (!_shouldClose)
     {
         try
         {
             string msg = string.Empty;
             if (!_toSendMsgQueue.IsEmpty)
             {
                 _toSendMsgQueue.TryDequeue(out msg);
                 // Translate the passed message into ASCII and store it as a Byte array.
                 byte[] data      = Encoding.UTF8.GetBytes(msg);
                 string s         = Encoding.UTF8.GetString(data);
                 byte[] encrypted = _security.Encrypt(s);
                 s = Encoding.UTF8.GetString(encrypted);
                 s = _security.Decrypt(encrypted);
                 // Send the message to the connected TcpServer.
                 _stream.Write(encrypted, 0, encrypted.Length);
             }
         }
         catch (SocketException e)
         {
             Console.WriteLine("SocketException: {0}", e);
         }
     }
 }
Example #2
0
        public void Execute(IJobExecutionContext context)
        {
            string username = _security.Decrypt(context.JobDetail.JobDataMap.GetString("username"));
            string password = _security.Decrypt(context.JobDetail.JobDataMap.GetString("password"));

            var stats = _monitor.Get(username, password);

            Console.WriteLine("[{0}] Channel monitor fired.", context.FireTimeUtc.Value.ToString("MM/dd/yyyy hh:mm:ss"));

            //foreach (var channel in stats)
            //{
            //    Console.WriteLine("Acknowledged = {0}", channel.Acknowledged);
            //}
        }
Example #3
0
        //[TestCase]
        //public void SendingManyShortMsgTest()
        //{
        //    const int numOfMsgs = 5;

        //    var task = Task.Factory.StartNew(() => _commHandler.Start());
        //    _client = ConnectSocketLoopback();

        //    TcpClient socketAtServer;
        //    _commHandler.SocketsQueue.TryPeek(out socketAtServer);

        //    Assert.IsNotNull(socketAtServer);
        //    _commHandler.SetUserIdToSocket(UserId, socketAtServer);

        //    for (int i = 0; i < numOfMsgs; i++)
        //    {
        //        bool sent = _commHandler.AddMsgToSend(ShortMsg, UserId);
        //        Assert.True(sent);
        //    }

        //    var msgQueue = _commHandler.UserIdToMsgQueue[UserId];
        //    Assert.IsNotNull(msgQueue);


        //    //wait for server to take the msg from the queue:
        //    while (!msgQueue.IsEmpty)
        //    {
        //        Thread.Sleep(200);
        //    }

        //    int bytesRead = 0;
        //    bool stillGotMsgs = true;
        //    while (stillGotMsgs)
        //    {
        //        try
        //        {
        //            string msg = ClientRead(2000);
        //            bytesRead += msg.Length;
        //        }
        //        catch (Exception)
        //        {
        //            stillGotMsgs = false;
        //        }
        //    }

        //    Assert.AreEqual(bytesRead, numOfMsgs * ShortMsg.Length);

        //    CloseHandlerAndClient();

        //    task.Wait();
        //    Assert.True(task.IsCompleted);
        //}

        private string ClientRead(int timeOutMili)
        {
            _client.ReceiveTimeout = timeOutMili;
            byte[]        buffer = new byte[1];
            IList <byte>  data   = new List <byte>();
            NetworkStream stream = _client.GetStream();

            try
            {
                var dataReceived = 0;
                do
                {
                    dataReceived = stream.Read(buffer, 0, 1);
                    if (dataReceived > 0)
                    {
                        data.Add(buffer[0]);
                    }
                } while (dataReceived > 0);
            }
            catch (IOException e)
            {
                //_client timeout was reached
            }

            if (data.Count > 0)
            {
                return(_security.Decrypt(data.ToArray()));
            }

            throw new Exception("no data was read by _client");
        }
Example #4
0
        //thread 1
        protected void HandleReading()
        {
            Console.WriteLine("comm: reading");
            byte[] buffer = new byte[1];

            while (!ShouldClose)
            {
                IList <TcpClient> readyToRead = Selector.SelectForReading(_socketsQueue);
                foreach (var tcpClient in readyToRead)
                {
                    IList <byte>  data         = new List <byte>();
                    NetworkStream stream       = tcpClient.GetStream();
                    var           dataReceived = 0;
                    do
                    {
                        dataReceived = stream.Read(buffer, 0, 1);
                        if (dataReceived > 0)
                        {
                            data.Add(buffer[0]);
                        }
                    } while (dataReceived > 0 && stream.DataAvailable);

                    //add msg string to queue
                    if (data.Count > 0)
                    {
                        string msgStr = _security.Decrypt(data.ToArray());
                        _receivedMsgQueue.Enqueue(new Tuple <string, TcpClient>(msgStr, tcpClient));
                        Console.WriteLine("comm: msg is: " + Encoding.UTF8.GetString(data.ToArray()));
                    }
                }
            }
        }
Example #5
0
        public void DecryptionTest()
        {
            var ptBytes   = Encoding.UTF8.GetBytes("This is a plaintext msg");
            var encrypted = _security.Encrypt(Encoding.UTF8.GetString(ptBytes));
            var s         = Encoding.UTF8.GetString(encrypted);

            s = _security.Decrypt(encrypted);
            Console.WriteLine(s);
            Assert.AreEqual(s, "This is a plaintext msg");
        }
Example #6
0
        public void Execute(IJobExecutionContext context)
        {
            string username = _security.Decrypt(context.JobDetail.JobDataMap.GetString("username"));
            string password = _security.Decrypt(context.JobDetail.JobDataMap.GetString("password"));

            var stats = _queueMonitor.Get(username, password);

            Console.WriteLine("[{0}] Queue monitor fired.", context.FireTimeUtc.Value.ToString("MM/dd/yyyy hh:mm:ss"));
            //Console.WriteLine("Messages: {0}", stats.Messages);
            //Console.WriteLine("Messages Ready: {0}", stats.MessagesReady);
            //Console.WriteLine("Messages Unacknowledged: {0}", stats.MessagesUnacknowledged);
            //Console.WriteLine("Rate: {0}", stats.QueueTotals.MessagesDetails.Rate);
            //Console.WriteLine("Interval: {0}", stats.QueueTotals.MessagesDetails.Interval);
            //Console.WriteLine("Last Event: {0}", stats.QueueTotals.MessagesDetails.LastEvent);
            //Console.WriteLine("Rate: {0}", stats.QueueTotals.MessagesReadyDetails.Rate);
            //Console.WriteLine("Interval: {0}", stats.QueueTotals.MessagesReadyDetails.Interval);
            //Console.WriteLine("Last Event: {0}", stats.QueueTotals.MessagesReadyDetails.LastEvent);
            //Console.WriteLine("Rate: {0}", stats.QueueTotals.MessagesUnacknowledgedDetails.Rate);
            //Console.WriteLine("Interval: {0}", stats.QueueTotals.MessagesUnacknowledgedDetails.Interval);
            //Console.WriteLine("Last Event: {0}", stats.QueueTotals.MessagesUnacknowledgedDetails.LastEvent);
            //Console.WriteLine("****************************************");

            //_queueMonitor.Save(heartbeat);
        }
        private void btn_Decrypt_Click(object sender, EventArgs e)
        {
            if (!ValidateText(txt_CypherText, "Enter text to decrypt"))
            {
                return;
            }
            string strCypher    = txt_CypherText.Text.Trim().ToLower().Replace(" ", "");
            string strPlainText = "";

            switch (cmb_Algorithm.SelectedIndex)
            {
            case (int)Algorithm.Ceaser:
                securityModel = new Ceaser();
                strPlainText  = securityModel.Decrypt(strCypher, new object[1] {
                    nm_Key.Value
                });
                break;

            case (int)Algorithm.Autokey:
                if (ValidateKey())
                {
                    securityModel = new AutoKey();
                    strPlainText  = securityModel.Decrypt(strCypher, new object[1] {
                        txt_Key.Text
                    });
                }
                break;

            case (int)Algorithm.Hill:
                if (!bKeyGenerated)
                {
                    MessageBox.Show("please set key", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                securityModel = new Hill();
                strPlainText  = securityModel.Decrypt(strCypher, new object[2] {
                    m_frmMatrix.m_arrMatrix, nm_Key.Value
                });
                break;

            case (int)Algorithm.Monoalphabetic:
                securityModel = new Monoalphabetic();
                strPlainText  = securityModel.Decrypt(strCypher, new object[] { });
                break;

            case (int)Algorithm.Playfair:
                if (ValidateKey())
                {
                    securityModel = new PlayFair();
                    strPlainText  = securityModel.Decrypt(strCypher, new object[1] {
                        txt_Key.Text
                    });
                }
                break;

            case (int)Algorithm.RailFence:
                securityModel = new RailFence();
                strPlainText  = securityModel.Decrypt(strCypher, new object[1] {
                    nm_Key.Value
                });
                break;

            case (int)Algorithm.Vigenere:
                if (ValidateKey())
                {
                    securityModel = new Vigenere();
                    strPlainText  = securityModel.Decrypt(strCypher, new object[1] {
                        txt_Key.Text
                    });
                }
                break;

            case (int)Algorithm.RowTransposition:
                if (ValidateKey())
                {
                    securityModel = new RowTransposition();
                    strPlainText  = securityModel.Decrypt(strCypher, new object[1] {
                        txt_Key.Text
                    });
                }
                break;
            }
            txt_PlainText.Text = strPlainText;
        }
 private void btn_Decrypt_Click(object sender, EventArgs e)
 {
     if (!ValidateText(txt_CypherText, "Enter text to decrypt"))
     {
         return;
     }
     string strCypher = txt_CypherText.Text.Trim().ToLower().Replace(" ", "");
     string strPlainText = "";
     switch (cmb_Algorithm.SelectedIndex)
     {
         case (int)Algorithm.Ceaser:
             securityModel = new Ceaser();
             strPlainText = securityModel.Decrypt(strCypher, new object[1] { nm_Key.Value });
             break;
         case (int)Algorithm.Autokey:
             if (ValidateKey())
             {
                 securityModel = new AutoKey();
                 strPlainText = securityModel.Decrypt(strCypher, new object[1] { txt_Key.Text });
             }
             break;
         case (int)Algorithm.Hill:
             if (!bKeyGenerated)
             {
                 MessageBox.Show("please set key", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 return;
             }
             securityModel = new Hill();
             strPlainText = securityModel.Decrypt(strCypher, new object[2] { m_frmMatrix.m_arrMatrix, nm_Key.Value });
             break;
         case (int)Algorithm.Monoalphabetic:
             securityModel = new Monoalphabetic();
             strPlainText = securityModel.Decrypt(strCypher, new object[] { });
             break;
         case (int)Algorithm.Playfair:
             if (ValidateKey())
             {
                 securityModel = new PlayFair();
                 strPlainText = securityModel.Decrypt(strCypher, new object[1] { txt_Key.Text });
             }
             break;
         case (int)Algorithm.RailFence:
             securityModel = new RailFence();
             strPlainText = securityModel.Decrypt(strCypher, new object[1] { nm_Key.Value });
             break;
         case (int)Algorithm.Vigenere:
             if (ValidateKey())
             {
                 securityModel = new Vigenere();
                 strPlainText = securityModel.Decrypt(strCypher, new object[1] { txt_Key.Text });
             }
             break;
         case (int)Algorithm.RowTransposition:
             if (ValidateKey())
             {
                 securityModel = new RowTransposition();
                 strPlainText = securityModel.Decrypt(strCypher, new object[1] { txt_Key.Text });
             }
             break;
     }
     txt_PlainText.Text = strPlainText;
 }