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