blob: 63dc56e77f14b16b7cf30bb7e1c67895fea72826 [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Built-in Module \module{rotor}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-rotor}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{rotor}
4
Guido van Rossum16d6e711994-08-08 12:30:22 +00005This module implements a rotor-based encryption algorithm, contributed by
Fred Drakefc576191998-04-04 07:15:02 +00006Lance Ellinghouse\index{Ellinghouse, Lance}. The design is derived
7from the Enigma device\indexii{Enigma}{device}, a machine
Guido van Rossum16d6e711994-08-08 12:30:22 +00008used during World War II to encipher messages. A rotor is simply a
9permutation. For example, if the character `A' is the origin of the rotor,
10then a given rotor might map `A' to `L', `B' to `Z', `C' to `G', and so on.
11To encrypt, we choose several different rotors, and set the origins of the
12rotors to known positions; their initial position is the ciphering key. To
13encipher a character, we permute the original character by the first rotor,
14and then apply the second rotor's permutation to the result. We continue
15until we've applied all the rotors; the resulting character is our
16ciphertext. We then change the origin of the final rotor by one position,
17from `A' to `B'; if the final rotor has made a complete revolution, then we
18rotate the next-to-last rotor by one position, and apply the same procedure
19recursively. In other words, after enciphering one character, we advance
20the rotors in the same fashion as a car's odometer. Decoding works in the
21same way, except we reverse the permutations and apply them in the opposite
22order.
Guido van Rossum16d6e711994-08-08 12:30:22 +000023\indexii{Enigma}{cipher}
24
25The available functions in this module are:
26
Fred Drakecce10901998-03-17 06:33:25 +000027\begin{funcdesc}{newrotor}{key\optional{, numrotors}}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000028Return a rotor object. \var{key} is a string containing the encryption key
Guido van Rossum16d6e711994-08-08 12:30:22 +000029for the object; it can contain arbitrary binary data. The key will be used
30to randomly generate the rotor permutations and their initial positions.
31\var{numrotors} is the number of rotor permutations in the returned object;
32if it is omitted, a default value of 6 will be used.
33\end{funcdesc}
34
35Rotor objects have the following methods:
36
Fred Drakefc576191998-04-04 07:15:02 +000037\begin{methoddesc}[rotor]{setkey}{key}
Barry Warsaw67170301997-01-02 19:48:00 +000038Sets the rotor's key to \var{key}.
Fred Drakefc576191998-04-04 07:15:02 +000039\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000040
Fred Drakefc576191998-04-04 07:15:02 +000041\begin{methoddesc}[rotor]{encrypt}{plaintext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000042Reset the rotor object to its initial state and encrypt \var{plaintext},
Guido van Rossum16d6e711994-08-08 12:30:22 +000043returning a string containing the ciphertext. The ciphertext is always the
44same length as the original plaintext.
Fred Drakefc576191998-04-04 07:15:02 +000045\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000046
Fred Drakefc576191998-04-04 07:15:02 +000047\begin{methoddesc}[rotor]{encryptmore}{plaintext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000048Encrypt \var{plaintext} without resetting the rotor object, and return a
Guido van Rossum16d6e711994-08-08 12:30:22 +000049string containing the ciphertext.
Fred Drakefc576191998-04-04 07:15:02 +000050\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000051
Fred Drakefc576191998-04-04 07:15:02 +000052\begin{methoddesc}[rotor]{decrypt}{ciphertext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000053Reset the rotor object to its initial state and decrypt \var{ciphertext},
Guido van Rossum16d6e711994-08-08 12:30:22 +000054returning a string containing the ciphertext. The plaintext string will
55always be the same length as the ciphertext.
Fred Drakefc576191998-04-04 07:15:02 +000056\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000057
Fred Drakefc576191998-04-04 07:15:02 +000058\begin{methoddesc}[rotor]{decryptmore}{ciphertext}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000059Decrypt \var{ciphertext} without resetting the rotor object, and return a
Guido van Rossum16d6e711994-08-08 12:30:22 +000060string containing the ciphertext.
Fred Drakefc576191998-04-04 07:15:02 +000061\end{methoddesc}
Guido van Rossum16d6e711994-08-08 12:30:22 +000062
63An example usage:
Fred Drake19479911998-02-13 06:58:54 +000064\begin{verbatim}
Guido van Rossum16d6e711994-08-08 12:30:22 +000065>>> import rotor
66>>> rt = rotor.newrotor('key', 12)
67>>> rt.encrypt('bar')
68'\2534\363'
69>>> rt.encryptmore('bar')
70'\357\375$'
71>>> rt.encrypt('bar')
72'\2534\363'
73>>> rt.decrypt('\2534\363')
74'bar'
75>>> rt.decryptmore('\357\375$')
76'bar'
77>>> rt.decrypt('\357\375$')
78'l(\315'
79>>> del rt
Fred Drake19479911998-02-13 06:58:54 +000080\end{verbatim}
Fred Drakefc576191998-04-04 07:15:02 +000081
82The module's code is not an exact simulation of the original Enigma
83device; it implements the rotor encryption scheme differently from the
84original. The most important difference is that in the original
85Enigma, there were only 5 or 6 different rotors in existence, and they
86were applied twice to each character; the cipher key was the order in
87which they were placed in the machine. The Python \module{rotor}
88module uses the supplied key to initialize a random number generator;
89the rotor permutations and their initial positions are then randomly
90generated. The original device only enciphered the letters of the
91alphabet, while this module can handle any 8-bit binary data; it also
92produces binary output. This module can also operate with an
Guido van Rossum16d6e711994-08-08 12:30:22 +000093arbitrary number of rotors.
94
95The original Enigma cipher was broken in 1944. % XXX: Is this right?
96The 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
98a truly skilful and determined attacker to break the cipher. So if you want
99to keep the NSA out of your files, this rotor cipher may well be unsafe, but
100for discouraging casual snooping through your files, it will probably be
Fred Drake75fc0451998-02-16 21:36:57 +0000101just fine, and may be somewhat safer than using the \UNIX{} \program{crypt}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000102command.
Fred Drake75fc0451998-02-16 21:36:57 +0000103\index{NSA}
Fred Drakefc576191998-04-04 07:15:02 +0000104\index{National Security Agency}