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
use num_integer::Integer;
/// Affine Cipher
///
/// The struct is generated through the new() function
///
pub struct Affine {
alpha: u16,
beta: u16,
}
impl Affine {
/// Initialize a affine cipher with a key
///
/// # Examples:
/// - Initialization with a valid key:
/// ```
/// use cienli::ciphers::affine::Affine;
/// let affine = Affine::new((5, 2));
/// assert!(affine.is_ok());
/// ```
///
/// - Initialization with a non-coprime key:
/// ```
/// use cienli::ciphers::affine::Affine;
/// let affine = Affine::new((10, 2));
/// assert!(affine.is_err());
/// ```
/// this example will
///
/// - Initialization with a big key:
/// ```
/// use cienli::ciphers::affine::Affine;
/// let affine = Affine::new((27, 2));
/// assert!(affine.is_err());
/// ```
pub fn new(key: (u16, u16)) -> Result<Affine, &'static str> {
let is_key_valid = Affine::key_checker(key);
match is_key_valid {
Ok(_v) => Ok(Affine {
alpha: key.0,
beta: key.1,
}),
Err(v) => Err(v),
}
}
/// Enciphers a message with the affine cipher.
///
/// # Example:
/// ```
/// use cienli::ciphers::affine::Affine;
/// let affine = Affine::new((5, 2)).unwrap();
///
/// assert_eq!("Lwffu :)", affine.encipher("Hello :)"));
/// ```
pub fn encipher(&self, message: &str) -> String {
message
.chars()
.map(|character| match character {
'a'..='z' => {
(((character as u16 - 97) * self.alpha + self.beta) % 26 + 97) as u8 as char
}
'A'..='Z' => {
(((character as u16 - 65) * self.alpha + self.beta) % 26 + 65) as u8 as char
}
_ => character,
})
.collect()
}
/// Deciphers a message with the affine cipher.
///
/// # Example:
/// ```
/// use cienli::ciphers::affine::Affine;
/// let affine = Affine::new((5, 2)).unwrap();
///
/// assert_eq!("Hello :)", affine.decipher("Lwffu :)"));
/// ```
pub fn decipher(&self, message: &str) -> String {
let mut alpha_inv = 0;
while (self.alpha * alpha_inv) % 26 != 1 {
alpha_inv += 1;
}
message
.chars()
.map(|character| match character {
'a'..='z' => {
(alpha_inv * ((character as u16 - 97) - self.beta) % 26 + 97) as u8 as char
}
'A'..='Z' => {
(alpha_inv * ((character as u16 - 65) - self.beta) % 26 + 65) as u8 as char
}
_ => character,
})
.collect()
}
fn key_checker(key: (u16, u16)) -> Result<(), &'static str> {
if (key.0 >= 1 && key.0 <= 26) && key.1 <= 26 {
if key.0.gcd(&26) == 1 {
Ok(())
} else {
Err("The alpha is not co-prime with 26")
}
} else {
Err("The is greater than 26")
}
}
}
#[cfg(test)]
mod tests {
use super::Affine;
#[test]
fn invalid_key_length_test() {
assert!(Affine::new((27, 2)).is_err())
}
#[test]
fn invalid_key_coprime_test() {
assert!(Affine::new((10, 2)).is_err())
}
#[test]
fn encipher_test() {
let affine = Affine::new((5, 2)).unwrap();
assert_eq!("Lwffu :)", affine.encipher("Hello :)"))
}
#[test]
fn decipher_test() {
let affine = Affine::new((5, 2)).unwrap();
assert_eq!("Hello :)", affine.decipher("Lwffu :)"))
}
}