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
//! The Railfence Cipher is a transposition cipher. It has a very low keyspace and is therefore
//! incredibly insecure.
//!
//! This implementation currently transposes all input characters including whitespace and
//! punctuation.

/// A Railfence cipher.
///
/// This struct is created by the `new()` method. See its documentation for more.
use common::cipher::Cipher;

pub struct Railfence {
    rails: usize,
}

impl Cipher for Railfence {
    type Key = usize;
    type Algorithm = Railfence;

    /// Initialise a Railfence cipher given a specific key (number of rails).
    ///
    /// Will return `Err` if the `key == 0`.
    fn new(key: usize) -> Result<Railfence, &'static str> {
        if key == 0 {
            return Err("Invalid key. Railfence key cannot be zero.");
        }

        Ok(Railfence { rails: key })
    }

    /// Encrypt a message using a Railfence cipher.
    ///
    /// # Examples
    /// Basic usage:
    ///
    /// ```
    /// use cipher_crypt::{Cipher, Railfence};
    ///
    /// let r = Railfence::new(3).unwrap();
    /// assert_eq!("Src s!ue-ertmsaepseeg", r.encrypt("Super-secret message!").unwrap());
    /// ```
    fn encrypt(&self, message: &str) -> Result<String, &'static str> {
        // Encryption process:
        //   First a table is created with a height given by the key and a length
        //   given by the message length.
        //   e.g.
        //   For a key of 3 and the message "Hello, World!" of length 13:
        //      .............
        //      .............
        //      .............
        //   The message can then be written onto the grid in a zigzag going right:
        //      H...o...o...!
        //      .e.l.,.W.r.d.
        //      ..l... ...l..
        //   The encrypted message is then read line by line:
        //      Hoo!el,Wrdl l

        // We simply return the message as the 'encrypted' message when there is one rail.
        // This is because the message is transposed along a single rail without being altered.
        if self.rails == 1 {
            return Ok(message.to_string())
        }

        // Initialise the fence (a simple table)
        // The form of an entry is (bool, char) => (is_msg_element, msg_element)
        let mut table = vec![vec![(false, '.'); message.len()]; self.rails];

        //Transpose the message along the fence
        for (col, element) in message.chars().enumerate() {
            //Given the column (ith element of the message), determine which row to place the
            //character on
            let rail = Railfence::calc_current_rail(col, self.rails);
            table[rail][col] = (true, element);
        }

        // Read the ciphertext row by row
        let mut ciphertext = String::new();
        for row in table {
            for (is_msg_element, element) in row {
                if is_msg_element {
                    ciphertext.push(element);
                }
            }
        }

        Ok(ciphertext)
    }

    /// Decrypt a message using a Railfence cipher.
    ///
    /// # Examples
    /// Basic usage:
    ///
    /// ```
    /// use cipher_crypt::{Cipher, Railfence};
    ///
    /// let r = Railfence::new(3).unwrap();
    /// assert_eq!("Super-secret message!", r.decrypt("Src s!ue-ertmsaepseeg").unwrap());
    /// ```
    fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str> {
        // Decryption process:
        //   First a table is created with a height given by the key and a length
        //   given by the ciphertext length.
        //   e.g.
        //   For a key of 3 and the ciphertext "Hoo!el,Wrdl l" of length 13:
        //      .............
        //      .............
        //      .............
        //   The positions in the table that would be used to encrypt a message are identified
        //      x...x...x...x
        //      .x.x.x.x.x.x.
        //      ..x...x...x..
        //   The ciphertext is then written onto the identified positions, line by line
        //      H...o...o...!
        //      .e.l.,.W.r.d.
        //      ..l... ...l..
        //   The decrypted message is then read in a zigzag:
        //      Hello, World!

        // As mentioned previously, a single rail means that the original message has not been
        // altered
        if self.rails == 1 {
            return Ok(ciphertext.to_string())
        }

        let mut table = vec![vec![(false, '.'); ciphertext.len()]; self.rails];

        // Traverse the table and mark the elements that will be filled by the cipher text
        for col in 0..ciphertext.len() {
            let rail = Railfence::calc_current_rail(col, self.rails);
            table[rail][col].0 = true;
        }

        // Fill the identified positions in the table with the ciphertext, line by line
        let mut ct_chars = ciphertext.chars();
        'outer: for row in table.iter_mut() {
            // For each element in the row, determine if a char should be placed there
            for element in row.iter_mut() {
                if element.0 {
                    if let Some(c) = ct_chars.next() {
                        *element = (element.0, c);
                    } else {
                        // We have transposed all chars of the cipher text
                        break 'outer;
                    }
                }
            }
        }

        // From the transposed cipher text construct the original message
        let mut message = String::new();
        for col in 0..ciphertext.len() {
            // For this column, determine which row we should read from to get the next char
            // of the message
            let rail = Railfence::calc_current_rail(col, self.rails);
            message.push(table[rail][col].1);
        }

        Ok(message)
    }
}

