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