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