-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
88 lines (72 loc) · 1.8 KB
/
main.rs
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
struct PublicKey {
e: u64,
n: u64,
}
struct PrivateKey {
d: u64,
n: u64,
}
// Modular exponentiation: (base^exp) % modulus
fn mod_exp(mut base: u64, mut exp: u64, modulus: u64) -> u64 {
let mut result = 1;
base %= modulus;
while exp > 0 {
if exp % 2 == 1 {
result = (result * base) % modulus;
}
exp >>= 1;
base = (base * base) % modulus;
}
result
}
// Extended Euclidean Algorithm to find modular inverse
fn mod_inv(a: i64, m: i64) -> i64 {
let (mut m0, mut x0, mut x1) = (m, 0, 1);
let mut a = a;
while a > 1 {
let q = a / m0;
let t = m0;
m0 = a % m0;
a = t;
let t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if x1 < 0 {
x1 += m;
}
x1
}
// RSA key generation (with hardcoded small primes for simplicity)
fn generate_keys() -> (PublicKey, PrivateKey) {
let p = 61;
let q = 53;
let n = p * q;
let phi = (p - 1) * (q - 1);
let e = 17;
let d = mod_inv(e as i64, phi as i64) as u64;
(
PublicKey { e, n },
PrivateKey { d, n },
)
}
// RSA encryption: c = m^e mod n
fn encrypt(pub_key: &PublicKey, message: u64) -> u64 {
mod_exp(message, pub_key.e, pub_key.n)
}
// RSA decryption: m = c^d mod n
fn decrypt(priv_key: &PrivateKey, ciphertext: u64) -> u64 {
mod_exp(ciphertext, priv_key.d, priv_key.n)
}
// Main test function
fn main() {
let (public_key, private_key) = generate_keys();
// Test message
let message: u64 = 42;
assert!(message < public_key.n);
let encrypted = encrypt(&public_key, message);
let decrypted = decrypt(&private_key, encrypted);
// Check correctness
assert!(decrypted == message);
assert!(encrypted != message); // Make sure encryption changes the message
}