1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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<String>,
error: Option<String>,
}
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::<Vec<&str>>();
if split.len() != 2 {
self.encrypted = None;
self.error = Some("Неверный формат ключа".to_string());
return;
}
let e = match split[0].parse::<u64>() {
Ok(num) => num,
Err(_) => {
self.encrypted = None;
self.error = Some("Не удалось прочитать открытую экспоненту".to_string());
return;
}
};
let n = match split[1].parse::<u64>() {
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);
}
}
}
|