impl Railfence {
    /// For a given column and the total number of 'rails' (rows), determine the current rail
    /// that should be referenced.
    ///
    fn calc_current_rail(col: usize, total_rails: usize) -> usize {
        // In the Railfence cipher the letters are placed diagonally in a zigzag,
        // so, with a key of 4 say, the row numbers will go
        //      0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0, ...
        // This repeats with a cycle (or period) given by (2*key - 2)
        //      [0, 1, 2, 3, 2, 1], [0, 1, 2, 3, 2, 1], 0, ...
        // This cycle is always even.
        let cycle = 2 * total_rails - 2;

        // For the first half of a cycle, the row is given by the index,
        // but for the second half it decreases and is therefore given by the reverse index,
        // the distance from the end of the cycle.
        let rail = if col % cycle <= cycle / 2 {
            col % cycle
        } else {
            cycle - col % cycle
        };

        rail
    }
}

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

    #[test]
    fn encrypt_test() {
        let message = "attackatdawn";
        let r = Railfence::new(6).unwrap();
        assert_eq!("awtantdatcak", r.encrypt(message).unwrap());
    }

    #[test]
    fn encrypt_mixed_case() {
        let message = "Hello, World!";
        let r = Railfence::new(3).unwrap();
        assert_eq!("Hoo!el,Wrdl l", r.encrypt(message).unwrap());
    }

    #[test]
    fn encrypt_short_key() {
        let message = "attackatdawn";
        let r = Railfence::new(1).unwrap();
        assert_eq!("attackatdawn", r.encrypt(message).unwrap());
    }

    #[test]
    fn encrypt_long_key() {
        let message = "attackatdawn";
        let r = Railfence::new(20).unwrap();
        assert_eq!("attackatdawn", r.encrypt(message).unwrap());
    }

    #[test]
    fn decrypt_test() {
        let message = "awtantdatcak";
        let r = Railfence::new(6).unwrap();
        assert_eq!("attackatdawn", r.decrypt(message).unwrap());
    }

    #[test]
    fn decrypt_short_key() {
        let message = "attackatdawn";
        let r = Railfence::new(1).unwrap();
        assert_eq!("attackatdawn", r.decrypt(message).unwrap());
    }

    #[test]
    fn decrypt_mixed_case() {
        let message = "Hoo!el,Wrdl l";
        let r = Railfence::new(3).unwrap();
        assert_eq!("Hello, World!", r.decrypt(message).unwrap());
    }

    #[test]
    fn decrypt_long_key() {
        let message = "attackatdawn";
        let r = Railfence::new(20).unwrap();
        assert_eq!("attackatdawn", r.decrypt(message).unwrap());
    }

    #[test]
    fn incorrect_key_test() {
        assert!(Railfence::new(0).is_err());
    }

    #[test]
    fn unicode_test() {
        let r = Railfence::new(3).unwrap();
        let message = "ÂƮƮäƈķ ɑƬ Ðawŋ ✓";
        assert_eq!("ÂƈƬwƮäķɑ aŋ✓Ʈ Ð ", r.encrypt(message).unwrap());
    }
}