1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{rotor} ---
|
2000-10-10 14:03:45 -03:00
|
|
|
Enigma-like encryption and decryption}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
2000-10-10 14:03:45 -03:00
|
|
|
\declaremodule{builtin}{rotor}
|
1998-07-23 14:59:49 -03:00
|
|
|
\modulesynopsis{Enigma-like encryption and decryption.}
|
|
|
|
|
2003-04-24 10:19:09 -03:00
|
|
|
\deprecated{2.3}{The encryption algorithm is insecure.}
|
|
|
|
|
1994-01-01 21:22:07 -04:00
|
|
|
|
1994-08-08 09:30:22 -03:00
|
|
|
This module implements a rotor-based encryption algorithm, contributed by
|
1998-04-04 03:15:02 -04:00
|
|
|
Lance Ellinghouse\index{Ellinghouse, Lance}. The design is derived
|
|
|
|
from the Enigma device\indexii{Enigma}{device}, a machine
|
1994-08-08 09:30:22 -03:00
|
|
|
used during World War II to encipher messages. A rotor is simply a
|
|
|
|
permutation. For example, if the character `A' is the origin of the rotor,
|
|
|
|
then a given rotor might map `A' to `L', `B' to `Z', `C' to `G', and so on.
|
|
|
|
To encrypt, we choose several different rotors, and set the origins of the
|
|
|
|
rotors to known positions; their initial position is the ciphering key. To
|
|
|
|
encipher a character, we permute the original character by the first rotor,
|
|
|
|
and then apply the second rotor's permutation to the result. We continue
|
|
|
|
until we've applied all the rotors; the resulting character is our
|
|
|
|
ciphertext. We then change the origin of the final rotor by one position,
|
|
|
|
from `A' to `B'; if the final rotor has made a complete revolution, then we
|
|
|
|
rotate the next-to-last rotor by one position, and apply the same procedure
|
|
|
|
recursively. In other words, after enciphering one character, we advance
|
|
|
|
the rotors in the same fashion as a car's odometer. Decoding works in the
|
|
|
|
same way, except we reverse the permutations and apply them in the opposite
|
|
|
|
order.
|
|
|
|
\indexii{Enigma}{cipher}
|
|
|
|
|
|
|
|
The available functions in this module are:
|
|
|
|
|
1998-03-17 02:33:25 -04:00
|
|
|
\begin{funcdesc}{newrotor}{key\optional{, numrotors}}
|
1995-03-13 06:03:32 -04:00
|
|
|
Return a rotor object. \var{key} is a string containing the encryption key
|
2002-06-10 16:42:43 -03:00
|
|
|
for the object; it can contain arbitrary binary data but not null bytes.
|
|
|
|
The key will be used
|
1994-08-08 09:30:22 -03:00
|
|
|
to randomly generate the rotor permutations and their initial positions.
|
|
|
|
\var{numrotors} is the number of rotor permutations in the returned object;
|
|
|
|
if it is omitted, a default value of 6 will be used.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
Rotor objects have the following methods:
|
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{methoddesc}[rotor]{setkey}{key}
|
2002-06-10 16:42:43 -03:00
|
|
|
Sets the rotor's key to \var{key}. The key should not contain null bytes.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1994-08-08 09:30:22 -03:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{methoddesc}[rotor]{encrypt}{plaintext}
|
1995-03-13 06:03:32 -04:00
|
|
|
Reset the rotor object to its initial state and encrypt \var{plaintext},
|
1994-08-08 09:30:22 -03:00
|
|
|
returning a string containing the ciphertext. The ciphertext is always the
|
|
|
|
same length as the original plaintext.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1994-08-08 09:30:22 -03:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{methoddesc}[rotor]{encryptmore}{plaintext}
|
1995-03-13 06:03:32 -04:00
|
|
|
Encrypt \var{plaintext} without resetting the rotor object, and return a
|
1994-08-08 09:30:22 -03:00
|
|
|
string containing the ciphertext.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1994-08-08 09:30:22 -03:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{methoddesc}[rotor]{decrypt}{ciphertext}
|
1995-03-13 06:03:32 -04:00
|
|
|
Reset the rotor object to its initial state and decrypt \var{ciphertext},
|
2000-11-14 17:43:01 -04:00
|
|
|
returning a string containing the plaintext. The plaintext string will
|
1994-08-08 09:30:22 -03:00
|
|
|
always be the same length as the ciphertext.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1994-08-08 09:30:22 -03:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{methoddesc}[rotor]{decryptmore}{ciphertext}
|
1995-03-13 06:03:32 -04:00
|
|
|
Decrypt \var{ciphertext} without resetting the rotor object, and return a
|
2000-11-14 17:43:01 -04:00
|
|
|
string containing the plaintext.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1994-08-08 09:30:22 -03:00
|
|
|
|
|
|
|
An example usage:
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1994-08-08 09:30:22 -03:00
|
|
|
>>> import rotor
|
|
|
|
>>> rt = rotor.newrotor('key', 12)
|
|
|
|
>>> rt.encrypt('bar')
|
2001-01-24 13:19:08 -04:00
|
|
|
'\xab4\xf3'
|
1994-08-08 09:30:22 -03:00
|
|
|
>>> rt.encryptmore('bar')
|
2001-01-24 13:19:08 -04:00
|
|
|
'\xef\xfd$'
|
1994-08-08 09:30:22 -03:00
|
|
|
>>> rt.encrypt('bar')
|
2001-01-24 13:19:08 -04:00
|
|
|
'\xab4\xf3'
|
|
|
|
>>> rt.decrypt('\xab4\xf3')
|
1994-08-08 09:30:22 -03:00
|
|
|
'bar'
|
2001-01-24 13:19:08 -04:00
|
|
|
>>> rt.decryptmore('\xef\xfd$')
|
1994-08-08 09:30:22 -03:00
|
|
|
'bar'
|
2001-01-24 13:19:08 -04:00
|
|
|
>>> rt.decrypt('\xef\xfd$')
|
|
|
|
'l(\xcd'
|
1994-08-08 09:30:22 -03:00
|
|
|
>>> del rt
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1998-04-04 03:15:02 -04:00
|
|
|
|
|
|
|
The module's code is not an exact simulation of the original Enigma
|
|
|
|
device; it implements the rotor encryption scheme differently from the
|
|
|
|
original. The most important difference is that in the original
|
|
|
|
Enigma, there were only 5 or 6 different rotors in existence, and they
|
|
|
|
were applied twice to each character; the cipher key was the order in
|
|
|
|
which they were placed in the machine. The Python \module{rotor}
|
|
|
|
module uses the supplied key to initialize a random number generator;
|
|
|
|
the rotor permutations and their initial positions are then randomly
|
|
|
|
generated. The original device only enciphered the letters of the
|
|
|
|
alphabet, while this module can handle any 8-bit binary data; it also
|
|
|
|
produces binary output. This module can also operate with an
|
1994-08-08 09:30:22 -03:00
|
|
|
arbitrary number of rotors.
|
|
|
|
|
|
|
|
The original Enigma cipher was broken in 1944. % XXX: Is this right?
|
|
|
|
The version implemented here is probably a good deal more difficult to crack
|
|
|
|
(especially if you use many rotors), but it won't be impossible for
|
2000-07-16 16:01:10 -03:00
|
|
|
a truly skillful and determined attacker to break the cipher. So if you want
|
1994-08-08 09:30:22 -03:00
|
|
|
to keep the NSA out of your files, this rotor cipher may well be unsafe, but
|
|
|
|
for discouraging casual snooping through your files, it will probably be
|
1998-02-16 17:36:57 -04:00
|
|
|
just fine, and may be somewhat safer than using the \UNIX{} \program{crypt}
|
1994-08-08 09:30:22 -03:00
|
|
|
command.
|
1998-02-16 17:36:57 -04:00
|
|
|
\index{NSA}
|
1998-04-04 03:15:02 -04:00
|
|
|
\index{National Security Agency}
|