From 4862c2cf3eafdb6fa017a4e86a6fad8b4fc64171 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Sun, 13 Nov 2022 11:50:28 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab2/src/encrypt_window.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 lab2/src/encrypt_window.rs (limited to 'lab2/src/encrypt_window.rs') diff --git a/lab2/src/encrypt_window.rs b/lab2/src/encrypt_window.rs new file mode 100644 index 0000000..62899ba --- /dev/null +++ b/lab2/src/encrypt_window.rs @@ -0,0 +1,92 @@ +use crate::utils::{powmod, to_bytes}; +use crate::window_state::WindowState; +use base64; +use eframe::egui; + +pub struct Window { + message: String, + key: String, + encrypted: Option, + error: Option, +} + +impl Default for Window { + fn default() -> Self { + Self { + message: String::new(), + key: String::new(), + encrypted: None, + error: None, + } + } +} + +impl Window { + fn encrypt(&mut self) { + let split = self.key.split(",").collect::>(); + if split.len() != 2 { + self.encrypted = None; + self.error = Some("Неверный формат ключа".to_string()); + return; + } + let e = match split[0].parse::() { + Ok(num) => num, + Err(_) => { + self.encrypted = None; + self.error = Some("Не удалось прочитать открытую экспоненту".to_string()); + return; + } + }; + let n = match split[1].parse::() { + Ok(num) => num, + Err(_) => { + self.encrypted = None; + self.error = Some("Не удалось прочитать модуль".to_string()); + return; + } + }; + + let mut cipher = Vec::new(); + for byte in self.message.clone().into_bytes() { + let encr = powmod(byte as u64, e, n); + let bytes = to_bytes(encr); + cipher.push(bytes.0); + cipher.push(bytes.1); + cipher.push(bytes.2); + cipher.push(bytes.3); + } + self.encrypted = Some(base64::encode(cipher)); + } +} + +impl WindowState for Window { + fn get_name(&self) -> &str { + "Шифрование сообщения" + } + + fn update(&mut self, ui: &mut egui::Ui) { + ui.label("Сообщение для зашифрования:"); + let resp1 = ui.add(egui::TextEdit::multiline(&mut self.message)); + ui.label("Открытый ключ:"); + let resp2 = ui.add(egui::TextEdit::singleline(&mut self.key)); + if resp1.changed() || resp2.changed() { + self.encrypted = None; + self.error = None; + } + if ui.button("Зашифровать").clicked() { + self.encrypt(); + } + + if let Some(cipertext) = &self.encrypted { + let mut tmp = cipertext.clone(); + ui.label("Зашифрованное сообщение:"); + ui.add_enabled(false, egui::TextEdit::multiline(&mut tmp)); + if ui.button("Скопировать шифротекст").clicked() { + ui.output().copied_text = tmp.clone(); + } + } + if let Some(error) = &self.error { + ui.label(error); + } + } +} -- cgit v1.2.3