Beispiel #1
0
        private bool IsIdValid(Message message)
        {
            if (message.Id <= 0)
                throw new ArgumentException("Message Id should be positive");

            return true;
        }
Beispiel #2
0
        public int AddMessage(Message message)
        {
            if (!IsMessageValid(message)) return -1;
            else if(!IsIdValid(message)) return -1;

            return Model.AddMessage(message);
        }
Beispiel #3
0
        private bool IsDateValid(Message message)
        {
            if (message.Date == null)
                message.Date = DateTime.Now;
            if(message.Date.)

            return true;
        }
        public void Notify(Messages.Message message)
        {
            string json = JsonConvert.SerializeObject(message);

            Console.WriteLine($"sending message to topic {_topic}");
            var dr = _producer.ProduceAsync(_topic, null, json).Result;

            Console.WriteLine("message sent");
        }
Beispiel #5
0
 public int AddMessage(Message message)
 {
     var index = messages.FindIndex(msg => msg.Id == message.Id);
     if (index >= 0)
     {
         message.Id = messages.Max(msg => msg.Id) + 1;
     }
     messages.Add(message);
     return message.Id;
 }
Beispiel #6
0
        public static byte[] GetSerializedMessage(Message message)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            using (MemoryStream stream = new MemoryStream())
            {
                bFormatter.Serialize(stream, message);
                byte[] buffer = stream.ToArray();

                //int count = await stream.ReadAsync(buffer, 0, buffer.Length);
                return buffer;
            }
        }
        protected void AddMessage_Click(object sender, EventArgs e)
        {
            MessagesEntities context = new MessagesEntities();

            Message newMessage = new Message()
            {
                Text = this.NewMessage.Text
            };

            context.Messages.Add(newMessage);
            context.SaveChanges();

            this.NewMessage.Text = "";
        }
Beispiel #8
0
        public void getlogs()
        {
            LogRequest objlogRequest = new LogRequest();

            objlogRequest.author          = "Karthik";
            objlogRequest.TestRequestName = "TestRequest1";
            Messages.Message logMessage = new Messages.Message();
            logMessage.author = "karu";
            logMessage.body   = objlogRequest.ToXml();
            logMessage.to     = Comm <Client> .makeEndPoint(url, repoPort);

            logMessage.from = Comm <Client> .makeEndPoint(url, clientPort);

            logMessage.type = "LogRequest";
            logMessage.time = DateTime.Now;
            objComm.sndr.PostMessage(logMessage);
        }
Beispiel #9
0
        private bool IsMessageValid(Message message)
        {
            if (message.Text == null)
            {
                throw new ArgumentNullException("message", "message.Text should not be null");
            }

            if (message.Text.Length <= 0)
            {
                throw new ArgumentOutOfRangeException("message", "message text should not be empty");
            }

            if (message.Text.Length > 20)
            {
                throw new ArgumentOutOfRangeException("message", "message text length should not be > 20");
            }
            return true;
        }
 /// <summary>
 /// metoda sluzaca do wyslania prosby o klucz danego uzytkownika do serwera
 /// </summary>
 private void GetAesKey()
 {
     var selected = (User)onlineUsersListBox.SelectedItem;
     var writer = new BinaryWriter(client.GetStream());
     var start = new Messages.Message(user.Username, "server", selected.Username, SignString(selected.Username));
     var startEncrypted = Security.EncryptMessage(start, serverPK.ExportParameters(false));
     writer.Write(startEncrypted);
 }
 /// <summary>
 /// metoda sluzaca do wyslania wiadomosci do drugiego uzytkownika
 /// </summary>
 private void SendMessage()
 {
     if (!string.IsNullOrEmpty(MessageInputTextBox.Text))
     {
         var selected = (User)onlineUsersListBox.SelectedItem;
         var writer = new BinaryWriter(client.GetStream());
         var aes = GetAes(selected.Username);
         var message = new Messages.Message(user.Username, selected.Username, MessageInputTextBox.Text, SignString(MessageInputTextBox.Text));
         var encryptedMessage = Security.EncryptMessage(message, serverPK.ExportParameters(false), aes.Key, aes.IV);
         var text = string.Format("{0} You wrote: {1} ", DateTime.Now.ToString(), MessageInputTextBox.Text);
         AddOrUpdateDictionary(selected.Username, text);
         messengerRichTextBox.ChangeText(text, true);
         MessageInputTextBox.Text = string.Empty;
         writer.Write(encryptedMessage); 
     }
 }
        // Получить пакет.
        public byte[] Encrypt(Message m)
        {
            int type = -1;
            if (m is Inform_of_Down_Message)
            {
                type = 0;
            }
            if (m is Inform_of_Rec_Message)
            {
                type = 1;
            }
            if (m is ListMessage)
            {
                type = 2;
            }
            if (m is RequestMessage)
            {
                type = 3;
            }
            if (m is FilePartMessage)
            {
                type = 4;
            }
            if (m is EndMessage)
            {
                type = 5;
            }
            if (m is ResponseMessage)
            {
                type = 6;
            }
            if (m is ErrorMessage)
            {
                type = 7;
            }
            if (m is HubInformMessage)
            {
                type = 8;
            }

            // Буффер для передачи данных.
            byte[] bytes = new byte[1024];
            // Вспомогательная строка для составления пакета.
            string assis = string.Empty;
            // Формирование пакета.
            assis += type.ToString();
            assis += m.Get_Data();
            // Кодировка пакета.
            bytes = Encoding.ASCII.GetBytes(assis);

            return bytes;
        }
 // Раскрыть пакет.
 public Message Decrypt(byte[] b)
 {
     // Возвращаемое значение.
     Message mes;
     // Строка, полученная из полученного массива байт.
     string str = Encoding.ASCII.GetString(b);
     // Тип сообщений.
     if (str[0] != '\0')
     {
         int type = int.Parse(str[0].ToString());
         int border = str.IndexOf('\0');
         if (border==(-1))
         {
             border = str.Length;
         }
         if (type != -1)
         {
             // Строка данных.
             string assis = str.Substring(1, border - 1);
             // Определение типа сообщения.
             switch (type)
             {
                 case 0:
                     mes = new Inform_of_Down_Message(assis);
                     break;
                 case 1:
                     mes = new Inform_of_Rec_Message(assis);
                     break;
                 case 2:
                     mes = new ListMessage(assis);
                     break;
                 case 3:
                     mes = new RequestMessage(assis);
                     break;
                 case 4:
                     mes = new FilePartMessage(assis);
                     break;
                 case 5:
                     mes = new EndMessage(assis);
                     break;
                 case 6:
                     mes = new ResponseMessage(assis);
                     break;
                 case 7:
                     mes = new ErrorMessage(assis);
                     break;
                 case 8:
                     mes = new HubInformMessage(assis);
                     break;
                 default:
                     mes = new Message(assis);
                     break;
             }
         }
         else
             mes = new Message(string.Empty);
         return mes;
     }
     else
         return new Message(string.Empty);
 }
 public void Change_Data(string s, Message m)
 {
     m.Set_Data(s);
 }
