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
//! The Vigenère Cipher is a polyalphabetic substitution cipher. It was considered 'le chiffre
//! indéchiffrable' for 300 years until Friedrich Kasiski broke it in 1863.
//!
//! Note that this implementation does not mutate the calculated encoding/decoding key if the
//! message contains non-alphabetic symbols (including whitespace).
//!
//! For example, say the message was `ATTACK AT DAWN` and the key was `CRYPT` then the calculated
//! encoding key would be `CRYPTCRYPTCRYP` not `CRYPTC RY PTCR`.
use std::iter;
use common::alphabet;
use common::cipher::Cipher;

/// A Vigenère cipher.
///
/// This struct is created by the `new()` method. See its documentation for more.
pub struct Vigenere {
    key: String,
}

impl Cipher for Vigenere {
    type Key = String;
    type Algorithm = Vigenere;

    /// Initialise a Vigenère cipher given a specific key.
    ///
    /// Will return `Err` if the key contains non-alphabetic symbols.
    fn new(key: String) -> Result<Vigenere, &'static str> {
        for c in key.chars() {
            //Keys can only contain characters in the known alphabet
            if alphabet::find_position(c).is_none(){
                return Err("Invalid key. Vigenere keys cannot contain non-alphabetic symbols.");
            }
        }

        Ok(Vigenere { key: key })
    }

    /// Encrypt a message using a Vigenère cipher.
    ///
    /// # Examples
    /// Basic usage:
    ///
    /// ```
    /// use cipher_crypt::{Cipher, Vigenere};
    ///
    /// let v = Vigenere::new(String::from("giovan")).unwrap();
    /// assert_eq!("O bzvrx uzt gvm ceklwo!", v.encrypt("I never get any credit!").unwrap());
    /// ```
    fn encrypt(&self, message: &str) -> Result<String, &'static str> {
        // Encryption of a letter in a message:
        //         Ci = Ek(Mi) = (Mi + Ki) mod 26
        // Where;  Mi = position within the alphabet of ith char in message
        //         Ki = position within the alphabet of ith char in key
        let e_key = self.fit_key(message.len());

        Vigenere::poly_substitute(message, e_key, |mi, ki| (mi + ki) % 26)
    }

    /// Decrypt a message using a Vigenère cipher.
    ///
    /// # Examples
    /// Basic usage:
    ///
    /// ```
    /// use cipher_crypt::{Cipher, Vigenere};
    ///
    /// let v = Vigenere::new(String::from("giovan")).unwrap();
    /// assert_eq!("I never get any credit!", v.decrypt("O bzvrx uzt gvm ceklwo!").unwrap());
    /// ```
    fn decrypt(&self, cipher_text: &str) -> Result<String, &'static str> {
        // Decryption of a letter in a message:
        //         Mi = Dk(Ci) = (Ci - Ki) mod 26
        // Where;  Ci = position within the alphabet of ith char in cipher text
        //         Ki = position within the alphabet of ith char in key
        let d_key = self.fit_key(cipher_text.len());

        let decrypt = |ci, ki| {
            let a: isize = ci as isize - ki as isize;
            (((a % 26) + 26) % 26) as usize
            //Rust does not natievly support negative wrap around modulo operations
        };

        Vigenere::poly_substitute(cipher_text, d_key, decrypt)
    }
}

impl Vigenere {
    /// Fits the key to a given `msg_length`.
    ///
    /// Will simply return a copy of the key if its length is already larger than the message.
    fn fit_key(&self, msg_length: usize) -> String {
        let key_copy = self.key.clone();

        if key_copy.len() >= msg_length {
            return key_copy; //The key is large enough for the message already
        }

        //Repeat the key until it fits within the length of the message
        let mut repeated_key = iter::repeat(key_copy).take((msg_length / self.key.len()) + 1)
            .collect::<String>();

        repeated_key.truncate(msg_length);
        repeated_key
    }

    /// Performs a poly substitution on a piece of text based on the index of its characters
    /// within the alphabet.
    ///
    /// This substitution is defined by the closure `calc_index`
    fn poly_substitute<F>(text: &str, key: String, calc_index: F) -> Result<String, &'static str>
        where F: Fn(usize, usize) -> usize
    {
        let mut s_text = String::new();

        for (i, tc) in text.chars().enumerate() {
            //Find the index of the character in the alphabet (if it exists in there)
            let tpos = alphabet::find_position(tc);
            match tpos {
                Some(ti) => {
                    //Get the key character at position i
                    if let Some(kc) = key.chars().nth(i) {
                        //Get position of character within the alphabet
                        if let Some(ki) = alphabet::find_position(kc) {
                            //Calculate the index and retrieve the letter to substitute
                            let si = calc_index(ti, ki);
                            if let Some(s) = alphabet::get_letter(si, tc.is_uppercase()){
                                s_text.push(s);
                            } else {
                                return Err("Calculated a substitution index outside of the known alphabet.")
                            }
                        } else {
                            return Err("Vigenere key contains a non-alphabetic symbol.")
                        }
                    } else {
                        return Err("Fitted key is too small for message length.")
                    }

                },
                None => s_text.push(tc), //Push non-alphabetic chars 'as-is'
            }
        }

        Ok(s_text)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn encrypt_test() {
        let message = "attackatdawn";
        let v = Vigenere::new(String::from("lemon")).unwrap();
        assert_eq!("lxfopvefrnhr", v.encrypt(message).unwrap());
    }

    #[test]
    fn decrypt_test() {
        let cipher_text = "lxfopvefrnhr";
        let v = Vigenere::new(String::from("lemon")).unwrap();
        assert_eq!("attackatdawn", v.decrypt(cipher_text).unwrap());
    }

    #[test]
    fn mixed_case() {
        let message = "Attack at Dawn!";
        let v = Vigenere::new(String::from("giovan")).unwrap();

        let cipher_text = v.encrypt(message).unwrap();
        let plain_text = v.decrypt(&cipher_text).unwrap();

        assert_eq!(plain_text, message);
    }

    #[test]
    fn with_emoji(){
        let v = Vigenere::new(String::from("emojisarefun")).unwrap();
        let message = "Peace, Freedom and Liberty! 🗡️";
        let encrypted = v.encrypt(message).unwrap();
        let decrypted = v.decrypt(&encrypted).unwrap();

        assert_eq!(decrypted, message);
    }

    #[test]
    fn fit_smaller_key() {
        let message = "We are under seige!"; //19 character message
        let v = Vigenere::new(String::from("lemon")).unwrap(); //key length of 5

        assert_eq!("lemonlemonlemonlemo", v.fit_key(message.len()));
        assert!(v.fit_key(message.len()).len() >= message.len());
    }

    #[test]
    fn fit_larger_key() {
        let message = "hi";
        let v = Vigenere::new(String::from("lemon")).unwrap();

        assert_eq!("lemon", v.fit_key(message.len()));
        assert!(v.fit_key(message.len()).len() >= message.len());
    }

    #[test]
    fn valid_key() {
        assert!(Vigenere::new(String::from("LeMon")).is_ok());
    }

    #[test]
    fn key_with_symbols() {
        assert!(Vigenere::new(String::from("!em@n")).is_err());
    }

    #[test]
    fn key_with_whitespace() {
        assert!(Vigenere::new(String::from("wow this key is a real lemon")).is_err());
    }
}