summaryrefslogtreecommitdiff
path: root/sem2/src/elgamal.rs
blob: c53622e402de3fed818b6a09c74bed32e67bc632 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::algo;
use crate::mpn::Number;
use std::collections::HashMap;

#[derive(Debug)]
pub struct OpenKey {
    pub y: Number,
    pub g: Number,
    pub p: Number,
}

#[derive(Debug)]
pub struct SecretKey {
    pub x: Number,
}

fn factorization(x: &Number) -> Vec<Number> {
    let mut factors = Vec::new();
    let mut n = x.clone();
    let mut i: Number = 2.into();
    let mut flag = true;
    while i.clone() * i.clone() <= n.clone() && flag {
        if n.clone() % i.clone() == 0.into() {
            factors.push(i.clone());
            while n.clone() % i.clone() == 0.into() {
                n = n.clone() / i.clone();
                if algo::rabin_miller_test(&n, 10) {
                    flag = false;
                    break;
                }
            }
        }
        i = i.clone() + 1.into();
    }
    if n.clone() > 1.into() {
        factors.push(n.clone());
    }
    return factors;
}

pub fn is_primitive_root(x: &Number, p: &Number, powers: &Vec<Number>) -> bool {
    if !algo::rabin_miller_test(p, 10) {
        todo!("примитивный корень для составных чисел");
    }
    for l in powers {
        if x.pow_mod(&l, p).unwrap() == 1.into() {
            return false;
        }
    }
    return true;
}

pub fn generate_keypair(p: &Number) -> (OpenKey, SecretKey) {
    let p1 = p.clone() - 1.into();
    let powers = factorization(&p1);
    let mut g;
    loop {
        g = p.random_below();
        if gcd(&g, &p1) == 1.into() {
            continue;
        }
        if g.pow_mod(&(p1.clone() / 2.into()), p).unwrap() != 1.into() {
            continue;
        }
        if is_primitive_root(&g, &p, &powers) {
            break;
        }
    }

    let x = g.random_below();
    let y = g.pow_mod(&x, &p).unwrap();

    println!("Найден примитивный корень: {g}");
    println!("x = {x}");
    println!("y = {y}");

    return (OpenKey { y, g, p: p.clone() }, SecretKey { x });
}

pub const ALPHABET: &str =
    "абвгдеёжзиклмнопрстуфхцчшщъыьэюя1234567890!\"'$%#,.;:{}[]<>*\n\t";

fn get_encoding() -> HashMap<char, String> {
    let mut map = HashMap::new();
    let mut enc = 111;
    for ch in ALPHABET.chars() {
        let mut txt = enc.to_string();
        while txt.contains('0') {
            enc += 1;
            txt = enc.to_string();
        }
        map.insert(ch, txt);
        enc += 1;
    }
    return map;
}

fn get_inv_encoding() -> HashMap<String, char> {
    let encoding = get_encoding();
    let mut inv = HashMap::new();
    for (k, v) in &encoding {
        inv.insert(v.clone(), k.clone());
    }
    return inv;
}

fn gcd(a: &Number, b: &Number) -> Number {
    if *b == 0.into() {
        return a.clone();
    } else {
        return gcd(&b.clone(), &(a.clone() % b.clone()));
    }
}

fn session_key(p: &Number) -> Number {
    let p1 = p.clone() - 1.into();
    loop {
        let k = p1.random_below();
        if gcd(&k, &p1) == 1.into() {
            return k;
        }
    }
}

pub fn filter_text(text: &String) -> String {
    let mut chars = String::new();
    for ch in text.to_lowercase().chars() {
        if ALPHABET.contains(ch) {
            chars.push(ch);
        }
    }
    return chars;
}

pub fn encrypt_text(text: &String, open: OpenKey) -> Vec<(Number, Number)> {
    let encoding = get_encoding();
    println!("Кодировка: {encoding:?}");
    let mut ciphertext = String::new();
    for ch in text.chars() {
        ciphertext.extend(encoding[&ch].chars());
    }

    println!("Перекодированный текст: {ciphertext}");

    let mut result = Vec::new();
    let ciphertext = ciphertext.chars().collect::<Vec<char>>();
    let mut i = 0;
    loop {
        let mut num = String::new();
        while i < ciphertext.len()
            && (num.len() == 0 || Number::parse(&num, 10).unwrap() < open.p)
        {
            num.push(ciphertext[i]);
            i += 1;
        }
        let num2;
        if i == ciphertext.len() {
            let nn = Number::parse(&num, 10).unwrap();
            if nn >= open.p {
                i -= 1;
                num2 = Number::parse(&num[..num.len() - 1], 10).unwrap();
            } else {
                num2 = nn.clone();
            }
        } else {
            i -= 1;
            num2 = Number::parse(&num[..num.len() - 1], 10).unwrap();
        }
        let num = num2;
        println!("Блок: {num}");
        let k = session_key(&open.p);
        let a = open.g.pow_mod(&k, &open.p).unwrap();
        let b = (open.y.pow_mod(&k, &open.p).unwrap() * num.clone())
            % open.p.clone();
        result.push((a, b));
        if i == ciphertext.len() {
            break;
        }
    }
    return result;
}

pub fn decrypt_text(cipher: &String, key: SecretKey, open: OpenKey) -> String {
    let mut encoded = String::new();
    for elem in cipher.split(":") {
        let ab = elem.split(",").collect::<Vec<&str>>();
        let a = Number::parse(ab[0], 10).unwrap();
        let b = Number::parse(ab[1], 10).unwrap();

        println!("Считанные a и b: {a} {b}");
        let pow = open.p.clone() - 1.into() - key.x.clone();
        let m =
            (b.clone() * a.pow_mod(&pow, &open.p).unwrap()) % open.p.clone();
        println!("Расшифровка: {m}");

        encoded.extend(format!("{m}").chars());
    }

    println!("Закодированный текст: {encoded}");
    let inv_enc = get_inv_encoding();
    println!("Обратная кодировка: {inv_enc:?}");
    let encoded = encoded.chars().into_iter().collect::<Vec<char>>();
    let mut result = String::new();
    let mut i = 0;
    loop {
        let mut num = String::new();
        while i < encoded.len() && !inv_enc.contains_key(&num) {
            num.push(encoded[i]);
            i += 1;
        }
        result.push(inv_enc[&num]);
        if i == encoded.len() {
            break;
        }
    }

    return result;
}