Beispiel #15
0
 static void HandleTextMessage(Message textMessage)
 {
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine("Got message: {0}", textMessage.Text);
     Console.ResetColor();
 }
Beispiel #16
0
 public Chat(Model.Contact i)
 {
     hilo = new Thread(new ThreadStart(up));
     contact = i;
     viewModel = new Message(i);
     hilo.Start();
     DataContext = viewModel;            
     InitializeComponent();
     ScrollViewer Scr = (ScrollViewer)scroll;            
     Scr.ScrollToEnd();
     Title = i.userName;
 }
 /// <summary>
 /// metoda do szyfrowania wiadomosci algorytmen AES
 /// </summary>
 /// <param name="message">wiadomosc do zaszyfrowania</param>
 /// <param name="rsaKeyInfo">klucze RSA</param>
 /// <param name="aesKey">klucz tajny AES</param>
 /// <param name="aesIV">wektor inicjalizacji</param>
 /// <returns>zserializowana i zaszyfrowana wiadomosc</returns>
 public static string EncryptMessage(Message message, RSAParameters rsaKeyInfo, byte[] aesKey, byte[] aesIV)
 {
     var converter = new UnicodeEncoding();
     var sender = converter.GetBytes(message.Sender);
     var receiver = converter.GetBytes(message.Receiver);
     string encryptedSender = Convert.ToBase64String(RSAEncrypt(sender, rsaKeyInfo, false));
     string encryptedReceiver = Convert.ToBase64String(RSAEncrypt(receiver, rsaKeyInfo, false));
     string encryptedData = Convert.ToBase64String(AESEncrypt(message.data, aesKey, aesIV));
     return MessageSerializer.Serialize(new Message(encryptedSender, encryptedReceiver, encryptedData, message.Signature), typeof(Message));
 }
 /// <summary>
 /// metoda do deszyfrowania wiadomosci algorytmen AES
 /// </summary>
 /// <param name="message">wiadomosc do odszyfrowania</param>
 /// <param name="rsaKeyInfo">klucze RSA</param>
 /// <param name="aesKey">klucz tajny AES</param>
 /// <param name="aesIV">wektor inicjalizacji</param>
 /// <returns>zserializowana i odszyfrowana wiadomosc</returns>
 public static string DecryptMessage(Message message, RSAParameters rsaKeyInfo, byte[] aesKey, byte[] aesIV)
 {
     var converter = new UnicodeEncoding();
     var encryptedSender = Convert.FromBase64String(message.Sender);
     var encryptedReceiver = Convert.FromBase64String(message.Receiver);
     string decryptedSender = converter.GetString(RSADecrypt(encryptedSender, rsaKeyInfo, false));
     string decryptedReceiver = converter.GetString(RSADecrypt(encryptedReceiver, rsaKeyInfo, false));
     string decryptedData = AESDecrypt(Convert.FromBase64String(message.data), aesKey, aesIV);
     return MessageSerializer.Serialize(new Message(decryptedSender, decryptedReceiver, decryptedData, message.Signature), typeof(Message));
 }