Copyright | (c) Colin Woodbury, 2015 |
---|---|
License | BSD3 |
Maintainer | Colin Woodbury <colingw@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
- class Key k => Cipher k a | a -> k where
- encrypt :: k -> ByteString -> a ByteString
- decrypt :: k -> ByteString -> a ByteString
- class Key a where
- key :: CPRG g => g -> a
- data EnigmaKey = EnigmaKey {
- _rotors :: [Rotor]
- _settings :: [Char]
- _reflector :: Reflector
- _plugboard :: Plugboard
- data Rotor = Rotor {}
- type Reflector = Map (ℤ / 26) (ℤ / 26)
- type Plugboard = Map (ℤ / 26) (ℤ / 26)
- name :: Lens' Rotor Text
- turnover :: Lens' Rotor ((/) ℤ 26)
- circuit :: Lens' Rotor (Map ((/) ℤ 26) ((/) ℤ 26))
- rotors :: Lens' EnigmaKey [Rotor]
- settings :: Lens' EnigmaKey [Char]
- reflector :: Lens' EnigmaKey Reflector
- plugboard :: Lens' EnigmaKey Plugboard
Cipher
class Key k => Cipher k a | a -> k where Source
A Cipher must be able to encrypt and decrypt. The Cipher type determines the Key type.
encrypt :: k -> ByteString -> a ByteString Source
decrypt :: k -> ByteString -> a ByteString Source
Cipher EnigmaKey Enigma Source | When a machine operator presses a key, the Rotors rotate. A circuit is then completed as they hold the key down, and a bulb is lit. Here, we make sure to rotate the Rotors before encrypting the character. NOTE: Decryption is the same as encryption. |
Cipher [(/) ℤ 26] Stream Source | |
Cipher [(/) ℤ 26] Vigenère Source | |
Cipher ((/) ℤ 26, (/) ℤ 26) Affine Source | |
Cipher (Map Char Char) Substitution Source | |
Cipher ((/) ℤ 26) Caesar Source |
Keys
Keys can appear in a number of different forms.
E.g. a single number, a tuple, a mapping, etc.
Each needs to be interpreted uniquely by a Cipher's
encrypt
and decrypt
algorithms.
Key EnigmaKey Source | Note that the randomly generated initial Rotor positions are not applied to the Rotors when the key is generated. They have to be applied before first use. |
Key [(/) ℤ 26] Source | Key for Stream/Vigenère Cipher. |
Key ((/) ℤ 26, (/) ℤ 26) Source | For Affine Ciphers.
|
Key (Map Char Char) Source | Key for Substitution Cipher. The Key is the Mapping itself. |
Key ((/) ℤ 26) Source |
Enigma Types
Essentially the machine itself. It is made up of: 1. Three rotor choices from five, in a random placement. 2. Initial settings of those Rotors. 3. The Reflector model in use. 4. Plugboard settings (pairs of characters).
EnigmaKey | |
|
Eq EnigmaKey Source | |
Show EnigmaKey Source | |
Key EnigmaKey Source | Note that the randomly generated initial Rotor positions are not applied to the Rotors when the key is generated. They have to be applied before first use. |
Cipher EnigmaKey Enigma Source | When a machine operator presses a key, the Rotors rotate. A circuit is then completed as they hold the key down, and a bulb is lit. Here, we make sure to rotate the Rotors before encrypting the character. NOTE: Decryption is the same as encryption. |
A Rotor (German: Walze) is a wheel labelled A to Z, with internal wirings from each entry point to exit point. There is also a turnover point, upon which a Rotor would turn its left neighbour as well. Typically said turnover point is thought of in terms of letters (e.g. Q->R for Rotor I). Here, we represent the turnover point as a distance from A (or 0, the first entry point). As the Rotor rotates, this value decrements. When it rolls back to 25 (modular arithmetic), we rotate the next Rotor.
Our Rotors are letter-agnostic. That is, they only map numeric entry points to exit points. This allows us to simulate rotation very simply with Lenses.
type Reflector = Map (ℤ / 26) (ℤ / 26) Source
A unmoving map, similar to the Rotors, which feeds the electrical current back into Rotors. This would never feed the left Rotor's letter back to itself, meaning a plaintext character would never encrypt to itself. This was a major weakness in scheme which allowed the Allies to make Known Plaintext Attacks against the machine.