private void DisableEncryptionInterface(string text) { if (this.richMessage.InvokeRequired) { SetTextCallback d = new SetTextCallback(DisableEncryptionInterface); this.Invoke(d, new object[] { text }); } else { crypter = null; isPublicSertificateSent = false; btnEncrypt.Image = Resources.lock_open; AddMessageToRichChat("=========================================" + Environment.NewLine); AddMessageToRichChat("System: Encryption has been disabled" + Environment.NewLine); toolTip1.SetToolTip(btnEncrypt, "Enable Secured Mode"); btnEncrypt.Enabled = true; } }
private void btnEncrypt_Click(object sender, EventArgs e) { btnEncrypt.Enabled = false; if (crypter == null) { btnEncrypt.Image = Resources._lock; AddMessageToRichChat("System: Initializing encryption procedure" + Environment.NewLine); AddMessageToRichChat("System: Creating RSA-2048 certificates" + Environment.NewLine); crypter = new RSACrypter(); AddMessageToRichChat("System: The public certificate has been sent to the partner" + Environment.NewLine); SendMessage("$PUBLICKEY$" + crypter.PublicKey); isPublicSertificateSent = true; toolTip1.SetToolTip(btnEncrypt, "Disable Secured Mode"); btnEncrypt.Enabled = true; } else { SendMessage("$DISENCRYPT$", true); DisableEncryptionInterface(null); } }
void DoListen() { AddMessageToRichChat("System: Connecting..." + Environment.NewLine); if (isServer) { client = listener.AcceptTcpClient(); } NetworkStream netStream = client.GetStream(); AddMessageToRichChat("System: Connection established" + Environment.NewLine); EnableInterface(null); while (true) { if (netStream.CanRead) { int numberOfBytesRead = 0; string str = ""; try { numberOfBytesRead = netStream.Read(myReadBuffer, 0, myReadBuffer.Length); str += Encoding.Unicode.GetString(myReadBuffer, 0, numberOfBytesRead); if (str.Contains("$PUBLICKEY$")) { if (crypter == null) { crypter = new RSACrypter(); } crypter.ThirdPartyPublicKey = str.Replace("$PUBLICKEY$", ""); AddMessageToRichChat("System: The partner's public certificate has been accepted" + Environment.NewLine); if (!isPublicSertificateSent) { AddMessageToRichChat("System: Creating RSA-2048 certificates" + Environment.NewLine); AddMessageToRichChat("System: The public certificate has been sent to the partner" + Environment.NewLine); SendMessage("$PUBLICKEY$" + crypter.PublicKey); EnableEncryptionInterface(null); } AddMessageToRichChat("=========================================" + Environment.NewLine); AddMessageToRichChat("System: Secure connection successfully established" + Environment.NewLine); continue; } if (crypter == null) { AddMessageToRichChat("Partner: " + str + Environment.NewLine); } else { byte[] encrypted_buffer = new byte[numberOfBytesRead]; for (int i = 0; i < numberOfBytesRead; i++) { encrypted_buffer[i] = myReadBuffer[i]; } string message = crypter.decryptMessage(encrypted_buffer); if (!message.Contains("$DISENCRYPT$")) { AddMessageToRichChat("Partner: " + message + Environment.NewLine); } else { DisableEncryptionInterface(null); } } } catch (Exception ex) { netStream.Close(); AddMessageToRichChat("System: Partner disconnected. Reason: " + ex.Message + Environment.NewLine); DisableInterface(null); break; } } else { MessageBox.Show("Can`t read data!"); } } }