예제 #1
0
 public string decryptMessage(string message)
 {
     if (SymmKey != null)
     {
         EncryptionAES aes = new EncryptionAES(SymmKey);
         return aes.Decrypt(message);
     }
     return string.Empty;
 }
        private void OnMessageReceivedEventHandler(MessageData messageData)
        {
            Dispatcher.Invoke(() =>
                {
                    _logger.Debug($"Received message: '{messageData.UserInfo.Message}' from '{messageData.UserInfo.Username}'" +
                                  $" Message type: '{messageData.UserInfo.MessageType}'");
                    if (messageData.UserInfo.MessageType == MessageType.Connect ||
                        messageData.UserInfo.MessageType == MessageType.Disconnect)
                    {
                        ChatTextBox.AppendText($"{messageData.UserInfo.Message}{Environment.NewLine}");
                    }

                    else if (messageData.UserInfo.MessageType == MessageType.ParameterE)
                    {
                        encryption.serverE = new BigInteger(messageData.UserInfo.Message,10);
                    }
                    else if (messageData.UserInfo.MessageType == MessageType.ParameterN)
                    {
                        encryption.serverN = new BigInteger(messageData.UserInfo.Message,10);
                    }

                    else if (messageData.UserInfo.MessageType == MessageType.CryptoMessage)
                    {
                        // encrypted symm key, receiver, encrypted message
                        string[] receiverUsernameAndMessage = messageData.UserInfo.Message.Split(new char[] { ',' }, 3);
                        if (_username == receiverUsernameAndMessage[1])
                        {
                            string decryptedSymmKey = encryptionRsa.DecryptSymmetricKey(receiverUsernameAndMessage[0],
                                encryption.D, encryption.N);
                            EncryptionAES aes = new EncryptionAES(decryptedSymmKey);
                            string message = aes.Decrypt(receiverUsernameAndMessage[2]);
                            message.TrimEnd();
                            ChatTextBox.AppendText($"{messageData.UserInfo.Username}: (private) {message} {Environment.NewLine}");
                        }
                        else
                        {
                            ChatTextBox.AppendText($"{messageData.UserInfo.Username}: {receiverUsernameAndMessage[2]}" +
                                                   $"{Environment.NewLine}");
                        }
                    }
                    else
                    {
                        ChatTextBox.AppendText(
                            $"{messageData.UserInfo.Username}: {messageData.UserInfo.Message}{Environment.NewLine}");
                    }
                });
        }
        private string encryptMessage(string message, string receiverUsername, string senderUsername)
        {
            BigInteger n = 0, e = 0;
            string encryptedSymmKey = "";
            foreach (var user in Program.ConnectionManager.UsersList)
            {
                if (user.Username == receiverUsername)
                {
                    n = user.N;
                    e = user.E;
                    break;
                }
                if (user.Username == senderUsername)
                {
                    encryptedSymmKey = user.symmKey;
                }
            }

            string decryptedSymmKey = _serverKeys.DecryptSymmetricKey(encryptedSymmKey, _serverKeys.D, _serverKeys.N);
            symmKeyBytes            = _serverKeys.EncryptSymmetricKey(decryptedSymmKey, e, n);

            var EncryptionAES = new EncryptionAES(decryptedSymmKey);

            return EncryptionAES.Encrypt(message);
        }