示例#1
0
        private void generateRSAButton_Click(object sender, EventArgs e)
        {
            getSessionKeyButton.Enabled = true;
            generateRSAButton.Enabled   = false;

            keyPair = new RSAKeyPair();
            var serialized       = Serializer.SerializeKey(keyPair.privateKey) + "$";
            var keyPairEncrypted = AES.Encrypt(Encoding.UTF8.GetBytes(serialized), masterAESKey, masterAESIV);

            File.WriteAllBytes("RSAKey.bin", keyPairEncrypted);
        }
示例#2
0
        private void getTextButton_Click(object sender, EventArgs e)
        {
            var filename = filenameBox.Text;

            filename = "first.txt";
            var filenameArray = AES.Encrypt(Encoding.UTF8.GetBytes(filename), sessionAESKey, sessionAESIV);

            if (filename.Length != 0)
            {
                connection.Send(TCPConnection.FILENAME, filenameArray);
            }
        }
示例#3
0
        private void sendTextButton_Click(object sender, EventArgs e)
        {
            if (filenameBox.Text.Length != 0)
            {
                var text           = AES.Encrypt(Encoding.Default.GetBytes(textBox1.Text), sessionAESKey, sessionAESIV);
                var filename       = AES.Encrypt(Encoding.Default.GetBytes(filenameBox.Text), sessionAESKey, sessionAESIV);
                var filenameLength = BitConverter.GetBytes(filename.Length);

                var msg = new byte[4 + filename.Length + text.Length];
                Array.Copy(filenameLength, 0, msg, 0, 4);
                Array.Copy(filename, 0, msg, 4, filename.Length);
                Array.Copy(text, 0, msg, 4 + filename.Length, text.Length);

                connection.Send(TCPConnection.TEXT, msg);
            }
        }
示例#4
0
        private void SendLogin(TcpClient client, string login, string password)
        {
            login    = textBoxLogin.Text;
            password = textBoxPassword.Text;
            byte[] loginEcnr    = AES.Encrypt(Encoding.Default.GetBytes(login), sessionAESKey, sessionAESIV);
            byte[] passwordEcnr = AES.Encrypt(Encoding.Default.GetBytes(password), sessionAESKey, sessionAESIV);
            var    loginLen     = BitConverter.GetBytes(loginEcnr.Length);

            var msg = new byte[4 + loginEcnr.Length + passwordEcnr.Length];

            Array.Copy(loginLen, 0, msg, 0, 4);
            Array.Copy(loginEcnr, 0, msg, 4, loginEcnr.Length);
            Array.Copy(passwordEcnr, 0, msg, 4 + loginEcnr.Length, passwordEcnr.Length);

            connection.Send(TCPConnection.LOGIN, msg);
        }
示例#5
0
        private void generateQrCode(byte[] key)
        {
            Totp totp = new Totp(key);

            string totpCode = totp.ComputeTotp(DateTime.UtcNow);

            QRCodeGenerator qr     = new QRCodeGenerator();
            var             secret = "otpauth://totp/Example:" + login + "?secret=" + Base32Encoding.ToString(key) + "&issuer=Example";
            QRCodeData      qrData = qr.CreateQrCode(secret, QRCodeGenerator.ECCLevel.Q);
            QRCode          code   = new QRCode(qrData);

            QRCodeForm qrForm = new QRCodeForm();

            qrForm.QRPic = code.GetGraphic(2);
            qrForm.ShowDialog();
            var codeFromUser = qrForm.Password;

            connection.Send(TCPConnection.QR_PASS_FROM_USER, AES.Encrypt(Encoding.UTF8.GetBytes(codeFromUser), sessionAESKey, sessionAESIV));
        }
示例#6
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 if (messageType == TCPConnection.QR_CODE_GENERATED)
                    {
                        var key = AES.Decrypt(msg, sessionAESKey, sessionAESIV);

                        generateQrCode(key);
                    }
                    else if (messageType == TCPConnection.USE_OLD_KEY)
                    {
                        QRCodeForm qrForm = new QRCodeForm();
                        qrForm.ShowDialog();
                        var codeFromUser = qrForm.Password;
                        connection.Send(TCPConnection.QR_PASS_FROM_USER, AES.Encrypt(Encoding.UTF8.GetBytes(codeFromUser), sessionAESKey, sessionAESIV));
                    }
                    else
                    {
                        break;
                    }
                }
            });
        }