Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 1 | \section{Built-in Module \module{rotor}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{builtin}{rotor} |
| 3 | |
| 4 | \modulesynopsis{Enigma-like encryption and decryption.} |
| 5 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 7 | This module implements a rotor-based encryption algorithm, contributed by |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 8 | Lance Ellinghouse\index{Ellinghouse, Lance}. The design is derived |
| 9 | from the Enigma device\indexii{Enigma}{device}, a machine |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 10 | used during World War II to encipher messages. A rotor is simply a |
| 11 | permutation. For example, if the character `A' is the origin of the rotor, |
| 12 | then a given rotor might map `A' to `L', `B' to `Z', `C' to `G', and so on. |
| 13 | To encrypt, we choose several different rotors, and set the origins of the |
| 14 | rotors to known positions; their initial position is the ciphering key. To |
| 15 | encipher a character, we permute the original character by the first rotor, |
| 16 | and then apply the second rotor's permutation to the result. We continue |
| 17 | until we've applied all the rotors; the resulting character is our |
| 18 | ciphertext. We then change the origin of the final rotor by one position, |
| 19 | from `A' to `B'; if the final rotor has made a complete revolution, then we |
| 20 | rotate the next-to-last rotor by one position, and apply the same procedure |
| 21 | recursively. In other words, after enciphering one character, we advance |
| 22 | the rotors in the same fashion as a car's odometer. Decoding works in the |
| 23 | same way, except we reverse the permutations and apply them in the opposite |
| 24 | order. |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 25 | \indexii{Enigma}{cipher} |
| 26 | |
| 27 | The available functions in this module are: |
| 28 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 29 | \begin{funcdesc}{newrotor}{key\optional{, numrotors}} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 30 | 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] | 31 | for the object; it can contain arbitrary binary data. The key will be used |
| 32 | to randomly generate the rotor permutations and their initial positions. |
| 33 | \var{numrotors} is the number of rotor permutations in the returned object; |
| 34 | if it is omitted, a default value of 6 will be used. |
| 35 | \end{funcdesc} |
| 36 | |
| 37 | Rotor objects have the following methods: |
| 38 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 39 | \begin{methoddesc}[rotor]{setkey}{key} |
Barry Warsaw | 6717030 | 1997-01-02 19:48:00 +0000 | [diff] [blame] | 40 | Sets the rotor's key to \var{key}. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 41 | \end{methoddesc} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 42 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 43 | \begin{methoddesc}[rotor]{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. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 47 | \end{methoddesc} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 48 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 49 | \begin{methoddesc}[rotor]{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. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 52 | \end{methoddesc} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 53 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 54 | \begin{methoddesc}[rotor]{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. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 58 | \end{methoddesc} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 59 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 60 | \begin{methoddesc}[rotor]{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. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 63 | \end{methoddesc} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 64 | |
| 65 | An example usage: |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 66 | \begin{verbatim} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 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 |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 82 | \end{verbatim} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 83 | |
| 84 | The module's code is not an exact simulation of the original Enigma |
| 85 | device; it implements the rotor encryption scheme differently from the |
| 86 | original. The most important difference is that in the original |
| 87 | Enigma, there were only 5 or 6 different rotors in existence, and they |
| 88 | were applied twice to each character; the cipher key was the order in |
| 89 | which they were placed in the machine. The Python \module{rotor} |
| 90 | module uses the supplied key to initialize a random number generator; |
| 91 | the rotor permutations and their initial positions are then randomly |
| 92 | generated. The original device only enciphered the letters of the |
| 93 | alphabet, while this module can handle any 8-bit binary data; it also |
| 94 | produces binary output. This module can also operate with an |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 95 | arbitrary number of rotors. |
| 96 | |
| 97 | The original Enigma cipher was broken in 1944. % XXX: Is this right? |
| 98 | The 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 |
| 100 | a truly skilful and determined attacker to break the cipher. So if you want |
| 101 | to keep the NSA out of your files, this rotor cipher may well be unsafe, but |
| 102 | for discouraging casual snooping through your files, it will probably be |
Fred Drake | 75fc045 | 1998-02-16 21:36:57 +0000 | [diff] [blame] | 103 | just fine, and may be somewhat safer than using the \UNIX{} \program{crypt} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 104 | command. |
Fred Drake | 75fc045 | 1998-02-16 21:36:57 +0000 | [diff] [blame] | 105 | \index{NSA} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 106 | \index{National Security Agency} |