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
use cratemultiplicative_inverse;
/// Decrypts a string encoded with the affine cipher.
///
/// Given a ciphertext `y`, reverses the affine encryption using the formula:
///
/// ```text
/// D(y) = a⁻¹ · (y - b) mod 256
/// ```
///
/// where `a⁻¹` is the multiplicative inverse of `a` mod 256.
///
/// # Arguments
///
/// * `y` - Ciphertext to decrypt
/// * `a` - Multiplicative key. Must be odd otherwise `multiplicative_inverse` returns `None` and the function panics.
/// * `b` - Additive key
///
/// # Returns
///
/// * `Ok(String)` - Decrypted plaintext
/// * `Err(FromUtf8Error)` - If the resulting bytes are not valid UTF-8
///
/// # Panics
///
/// Panics if `a` has no multiplicative inverse mod 256 (i.e. `a` is even).
///
/// # Examples
///
/// ```
/// use cryptograph::cryptography::affine::decrypt::affine_decrypt;
/// use cryptograph::cryptography::affine::encrypt::affine_encrypt;
/// let encrypted = affine_encrypt("hola", 3, 7);
/// let decrypted = affine_decrypt(&encrypted.unwrap(), 3, 7).unwrap();
/// assert_eq!(decrypted, "hola");
/// ```