home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / rsa / Tdemo.py < prev   
Text File  |  1993-03-30  |  3KB  |  76 lines

  1. # Demonstration of the RSA cryptosystem.  This lets you use a public
  2. # channel (one where everybody can read what you send) to send
  3. # messages encrypted using a public encryption key, but which can
  4. # only be decrypted by who knows the secret decryption key.
  5. #
  6. # The security of the method comes from the fact that it is hard to
  7. # factorize the product of two large primes -- and we can always use
  8. # larger primes if the enemy buys a faster computer.
  9. #
  10. # It works as follows (in laymen's terms, and without proof):
  11. # - Let n be a large number, the modulus (e.g. about 512 bits).
  12. # - Let sk be the secret key and pk be the public key.
  13. # - Let plaintext be the message we want to encode, a number less than n.
  14. # - Let crypttext be plaintext to the power pk modulo n.
  15. # Then crypttext to the power sk modulo n equals plaintext again.
  16. #
  17. # Actually, n is the product of two (pseudo-)primes n1 and n2, pk is a
  18. # (small) prime, and sk is the inverse of pk mod (n1-1)*(n2-1), i.e.
  19. # pk*sk == 1 mod (n1-1)*(n2-1).  Then x**(pk*sk) == x (mod n) for all x.
  20. # This is the Chinese remainder theorem -- it actually only works for
  21. # real primes!
  22. #
  23. # The hard problem is coming up with suitable primes; our
  24. # implementation only uses pseudo-primes (numbers that aren't
  25. # obviously non-primes, given a suitable pseudo-primality test).
  26. #
  27. # Reference:
  28. #
  29. # RL Rivest, A Shamir, L Adleman.  A method for obtaining digital
  30. # signatures and public-key cryptosystems.  CACM 21(2):120-126,
  31. # February, 1978.
  32.  
  33.  
  34. # Import some functions
  35. from rsakeys import makersaset, default_pk
  36. from simplerandom import simplerandom
  37. from mpz import powm
  38. from T import TSTART, TSTOP
  39.  
  40. # Make a random function (the seed determines the secret key computed!)
  41. rf = simplerandom('<YOUR SEED HERE...>').random
  42.  
  43. # Use the default public key
  44. pk = default_pk
  45.  
  46. # Choose a random n and secret key (this can take from 5-50 seconds
  47. # on an R4000 processor at 50 MHz!).  Note that this is deterministic
  48. # if you don't vary the seed for the random function above
  49. TSTART()
  50. n, sk = makersaset(512, rf)
  51. TSTOP('makersaset')
  52. print n
  53. print sk
  54.  
  55. # Choose a random plaintext message to test the algorithms
  56. TSTART()
  57. plaintext = rf(n)
  58. TSTOP('plaintext')
  59. print plaintext
  60.  
  61. # Encrypt the plaintext
  62. TSTART()
  63. crypttext = powm(plaintext, pk, n)
  64. TSTOP('crypttext')
  65. print crypttext
  66.  
  67. # Decrypt the crypttext
  68. TSTART()
  69. testvalue = powm(crypttext, sk, n)
  70. TSTOP('testvalue')
  71. print testvalue
  72.  
  73. # The decrypted crypttext should be the same as the original plaintext
  74. if testvalue != plaintext:
  75.     raise 'FATAL', 'RSA implementation failed!'
  76.