Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{rotor}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 2 | \bimodindex{rotor} |
| 3 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 4 | This module implements a rotor-based encryption algorithm, contributed by |
| 5 | Lance Ellinghouse. The design is derived from the Enigma device, a machine |
| 6 | used during World War II to encipher messages. A rotor is simply a |
| 7 | permutation. For example, if the character `A' is the origin of the rotor, |
| 8 | then a given rotor might map `A' to `L', `B' to `Z', `C' to `G', and so on. |
| 9 | To encrypt, we choose several different rotors, and set the origins of the |
| 10 | rotors to known positions; their initial position is the ciphering key. To |
| 11 | encipher a character, we permute the original character by the first rotor, |
| 12 | and then apply the second rotor's permutation to the result. We continue |
| 13 | until we've applied all the rotors; the resulting character is our |
| 14 | ciphertext. We then change the origin of the final rotor by one position, |
| 15 | from `A' to `B'; if the final rotor has made a complete revolution, then we |
| 16 | rotate the next-to-last rotor by one position, and apply the same procedure |
| 17 | recursively. In other words, after enciphering one character, we advance |
| 18 | the rotors in the same fashion as a car's odometer. Decoding works in the |
| 19 | same way, except we reverse the permutations and apply them in the opposite |
| 20 | order. |
| 21 | \index{Ellinghouse, Lance} |
| 22 | \indexii{Enigma}{cipher} |
| 23 | |
| 24 | The available functions in this module are: |
| 25 | |
| 26 | \renewcommand{\indexsubitem}{(in module rotor)} |
| 27 | \begin{funcdesc}{newrotor}{key\optional{\, numrotors}} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 28 | Return a rotor object. \var{key} is a string containing the encryption key |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 29 | for the object; it can contain arbitrary binary data. The key will be used |
| 30 | to randomly generate the rotor permutations and their initial positions. |
| 31 | \var{numrotors} is the number of rotor permutations in the returned object; |
| 32 | if it is omitted, a default value of 6 will be used. |
| 33 | \end{funcdesc} |
| 34 | |
| 35 | Rotor objects have the following methods: |
| 36 | |
| 37 | \renewcommand{\indexsubitem}{(rotor method)} |
Barry Warsaw | 6717030 | 1997-01-02 19:48:00 +0000 | [diff] [blame^] | 38 | \begin{funcdesc}{setkey}{key} |
| 39 | Sets the rotor's key to \var{key}. |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{encrypt}{plaintext} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 43 | Reset the rotor object to its initial state and encrypt \var{plaintext}, |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 44 | returning a string containing the ciphertext. The ciphertext is always the |
| 45 | same length as the original plaintext. |
| 46 | \end{funcdesc} |
| 47 | |
| 48 | \begin{funcdesc}{encryptmore}{plaintext} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 49 | Encrypt \var{plaintext} without resetting the rotor object, and return a |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 50 | string containing the ciphertext. |
| 51 | \end{funcdesc} |
| 52 | |
| 53 | \begin{funcdesc}{decrypt}{ciphertext} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 54 | Reset the rotor object to its initial state and decrypt \var{ciphertext}, |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 55 | returning a string containing the ciphertext. The plaintext string will |
| 56 | always be the same length as the ciphertext. |
| 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{decryptmore}{ciphertext} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 60 | Decrypt \var{ciphertext} without resetting the rotor object, and return a |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 61 | string containing the ciphertext. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | An example usage: |
| 65 | \bcode\begin{verbatim} |
| 66 | >>> import rotor |
| 67 | >>> rt = rotor.newrotor('key', 12) |
| 68 | >>> rt.encrypt('bar') |
| 69 | '\2534\363' |
| 70 | >>> rt.encryptmore('bar') |
| 71 | '\357\375$' |
| 72 | >>> rt.encrypt('bar') |
| 73 | '\2534\363' |
| 74 | >>> rt.decrypt('\2534\363') |
| 75 | 'bar' |
| 76 | >>> rt.decryptmore('\357\375$') |
| 77 | 'bar' |
| 78 | >>> rt.decrypt('\357\375$') |
| 79 | 'l(\315' |
| 80 | >>> del rt |
| 81 | \end{verbatim}\ecode |
| 82 | |
| 83 | The module's code is not an exact simulation of the original Enigma device; |
| 84 | it implements the rotor encryption scheme differently from the original. The |
| 85 | most important difference is that in the original Enigma, there were only 5 |
| 86 | or 6 different rotors in existence, and they were applied twice to each |
| 87 | character; the cipher key was the order in which they were placed in the |
| 88 | machine. The Python rotor module uses the supplied key to initialize a |
| 89 | random number generator; the rotor permutations and their initial positions |
| 90 | are then randomly generated. The original device only enciphered the |
| 91 | letters of the alphabet, while this module can handle any 8-bit binary data; |
| 92 | it also produces binary output. This module can also operate with an |
| 93 | arbitrary number of rotors. |
| 94 | |
| 95 | The original Enigma cipher was broken in 1944. % XXX: Is this right? |
| 96 | The version implemented here is probably a good deal more difficult to crack |
| 97 | (especially if you use many rotors), but it won't be impossible for |
| 98 | a truly skilful and determined attacker to break the cipher. So if you want |
| 99 | to keep the NSA out of your files, this rotor cipher may well be unsafe, but |
| 100 | for discouraging casual snooping through your files, it will probably be |
| 101 | just fine, and may be somewhat safer than using the Unix \file{crypt} |
| 102 | command. |
| 103 | \index{National Security Agency}\index{crypt(1)} |
| 104 | % XXX How were Unix commands represented in the docs? |
| 105 | |