Example #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            var masterKeyForm = new MasterKey();

            masterKeyForm.ShowDialog();
            GetMasterKey(masterKeyForm.Result);
            logout.Visible = false;

            if (File.Exists("RSAKey.bin"))
            {
                var serialize = Encoding.UTF8.GetString(AES.Decrypt(File.ReadAllBytes("RSAKey.bin"), masterAESKey, masterAESIV));
                keyPair = new RSAKeyPair(
                    Serializer.DeserializeKey(serialize.Split('$')[0])
                    );
                generateRSAButton.Enabled   = false;
                getSessionKeyButton.Enabled = true;
            }
            else
            {
                generateRSAButton.Enabled   = true;
                getSessionKeyButton.Enabled = false;
            }

            loginRejected.Visible = false;
            toggleLoginForm(false);
        }
Example #2
0
        private void ReceiveResponseFromServerAsync()
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    int messageType;
                    try
                    {
                        messageType = BitConverter.ToInt32(connection.Receive(4), 0);
                    }
                    catch (IOException e)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            toggleLoginForm(false);
                            blocknote.Visible           = false;
                            getSessionKeyButton.Enabled = true;
                            serverResponses.Visible     = true;
                            serverResponses.Text        = "session has been expired";
                        });

                        break;
                    }

                    var lenBytes = BitConverter.ToInt32(connection.Receive(4), 0);
                    Invoke((MethodInvoker) delegate
                    {
                        serverResponses.Visible = false;
                    });

                    byte[] msg = connection.Receive(lenBytes);

                    if (messageType == TCPConnection.ENCRYPTED_AES_WITH_RSA)
                    {
                        sessionAESKey = new byte[128];
                        Array.Copy(msg, 0, sessionAESKey, 0, 128);
                        sessionAESKey = keyPair.Decrypt(sessionAESKey);

                        sessionAESIV = new byte[128];
                        Array.Copy(msg, 128, sessionAESIV, 0, 128);
                        sessionAESIV = keyPair.Decrypt(sessionAESIV);

                        Invoke((MethodInvoker) delegate
                        {
                            getSessionKeyButton.Enabled = false;
                        });
                    }
                    else if (messageType == TCPConnection.LOGIN_APPROVED)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            toggleLoginForm(false);
                            loginRejected.Visible = false;
                            blocknote.Visible     = true;
                        });
                    }
                    else if (messageType == TCPConnection.LOGIN_REJECTED)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            toggleLoginForm(true);
                            loginRejected.Visible = true;
                        });
                    }
                    else if (messageType == TCPConnection.TEXT)
                    {
                        var text = Encoding.UTF8.GetString(AES.Decrypt(msg, sessionAESKey, sessionAESIV));
                        Invoke((MethodInvoker) delegate
                        {
                            textBox1.Text = text;
                        });
                    }
                    else if (messageType == TCPConnection.FILE_DO_NOT_EXISTS)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            serverResponses.Visible = true;
                            serverResponses.Text    = "file with such a filename does not exist";
                        });
                    }
                    else if (messageType == TCPConnection.FILE_SAVED)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            serverResponses.Visible = true;
                            serverResponses.Text    = "file was saved";
                        });
                    }
                    else
                    {
                        break;
                    }
                }
            });
        }