blob: a59e2ca0e5b17078377755fcfe8f32dfc0ea495e [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Built-in Module \module{rotor}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{rotor}
3
4\modulesynopsis{Enigma-like encryption and decryption.}
5
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006
Guido van Rossum16d6e711994-08-08 12:30:22 +00007This module implements a rotor-based encryption algorithm, contributed by
Fred Drakefc576191998-04-04 07:15:02 +00008Lance Ellinghouse\index{Ellinghouse, Lance}. The design is derived
9from the Enigma device\indexii{Enigma}{device}, a machine
Guido van Rossum16d6e711994-08-08 12:30:22 +000010used during World War II to encipher messages. A rotor is simply a
11permutation. For example, if the character `A' is the origin of the rotor,
12then a given rotor might map `A' to `L', `B' to `Z', `C' to `G', and so on.
13To encrypt, we choose several different rotors, and set the origins of the
14rotors to known positions; their initial position is the ciphering key. To
15encipher a character, we permute the original character by the first rotor,
16and then apply the second rotor's permutation to the result. We continue
17until we've applied all the rotors; the resulting character is our
18ciphertext. We then change the origin of the final rotor by one position,
19from `A' to `B'; if the final rotor has made a complete revolution, then we
20rotate the next-to-last rotor by one position, and apply the same procedure
21recursively. In other words, after enciphering one character, we advance
22the rotors in the same fashion as a car's odometer. Decoding works in the
23same way, except we reverse the permutations and apply them in the opposite
24order.
Guido van Rossum16d6e711994-08-08 12:30:22 +000025\indexii{Enigma}{cipher}
26
27The available functions in this module are:
28
Fred Drakecce10901998-03-17 06:33:25 +000029\begin{funcdesc}{newrotor}{key\optional{, numrotors}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000030Return a rotor object. \var{key} is a string containing the encryption key
Guido van Rossum16d6e711994-08-08 12:30:22 +000031for the object; it can contain arbitrary binary data. The key will be used
32to randomly generate the rotor permutations and their initial positions.
33\var{numrotors} is the number of rotor permutations in the returned object;
34if it is omitted, a default value of 6 will be used.
35\end{funcdesc}
36
37Rotor objects have the following methods:
38
Fred Drakefc576191998-04-04 07:15:02 +000039\begin{methoddesc}[rotor]{setkey}{key}
Barry Warsaw67170301997-01-02 19:48:00 +000040Sets the rotor's key to \var{key}.
Fred Drakefc576191998-04-04 07:15:02 +000041\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000042
Fred Drakefc576191998-04-04 07:15:02 +000043\begin{methoddesc}[rotor]{encrypt}{plaintext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000044Reset the rotor object to its initial state and encrypt \var{plaintext},
Guido van Rossum16d6e711994-08-08 12:30:22 +000045returning a string containing the ciphertext. The ciphertext is always the
46same length as the original plaintext.
Fred Drakefc576191998-04-04 07:15:02 +000047\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000048
Fred Drakefc576191998-04-04 07:15:02 +000049\begin{methoddesc}[rotor]{encryptmore}{plaintext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000050Encrypt \var{plaintext} without resetting the rotor object, and return a
Guido van Rossum16d6e711994-08-08 12:30:22 +000051string containing the ciphertext.
Fred Drakefc576191998-04-04 07:15:02 +000052\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000053
Fred Drakefc576191998-04-04 07:15:02 +000054\begin{methoddesc}[rotor]{decrypt}{ciphertext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000055Reset the rotor object to its initial state and decrypt \var{ciphertext},
Guido van Rossum16d6e711994-08-08 12:30:22 +000056returning a string containing the ciphertext. The plaintext string will
57always be the same length as the ciphertext.
Fred Drakefc576191998-04-04 07:15:02 +000058\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000059
Fred Drakefc576191998-04-04 07:15:02 +000060\begin{methoddesc}[rotor]{decryptmore}{ciphertext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000061Decrypt \var{ciphertext} without resetting the rotor object, and return a
Guido van Rossum16d6e711994-08-08 12:30:22 +000062string containing the ciphertext.
Fred Drakefc576191998-04-04 07:15:02 +000063\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000064
65An example usage:
Fred Drake19479911998-02-13 06:58:54 +000066\begin{verbatim}
Guido van Rossum16d6e711994-08-08 12:30:22 +000067>>> 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
Fred Drake19479911998-02-13 06:58:54 +000082\end{verbatim}
Fred Drakefc576191998-04-04 07:15:02 +000083
84The module's code is not an exact simulation of the original Enigma
85device; it implements the rotor encryption scheme differently from the
86original. The most important difference is that in the original
87Enigma, there were only 5 or 6 different rotors in existence, and they
88were applied twice to each character; the cipher key was the order in
89which they were placed in the machine. The Python \module{rotor}
90module uses the supplied key to initialize a random number generator;
91the rotor permutations and their initial positions are then randomly
92generated. The original device only enciphered the letters of the
93alphabet, while this module can handle any 8-bit binary data; it also
94produces binary output. This module can also operate with an
Guido van Rossum16d6e711994-08-08 12:30:22 +000095arbitrary number of rotors.
96
97The original Enigma cipher was broken in 1944. % XXX: Is this right?
98The version implemented here is probably a good deal more difficult to crack
99(especially if you use many rotors), but it won't be impossible for
100a truly skilful and determined attacker to break the cipher. So if you want
101to keep the NSA out of your files, this rotor cipher may well be unsafe, but
102for discouraging casual snooping through your files, it will probably be
Fred Drake75fc0451998-02-16 21:36:57 +0000103just fine, and may be somewhat safer than using the \UNIX{} \program{crypt}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000104command.
Fred Drake75fc0451998-02-16 21:36:57 +0000105\index{NSA}
Fred Drakefc576191998-04-04 07:15:02 +0000106\index{National Security Agency}