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)); }
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; } } }); }