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
//! # Ciphers
//! 
//! Ciphers is a Rust library that provides implementations of many different
//! classical ciphers.
//! 
//! ## 1. Supported Ciphers
//! There are currently **16 different supported ciphers**.
//! 
//! | Transposition          | Monoalphabetic      | Polyalphabetic | Polygraphic | Other  |
//! | ---------------------- | ------------------- | -------------- | ----------- | ------ |
//! | Rail-fence             | Simple Substitution | Vigenere       | Playfair    | ADFGX  |
//! | Columnar Transposition | Caesar              | Beaufort       | Four-Square | ADFGVX |
//! |                        | Affine              | Autokey        |             |        |
//! |                        | Polybius Square     | Running Key    |             |        |
//! |                        | Atbash              | Porta          |             |        |
//! 
//! ## 2. Installation
//! Simply put the following in your **Cargo.toml**.
//! 
//! ```toml
//! [dependencies]
//! ciphers = "0.1.0"
//! ```
//! 
//! ## 3. Example Usage
//! E.g. using the **Vigenere** cipher.
//! 
//! ```
//! use ciphers::{Cipher, Vigenere};
//! 
//! fn main() {
//!     let vigenere = Vigenere::new("examplekey");
//! 
//!     // encipher
//!     let ctext = vigenere.encipher("someexampletexthere").unwrap();
//!     println!("ciphertext: {}", ctext);
//! 
//!     // decipher
//!     let ptext = vigenere.decipher(&ctext).unwrap();
//!     println!("plaintext:  {}", ptext);
//! }
//! ```
//! 
//! ## 4. To be Implemented
//! There are currently **6 different ciphers to be implemented**.
//! 
//! | Transposition | Monoalphabetic | Polyalphabetic | Polygraphic | Other                 |
//! | ------------- | -------------- | -------------- | ----------- | --------------------- |
//! |               | Rot13          | Gronsfeld      | Hill        | Bifid                 |
//! |               |                |                |             | Trifid                |
//! |               |                |                |             | Straddle Checkerboard |

mod adfgvx;
mod adfgx;
mod affine;
mod atbash;
mod autokey;
mod beaufort;
mod caesar;
mod columnar_transposition;
mod four_square;
mod playfair;
mod polybius_square;
mod porta;
mod rail_fence;
mod running_key;
mod substitution;
mod vigenere;

mod input;

// re-exports
pub use adfgvx::ADFGVX;
pub use adfgx::ADFGX;
pub use affine::Affine;
pub use atbash::Atbash;
pub use autokey::Autokey;
pub use beaufort::Beaufort;
pub use caesar::Caesar;
pub use columnar_transposition::ColumnarTransposition;
pub use four_square::FourSquare;
pub use playfair::Playfair;
pub use polybius_square::PolybiusSquare;
pub use porta::Porta;
pub use rail_fence::RailFence;
pub use running_key::RunningKey;
pub use substitution::Substitution;
pub use vigenere::Vigenere;

/// Errors that can result when giving a cipher method bad input.
#[derive(Debug, PartialEq)]
pub enum CipherInputError {
    NotAlphabetic,
    NotAscii,
    NotInAlphabet,
    BadInput(String),
}

/// A Result alias where the `Err` case is `ciphers::CipherInputError`.
pub type CipherResult = Result<String, CipherInputError>;

/// Defines the implementation for cipher functionality.
pub trait Cipher {
    /// Should take plaintext as a string reference, and return the enciphered ciphertext as
    /// a `CipherResult`.
    fn encipher(&self, ptext: &str) -> CipherResult;

    /// Should take the ciphertext as a string reference, and return the deciphered plaintext
    /// as a `CipherResult`.
    fn decipher(&self, ctext: &str) -> CipherResult;
}

static TABULA_RECTA: [[u8; 26]; 26] = [
    [
        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,
    ],
    [
        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, 65,
    ],
    [
        67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
        90, 65, 66,
    ],
    [
        68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
        65, 66, 67,
    ],
    [
        69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65,
        66, 67, 68,
    ],
    [
        70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66,
        67, 68, 69,
    ],
    [
        71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67,
        68, 69, 70,
    ],
    [
        72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68,
        69, 70, 71,
    ],
    [
        73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69,
        70, 71, 72,
    ],
    [
        74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70,
        71, 72, 73,
    ],
    [
        75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71,
        72, 73, 74,
    ],
    [
        76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72,
        73, 74, 75,
    ],
    [
        77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73,
        74, 75, 76,
    ],
    [
        78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
        75, 76, 77,
    ],
    [
        79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
        76, 77, 78,
    ],
    [
        80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
        77, 78, 79,
    ],
    [
        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
        78, 79, 80,
    ],
    [
        82, 83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
        79, 80, 81,
    ],
    [
        83, 84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
        80, 81, 82,
    ],
    [
        84, 85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
        81, 82, 83,
    ],
    [
        85, 86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
        82, 83, 84,
    ],
    [
        86, 87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
        83, 84, 85,
    ],
    [
        87, 88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
        84, 85, 86,
    ],
    [
        88, 89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
        85, 86, 87,
    ],
    [
        89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
        86, 87, 88,
    ],
    [
        90, 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,
    ],
];