Advertisement
Guest User

Eugene Tan

a guest
Sep 17th, 2009
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. """
  2. RSA public key cipher
  3.  
  4. >>> k = keygen(61, 53)
  5. >>> public = (k[0], k[1])
  6. >>> private = (k[0], k[2])
  7. >>> ciphertext = rsa(123, public, None)
  8. >>> rsa(ciphertext, None, private, decrypt=True)
  9. 123
  10. """
  11.  
  12. import math
  13. import random
  14.  
  15. def eeuclid(a, n, debug=False):
  16.     """Finds the GCD or multiplicative inverse of a number mod n
  17.    
  18.    Returns a two-element sequence in the format
  19.        (r, i)
  20.    where r either the gcd or inverse, and i is True if result is
  21.    inverse, False if result is gcd.
  22.  
  23.    >>> eeuclid(24141, 40902)
  24.    (3.0, False)
  25.    >>> eeuclid(7, 2311)
  26.    (-330.0, True)
  27.    """
  28.     a1, a2, a3 = 1, 0, n
  29.     b1, b2, b3 = 0, 1, a
  30.     while b3 > 1:
  31.         q = math.floor(a3/b3)
  32.         t1, t2, t3 = (a1 - (q * b1), a2 - (q * b2), a3 - (q * b3))
  33.         a1, a2, a3 = b1, b2, b3
  34.         b1, b2, b3 = t1, t2, t3
  35.     if b3 == 1:
  36.         if debug:
  37.             print 'return inverse'
  38.         return (b2, True)
  39.     if b3 == 0:
  40.         if debug:
  41.             print 'return gcd'
  42.         return (a3, False)
  43.  
  44. def totient(p, q):
  45.     """Returns the Euler totient of two numbers
  46.  
  47.    >>> p = 7
  48.    >>> q = 5
  49.    >>> totient(p, q)
  50.    24
  51.    """
  52.     return (p - 1) * (q - 1)
  53.  
  54. def coprime(t):
  55.     """Finds a possible coprime of a number
  56.  
  57.    >>> p = coprime(23)
  58.    >>> eeuclid(p, 23)[1]
  59.    True
  60.    """
  61.     nums = eratosthenes(t)
  62.     return random.choice(nums)
  63.  
  64. def eratosthenes(n):
  65.     """Generates a list of primes less than or equal to n
  66.  
  67.    Uses the Sieve of Eratosthenes.
  68.  
  69.    >>> eratosthenes(30)
  70.    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
  71.    """
  72.     nums = range(2, n)
  73.     p = t = 2
  74.     while p**2 <= n:
  75.         while t <= n:
  76.             s = t * p
  77.             if s in nums:
  78.                 del nums[nums.index(s)]
  79.             t += 1
  80.         p = t = nums[nums.index(p) + 1]
  81.     return nums
  82.  
  83. def keygen(p, q, e=None):
  84.     """Generate a key pair from two distinct prime numbers
  85.  
  86.    Returns a 3-element sequence in the following format:
  87.        (n, e, d)
  88.    where n is the modulo for the finite field, e is the
  89.    public key exponent and d is the multiplicative inverse.
  90.  
  91.    e may be also provided as an optional parameter.
  92.  
  93.    >>> keygen(61, 53, e=17)
  94.    (3233, 17, 2753)
  95.    """
  96.     n = p * q
  97.     t = totient(p, q)
  98.     if e is None:
  99.         e = coprime(t)
  100.     d = int(eeuclid(e, t)[0] % t)
  101.     return (n, e, d)
  102.  
  103. def rsa(message, public, private, decrypt=False):
  104.     """Encrypts or decrypts a message with RSA
  105.  
  106.    The public and private key format is in
  107.        (n, k)
  108.    where n is the product of two primes and k is either the
  109.    public key exponent (for encryption) or it's multiplicative
  110.    inverse (for decryption)
  111.  
  112.    Private key can be None for encryption since it is not used.
  113.    Likewise, public key can be None for decryption
  114.    
  115.    >>> public = (3233, 17)
  116.    >>> private = (3233, 2753)
  117.    >>> rsa(123, public, private)
  118.    855
  119.    >>> rsa(855, public, private, decrypt=True)
  120.    123
  121.    """
  122.     if decrypt is False:
  123.         return int(message**public[1] % public[0])
  124.     else:
  125.         return int(message**private[1] % private[0])
  126.  
  127. if __name__ == "__main__":
  128.     import doctest
  129.     doctest.testmod()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement