home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / Crypto / PublicKey / ElGamal.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.8 KB  |  149 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: ElGamal.py,v 1.9 2003/04/04 19:44:26 akuchling Exp $'
  5. from Crypto.PublicKey.pubkey import *
  6. from Crypto.Util import number
  7.  
  8. class error(Exception):
  9.     pass
  10.  
  11.  
  12. def generate(bits, randfunc, progress_func = None):
  13.     """generate(bits:int, randfunc:callable, progress_func:callable)
  14.  
  15.     Generate an ElGamal key of length 'bits', using 'randfunc' to get
  16.     random data and 'progress_func', if present, to display
  17.     the progress of the key generation.
  18.     """
  19.     obj = ElGamalobj()
  20.     if progress_func:
  21.         progress_func('p\n')
  22.     
  23.     obj.p = bignum(getPrime(bits, randfunc))
  24.     if progress_func:
  25.         progress_func('g\n')
  26.     
  27.     size = bits - 1 - (ord(randfunc(1)) & 63)
  28.     if size < 1:
  29.         size = bits - 1
  30.     
  31.     while None:
  32.         obj.g = bignum(getPrime(size, randfunc))
  33.         if obj.g < obj.p:
  34.             break
  35.         
  36.         size = (size + 1) % bits
  37.         if size == 0:
  38.             size = 4
  39.             continue
  40.         continue
  41.         if progress_func:
  42.             progress_func('x\n')
  43.         
  44.     while None:
  45.         size = bits - 1 - ord(randfunc(1))
  46.         if size > 2:
  47.             break
  48.             continue
  49.         continue
  50.         while None:
  51.             obj.x = bignum(getPrime(size, randfunc))
  52.             if obj.x < obj.p:
  53.                 break
  54.             
  55.             size = (size + 1) % bits
  56.             if size == 0:
  57.                 size = 4
  58.                 continue
  59.             continue
  60.             if progress_func:
  61.                 progress_func('y\n')
  62.             
  63.     obj.y = pow(obj.g, obj.x, obj.p)
  64.     return obj
  65.  
  66.  
  67. def construct(tuple):
  68.     '''construct(tuple:(long,long,long,long)|(long,long,long,long,long)))
  69.              : ElGamalobj
  70.     Construct an ElGamal key from a 3- or 4-tuple of numbers.
  71.     '''
  72.     obj = ElGamalobj()
  73.     if len(tuple) not in (3, 4):
  74.         raise error, 'argument for construct() wrong length'
  75.     len(tuple) not in (3, 4)
  76.     for i in range(len(tuple)):
  77.         field = obj.keydata[i]
  78.         setattr(obj, field, tuple[i])
  79.     
  80.     return obj
  81.  
  82.  
  83. class ElGamalobj(pubkey):
  84.     keydata = [
  85.         'p',
  86.         'g',
  87.         'y',
  88.         'x']
  89.     
  90.     def _encrypt(self, M, K):
  91.         a = pow(self.g, K, self.p)
  92.         b = M * pow(self.y, K, self.p) % self.p
  93.         return (a, b)
  94.  
  95.     
  96.     def _decrypt(self, M):
  97.         if not hasattr(self, 'x'):
  98.             raise error, 'Private key not available in this object'
  99.         hasattr(self, 'x')
  100.         ax = pow(M[0], self.x, self.p)
  101.         plaintext = M[1] * inverse(ax, self.p) % self.p
  102.         return plaintext
  103.  
  104.     
  105.     def _sign(self, M, K):
  106.         if not hasattr(self, 'x'):
  107.             raise error, 'Private key not available in this object'
  108.         hasattr(self, 'x')
  109.         p1 = self.p - 1
  110.         if GCD(K, p1) != 1:
  111.             raise error, 'Bad K value: GCD(K,p-1)!=1'
  112.         GCD(K, p1) != 1
  113.         a = pow(self.g, K, self.p)
  114.         t = (M - self.x * a) % p1
  115.         while t < 0:
  116.             t = t + p1
  117.         b = t * inverse(K, p1) % p1
  118.         return (a, b)
  119.  
  120.     
  121.     def _verify(self, M, sig):
  122.         v1 = pow(self.y, sig[0], self.p)
  123.         v1 = v1 * pow(sig[0], sig[1], self.p) % self.p
  124.         v2 = pow(self.g, M, self.p)
  125.         if v1 == v2:
  126.             return 1
  127.         return 0
  128.  
  129.     
  130.     def size(self):
  131.         '''Return the maximum number of bits that can be handled by this key.'''
  132.         return number.size(self.p) - 1
  133.  
  134.     
  135.     def has_private(self):
  136.         '''Return a Boolean denoting whether the object contains
  137.         private components.'''
  138.         if hasattr(self, 'x'):
  139.             return 1
  140.         return 0
  141.  
  142.     
  143.     def publickey(self):
  144.         '''Return a new key object containing only the public information.'''
  145.         return construct((self.p, self.g, self.y))
  146.  
  147.  
  148. object = ElGamalobj
  149.