home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / r / rotor < prev   
Text File  |  1996-11-14  |  5KB  |  92 lines

  1. <TITLE>rotor -- Python library reference</TITLE>
  2. Prev: <A HREF="../m/mpz" TYPE="Prev">mpz</A>  
  3. Up: <A HREF="../c/cryptographic_services" TYPE="Up">Cryptographic Services</A>  
  4. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  5. <H1>12.3. Built-in Module <CODE>rotor</CODE></H1>
  6. This module implements a rotor-based encryption algorithm, contributed by
  7. Lance Ellinghouse.  The design is derived from the Enigma device, a machine
  8. used during World War II to encipher messages.  A rotor is simply a
  9. permutation.  For example, if the character `A' is the origin of the rotor,
  10. then a given rotor might map `A' to `L', `B' to `Z', `C' to `G', and so on.
  11. To encrypt, we choose several different rotors, and set the origins of the
  12. rotors to known positions; their initial position is the ciphering key.  To
  13. encipher a character, we permute the original character by the first rotor,
  14. and then apply the second rotor's permutation to the result. We continue
  15. until we've applied all the rotors; the resulting character is our
  16. ciphertext.  We then change the origin of the final rotor by one position,
  17. from `A' to `B'; if the final rotor has made a complete revolution, then we
  18. rotate the next-to-last rotor by one position, and apply the same procedure
  19. recursively.  In other words, after enciphering one character, we advance
  20. the rotors in the same fashion as a car's odometer. Decoding works in the
  21. same way, except we reverse the permutations and apply them in the opposite
  22. order.
  23. The available functions in this module are:
  24. <P>
  25. <DL><DT><B>newrotor</B> (<VAR>key</VAR>[, <VAR>numrotors</VAR>]) -- function of module rotor<DD>
  26. Return a rotor object. <VAR>key</VAR> is a string containing the encryption key
  27. for the object; it can contain arbitrary binary data. The key will be used
  28. to randomly generate the rotor permutations and their initial positions.
  29. <VAR>numrotors</VAR> is the number of rotor permutations in the returned object;
  30. if it is omitted, a default value of 6 will be used.
  31. </DL>
  32. Rotor objects have the following methods:
  33. <P>
  34. <DL><DT><B>setkey</B> () -- Method on rotor<DD>
  35. Reset the rotor to its initial state.
  36. </DL>
  37. <DL><DT><B>encrypt</B> (<VAR>plaintext</VAR>) -- Method on rotor<DD>
  38. Reset the rotor object to its initial state and encrypt <VAR>plaintext</VAR>,
  39. returning a string containing the ciphertext.  The ciphertext is always the
  40. same length as the original plaintext.
  41. </DL>
  42. <DL><DT><B>encryptmore</B> (<VAR>plaintext</VAR>) -- Method on rotor<DD>
  43. Encrypt <VAR>plaintext</VAR> without resetting the rotor object, and return a
  44. string containing the ciphertext.
  45. </DL>
  46. <DL><DT><B>decrypt</B> (<VAR>ciphertext</VAR>) -- Method on rotor<DD>
  47. Reset the rotor object to its initial state and decrypt <VAR>ciphertext</VAR>,
  48. returning a string containing the ciphertext.  The plaintext string will
  49. always be the same length as the ciphertext.
  50. </DL>
  51. <DL><DT><B>decryptmore</B> (<VAR>ciphertext</VAR>) -- Method on rotor<DD>
  52. Decrypt <VAR>ciphertext</VAR> without resetting the rotor object, and return a
  53. string containing the ciphertext.
  54. </DL>
  55. An example usage:
  56. <UL COMPACT><CODE>>>> import rotor<P>
  57. >>> rt = rotor.newrotor('key', 12)<P>
  58. >>> rt.encrypt('bar')<P>
  59. '\2534\363'<P>
  60. >>> rt.encryptmore('bar')<P>
  61. '\357\375$'<P>
  62. >>> rt.encrypt('bar')<P>
  63. '\2534\363'<P>
  64. >>> rt.decrypt('\2534\363')<P>
  65. 'bar'<P>
  66. >>> rt.decryptmore('\357\375$')<P>
  67. 'bar'<P>
  68. >>> rt.decrypt('\357\375$')<P>
  69. 'l(\315'<P>
  70. >>> del rt<P>
  71. </CODE></UL>
  72. The module's code is not an exact simulation of the original Enigma device;
  73. it implements the rotor encryption scheme differently from the original. The
  74. most important difference is that in the original Enigma, there were only 5
  75. or 6 different rotors in existence, and they were applied twice to each
  76. character; the cipher key was the order in which they were placed in the
  77. machine.  The Python rotor module uses the supplied key to initialize a
  78. random number generator; the rotor permutations and their initial positions
  79. are then randomly generated.  The original device only enciphered the
  80. letters of the alphabet, while this module can handle any 8-bit binary data;
  81. it also produces binary output.  This module can also operate with an
  82. arbitrary number of rotors.
  83. <P>
  84. The original Enigma cipher was broken in 1944. 
  85. The version implemented here is probably a good deal more difficult to crack
  86. (especially if you use many rotors), but it won't be impossible for
  87. a truly skilful and determined attacker to break the cipher.  So if you want
  88. to keep the NSA out of your files, this rotor cipher may well be unsafe, but
  89. for discouraging casual snooping through your files, it will probably be
  90. just fine, and may be somewhat safer than using the Unix <FILE>crypt</FILE>
  91. command.
  92.