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
//! One of the oldest cryptography tools was a scytale, which was used to perform
//! transposition encryption. It consisted of a cylinder with a strip of parchment (containing a
//! message) wound around it.
//!
//! The ancient Greeks used this cipher to communicate during military campaigns. Sender and
//! recipient each had a cylinder of exactly the same radius. The sender wound a narrow ribbon of
//! parchment around their cylinder. Then they wrote on it lengthwise. After the ribbon is unwound,
//! the writing could be read only by a person who had a cylinder of exactly the same
//! circumference.
//!
//! Scytale encryption is only keyed by the number of letters that fit on each roll
//! around the scytale. Therefore, it can be trivially cracked.
//!
use crate::common::cipher::Cipher;

/// A Scytale cipher.
///
/// This struct is created by the `new()` method. See its documentation for more.
pub struct Scytale {
    height: usize,
}

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

    /// Initialize a Scytale cipher with a specific cylinder height.
    ///
    /// # Panics
    /// * The `key` is 0.
    ///
    fn new(key: usize) -> Scytale {
        if key == 0 {
            panic!("Invalid key, height cannot be zero.");
        }

        Scytale { height: key }
    }

    /// Encrypt a message using a Scytale cipher.
    ///
    /// Whilst all characters (including utf8) can be encrypted during the transposition process,
    /// it is important to note that the space character is also treated as padding. As such,
    /// whitespace characters at the end of a message are not preserved during the decryption
    /// process.
    ///
    /// # Examples
    /// Basic usage:
    ///
    /// ```
    /// use cipher_crypt::{Cipher, Scytale};
    ///
    /// let s = Scytale::new(6);
    /// assert_eq!("Pegr lefoporaryr !", s.encrypt("Prepare for glory!").unwrap());
    /// ```
    ///
    fn encrypt(&self, message: &str) -> Result<String, &'static str> {
        // In both these cases the message is not altered
        if self.height >= message.chars().count() || self.height == 1 {
            return Ok(message.to_string());
        }

        // Create the smallest table that fits the message
        let width = (message.chars().count() as f64 / self.height as f64).ceil() as usize;
        let mut table = vec![vec![' '; width]; self.height];

        // Iterate over message and insert into the table, along rows
        for (pos, element) in message.chars().enumerate() {
            let col = pos % self.height;
            let row = pos / self.height;

            table[col][row] = element;
        }

        // Construct the ciphertext out of each row
        // Trim off any trailing whitespace added
        Ok(table
            .iter()
            .flatten()
            .collect::<String>()
            .trim_right()
            .to_string())
    }

    /// Decrypt a message using a Scytale cipher.
    ///
    /// # Examples
    /// Basic usage:
    ///
    /// ```
    /// use cipher_crypt::{Cipher, Scytale};
    ///
    /// let ct = Scytale::new(6);
    /// assert_eq!("Prepare for glory!", ct.decrypt("Pegr lefoporaryr !").unwrap());
    /// ```
    ///
    fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str> {
        // In both these cases the ciphertext has not been altered
        if self.height >= ciphertext.chars().count() || self.height == 1 {
            return Ok(ciphertext.to_string());
        }

        // Create the smallest table that fits the ciphertext
        let width = (ciphertext.chars().count() as f64 / self.height as f64).ceil() as usize;
        let mut table = vec![vec![' '; width]; self.height];

        // Iterate over ciphertext and insert into the table, along columns
        for (pos, element) in ciphertext.chars().enumerate() {
            let col = pos / width;
            let row = pos % width;

            table[col][row] = element;
        }

        // Traverse each column and construct the plaintext
        let mut plaintext = String::new();
        while table.iter().filter(|v| !v.is_empty()).count() > 0 {
            // Continously pop from the top of each column until all are empty
            for column in table.iter_mut() {
                plaintext.push(column.remove(0));
            }
        }

        //Make sure to strip any padding characters
        Ok(plaintext.trim_right().to_string())
    }
}

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

    #[test]
    fn simple_encrypt() {
        let s = Scytale::new(6);
        assert_eq!("aatttdaacwkn", s.encrypt("attackatdawn").unwrap());
    }

    #[test]
    fn simple_decrypt() {
        let s = Scytale::new(6);
        assert_eq!("attackatdawn", s.decrypt("aatttdaacwkn").unwrap());
    }

    #[test]
    fn padding_required() {
        let s = Scytale::new(5);
        let m = "attackatdawn";
        assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
    }

    #[test]
    #[should_panic]
    fn invalid_height() {
        Scytale::new(0);
    }

    #[test]
    fn with_utf8() {
        let s = Scytale::new(5);
        let m = "Attack 🗡️ at once.";
        assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
    }

    #[test]
    fn with_spaces() {
        //Spaces at the end of a message are not preserved
        let s = Scytale::new(5);
        let m = "Attack At Dawn comrades!  ";
        assert_eq!(
            "Attack At Dawn comrades!",
            s.decrypt(&s.encrypt(m).unwrap()).unwrap()
        );
    }

    #[test]
    fn longer_height() {
        let s = Scytale::new(20);
        let m = "attackatdawn";
        assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
    }

    #[test]
    fn longer_msg() {
        let s = Scytale::new(7);
        let m = concat!(
            "We attack at dawn, not later when it is light, ",
            "or at some strange time of the clock. Only at dawn. ",
            "Why do we like to attack at dawn, actually, I don\'t ",
            "get it. I hate getting up that early, it puts me in ",
            "a bad mood. Can\'t we do it a bit later, say nine-thirty?"
        );
        assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
    }
}