-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathrsa_cipher.rs
More file actions
405 lines (356 loc) · 10.4 KB
/
rsa_cipher.rs
File metadata and controls
405 lines (356 loc) · 10.4 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! RSA Cipher Implementation
//!
//! This module provides a basic implementation of the RSA (Rivest-Shamir-Adleman) encryption algorithm.
//! RSA is an asymmetric cryptographic algorithm that uses a pair of keys: public and private.
//!
//! # Warning
//!
//! This is an educational implementation and should NOT be used for production cryptography.
//! Use established cryptographic libraries like `ring` or `rust-crypto` for real-world applications.
//!
//! # Examples
//!
//! ```
//! use the_algorithms_rust::ciphers::{generate_keypair, encrypt, decrypt};
//!
//! let (public_key, private_key) = generate_keypair(61, 53);
//! let message = 65;
//! let encrypted = encrypt(message, &public_key);
//! let decrypted = decrypt(encrypted, &private_key);
//! assert_eq!(message, decrypted);
//! ```
/// Represents an RSA public key containing (n, e)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PublicKey {
pub n: u64,
pub e: u64,
}
/// Represents an RSA private key containing (n, d)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PrivateKey {
pub n: u64,
pub d: u64,
}
/// Computes the greatest common divisor using Euclid's algorithm
///
/// # Arguments
///
/// * `a` - First number
/// * `b` - Second number
///
/// # Returns
///
/// The GCD of `a` and `b`
fn gcd(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
/// Computes the modular multiplicative inverse using the Extended Euclidean Algorithm
///
/// Finds `x` such that `(a * x) % m == 1`
///
/// # Arguments
///
/// * `a` - The number to find the inverse of
/// * `m` - The modulus
///
/// # Returns
///
/// The modular multiplicative inverse of `a` modulo `m`, or `None` if it doesn't exist
fn mod_inverse(a: i64, m: i64) -> Option<u64> {
let (mut old_r, mut r) = (a, m);
let (mut old_s, mut s) = (1_i64, 0_i64);
while r != 0 {
let quotient = old_r / r;
(old_r, r) = (r, old_r - quotient * r);
(old_s, s) = (s, old_s - quotient * s);
}
if old_r > 1 {
return None; // a is not invertible
}
if old_s < 0 {
Some((old_s + m) as u64)
} else {
Some(old_s as u64)
}
}
/// Performs modular exponentiation: (base^exp) % modulus
///
/// Uses the square-and-multiply algorithm for efficiency
///
/// # Arguments
///
/// * `base` - The base number
/// * `exp` - The exponent
/// * `modulus` - The modulus
///
/// # Returns
///
/// The result of (base^exp) % modulus
fn mod_pow(mut base: u64, mut exp: u64, modulus: u64) -> u64 {
if modulus == 1 {
return 0;
}
let mut result = 1;
base %= modulus;
while exp > 0 {
if exp % 2 == 1 {
result = ((result as u128 * base as u128) % modulus as u128) as u64;
}
exp >>= 1;
base = ((base as u128 * base as u128) % modulus as u128) as u64;
}
result
}
/// Generates an RSA keypair from two prime numbers
///
/// # Arguments
///
/// * `p` - First prime number
/// * `q` - Second prime number (should be different from p)
///
/// # Returns
///
/// A tuple containing (PublicKey, PrivateKey)
///
/// # Examples
///
/// ```
/// use the_algorithms_rust::ciphers::generate_keypair;
///
/// let (public, private) = generate_keypair(61, 53);
/// // n = p * q
/// assert_eq!(public.n, 3233);
/// assert_eq!(private.n, 3233);
/// // Both keys share the same n
/// assert_eq!(public.n, private.n);
/// ```
///
/// # Panics
///
/// Panics if the modular inverse cannot be computed
pub fn generate_keypair(p: u64, q: u64) -> (PublicKey, PrivateKey) {
let n = p * q;
let phi = (p - 1) * (q - 1);
// Choose e such that 1 < e < phi and gcd(e, phi) = 1
let mut e = 2;
while e < phi {
if gcd(e, phi) == 1 {
break;
}
e += 1;
}
// Compute d, the modular multiplicative inverse of e mod phi
let d = mod_inverse(e as i64, phi as i64).expect("Failed to compute modular inverse");
let public_key = PublicKey { n, e };
let private_key = PrivateKey { n, d };
(public_key, private_key)
}
/// Encrypts a message using the RSA public key
///
/// # Arguments
///
/// * `message` - The plaintext message (must be less than n)
/// * `public_key` - The public key to use for encryption
///
/// # Returns
///
/// The encrypted ciphertext
///
/// # Examples
///
/// ```
/// use the_algorithms_rust::ciphers::{generate_keypair, encrypt, decrypt};
///
/// let (public_key, private_key) = generate_keypair(61, 53);
/// let message = 65;
/// let ciphertext = encrypt(message, &public_key);
/// let decrypted = decrypt(ciphertext, &private_key);
/// assert_eq!(decrypted, message);
/// ```
pub fn encrypt(message: u64, public_key: &PublicKey) -> u64 {
mod_pow(message, public_key.e, public_key.n)
}
/// Decrypts a ciphertext using the RSA private key
///
/// # Arguments
///
/// * `ciphertext` - The encrypted message
/// * `private_key` - The private key to use for decryption
///
/// # Returns
///
/// The decrypted plaintext message
///
/// # Examples
///
/// ```
/// use the_algorithms_rust::ciphers::{generate_keypair, encrypt, decrypt};
///
/// let (public_key, private_key) = generate_keypair(61, 53);
/// let message = 65;
/// let ciphertext = encrypt(message, &public_key);
/// let plaintext = decrypt(ciphertext, &private_key);
/// assert_eq!(plaintext, message);
/// ```
pub fn decrypt(ciphertext: u64, private_key: &PrivateKey) -> u64 {
mod_pow(ciphertext, private_key.d, private_key.n)
}
/// Encrypts a text message by converting each character to its ASCII value
///
/// # Arguments
///
/// * `message` - The plaintext string
/// * `public_key` - The public key to use for encryption
///
/// # Returns
///
/// A vector of encrypted values, one for each character
///
/// # Examples
///
/// ```
/// use the_algorithms_rust::ciphers::{generate_keypair, encrypt_text, decrypt_text};
///
/// let (public, private) = generate_keypair(61, 53);
/// let encrypted = encrypt_text("HI", &public);
/// let decrypted = decrypt_text(&encrypted, &private);
/// assert_eq!(decrypted, "HI");
/// ```
pub fn encrypt_text(message: &str, public_key: &PublicKey) -> Vec<u64> {
message
.chars()
.map(|c| encrypt(c as u64, public_key))
.collect()
}
/// Decrypts a vector of encrypted values back to text
///
/// # Arguments
///
/// * `ciphertext` - The vector of encrypted character values
/// * `private_key` - The private key to use for decryption
///
/// # Returns
///
/// The decrypted string
///
/// # Examples
///
/// ```
/// use the_algorithms_rust::ciphers::{generate_keypair, encrypt_text, decrypt_text};
///
/// let (public, private) = generate_keypair(61, 53);
/// let encrypted = encrypt_text("HELLO", &public);
/// let decrypted = decrypt_text(&encrypted, &private);
/// assert_eq!(decrypted, "HELLO");
/// ```
pub fn decrypt_text(ciphertext: &[u64], private_key: &PrivateKey) -> String {
ciphertext
.iter()
.map(|&c| {
let decrypted = decrypt(c, private_key);
char::from_u32(decrypted as u32).unwrap_or('?')
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gcd() {
assert_eq!(gcd(48, 18), 6);
assert_eq!(gcd(17, 13), 1);
assert_eq!(gcd(100, 50), 50);
assert_eq!(gcd(7, 7), 7);
}
#[test]
fn test_mod_inverse() {
assert_eq!(mod_inverse(17, 3120), Some(2753));
assert_eq!(mod_inverse(7, 40), Some(23));
assert!(mod_inverse(4, 8).is_none()); // No inverse exists
}
#[test]
fn test_mod_pow() {
assert_eq!(mod_pow(4, 13, 497), 445);
assert_eq!(mod_pow(2, 10, 1000), 24);
assert_eq!(mod_pow(3, 5, 7), 5);
}
#[test]
fn test_generate_keypair() {
let (public, private) = generate_keypair(61, 53);
// n should be p * q
assert_eq!(public.n, 3233);
assert_eq!(private.n, 3233);
// e should be coprime with phi
let phi = (61 - 1) * (53 - 1);
assert_eq!(gcd(public.e, phi), 1);
// Verify that (e * d) % phi == 1
assert_eq!((public.e * private.d) % phi, 1);
}
#[test]
fn test_encrypt_decrypt_number() {
let (public, private) = generate_keypair(61, 53);
let message = 65;
let encrypted = encrypt(message, &public);
// Encrypted value will vary based on e, so we just check it's different
assert_ne!(encrypted, message);
let decrypted = decrypt(encrypted, &private);
assert_eq!(decrypted, message);
}
#[test]
fn test_encrypt_decrypt_various_numbers() {
let (public, private) = generate_keypair(61, 53);
for message in [1, 42, 100, 255, 1000, 3000] {
let encrypted = encrypt(message, &public);
let decrypted = decrypt(encrypted, &private);
assert_eq!(decrypted, message, "Failed for message: {message}");
}
}
#[test]
fn test_encrypt_decrypt_text() {
let (public, private) = generate_keypair(61, 53);
let message = "HI";
let encrypted = encrypt_text(message, &public);
let decrypted = decrypt_text(&encrypted, &private);
assert_eq!(decrypted, message);
}
#[test]
fn test_encrypt_decrypt_longer_text() {
let (public, private) = generate_keypair(61, 53);
let message = "HELLO";
let encrypted = encrypt_text(message, &public);
let decrypted = decrypt_text(&encrypted, &private);
assert_eq!(decrypted, message);
}
#[test]
fn test_different_primes() {
let (public, private) = generate_keypair(17, 19);
let message = 42;
let encrypted = encrypt(message, &public);
let decrypted = decrypt(encrypted, &private);
assert_eq!(decrypted, message);
}
#[test]
fn test_encrypt_decrypt_alphabet() {
let (public, private) = generate_keypair(61, 53);
let message = "ABC";
let encrypted = encrypt_text(message, &public);
let decrypted = decrypt_text(&encrypted, &private);
assert_eq!(decrypted, message);
}
#[test]
fn test_key_properties() {
let (public, private) = generate_keypair(61, 53);
// Both keys should have the same n
assert_eq!(public.n, private.n);
// e and d should be different
assert_ne!(public.e, private.d);
// Verify that (e * d) % phi == 1
let phi = (61 - 1) * (53 - 1);
assert_eq!((public.e * private.d) % phi, 1);
}
}