home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2243 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.5 KB  |  161 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: AllOrNothing.py,v 1.8 2003/02/28 15:23:20 akuchling Exp $'
  5. import operator
  6. import string
  7. from Crypto.Util.number import bytes_to_long, long_to_bytes
  8.  
  9. class AllOrNothing:
  10.     
  11.     def __init__(self, ciphermodule, mode = None, IV = None):
  12.         self._AllOrNothing__ciphermodule = ciphermodule
  13.         self._AllOrNothing__mode = mode
  14.         self._AllOrNothing__IV = IV
  15.         self._AllOrNothing__key_size = ciphermodule.key_size
  16.         if self._AllOrNothing__key_size == 0:
  17.             self._AllOrNothing__key_size = 16
  18.         
  19.  
  20.     __K0digit = chr(105)
  21.     
  22.     def digest(self, text):
  23.         key = self._inventkey(self._AllOrNothing__key_size)
  24.         K0 = self._AllOrNothing__K0digit * self._AllOrNothing__key_size
  25.         mcipher = self._AllOrNothing__newcipher(key)
  26.         hcipher = self._AllOrNothing__newcipher(K0)
  27.         block_size = self._AllOrNothing__ciphermodule.block_size
  28.         padbytes = block_size - len(text) % block_size
  29.         text = text + ' ' * padbytes
  30.         s = len(text) / block_size
  31.         blocks = []
  32.         hashes = []
  33.         for i in range(1, s + 1):
  34.             start = (i - 1) * block_size
  35.             end = start + block_size
  36.             mi = text[start:end]
  37.             cipherblock = mcipher.encrypt(long_to_bytes(i, block_size))
  38.             mticki = bytes_to_long(mi) ^ bytes_to_long(cipherblock)
  39.             blocks.append(mticki)
  40.             hi = hcipher.encrypt(long_to_bytes(mticki ^ i, block_size))
  41.             hashes.append(bytes_to_long(hi))
  42.         
  43.         i = i + 1
  44.         cipherblock = mcipher.encrypt(long_to_bytes(i, block_size))
  45.         mticki = padbytes ^ bytes_to_long(cipherblock)
  46.         blocks.append(mticki)
  47.         hi = hcipher.encrypt(long_to_bytes(mticki ^ i, block_size))
  48.         hashes.append(bytes_to_long(hi))
  49.         mtick_stick = bytes_to_long(key) ^ reduce(operator.xor, hashes)
  50.         blocks.append(mtick_stick)
  51.         return map(long_to_bytes, blocks)
  52.  
  53.     
  54.     def undigest(self, blocks):
  55.         if len(blocks) < 2:
  56.             raise ValueError, 'List must be at least length 2.'
  57.         len(blocks) < 2
  58.         blocks = map(bytes_to_long, blocks)
  59.         K0 = self._AllOrNothing__K0digit * self._AllOrNothing__key_size
  60.         hcipher = self._AllOrNothing__newcipher(K0)
  61.         hashes = []
  62.         for i in range(1, len(blocks)):
  63.             mticki = blocks[i - 1] ^ i
  64.             hi = hcipher.encrypt(long_to_bytes(mticki))
  65.             hashes.append(bytes_to_long(hi))
  66.         
  67.         key = blocks[-1] ^ reduce(operator.xor, hashes)
  68.         mcipher = self._AllOrNothing__newcipher(long_to_bytes(key))
  69.         block_size = self._AllOrNothing__ciphermodule.block_size
  70.         parts = []
  71.         for i in range(1, len(blocks)):
  72.             cipherblock = mcipher.encrypt(long_to_bytes(i, block_size))
  73.             mi = blocks[i - 1] ^ bytes_to_long(cipherblock)
  74.             parts.append(mi)
  75.         
  76.         padbytes = int(parts[-1])
  77.         text = string.join(map(long_to_bytes, parts[:-1]), '')
  78.         return text[:-padbytes]
  79.  
  80.     
  81.     def _inventkey(self, key_size):
  82.         import time
  83.         randpool = randpool
  84.         import Crypto.Util
  85.         pool = randpool.RandomPool(key_size * 2)
  86.         while key_size > pool.entropy:
  87.             pool.add_event()
  88.         return pool.get_bytes(key_size)
  89.  
  90.     
  91.     def __newcipher(self, key):
  92.         if self._AllOrNothing__mode is None and self._AllOrNothing__IV is None:
  93.             return self._AllOrNothing__ciphermodule.new(key)
  94.         if self._AllOrNothing__IV is None:
  95.             return self._AllOrNothing__ciphermodule.new(key, self._AllOrNothing__mode)
  96.         return self._AllOrNothing__ciphermodule.new(key, self._AllOrNothing__mode, self._AllOrNothing__IV)
  97.  
  98.  
  99. if __name__ == '__main__':
  100.     import sys
  101.     import getopt
  102.     import base64
  103.     usagemsg = 'Test module usage: %(program)s [-c cipher] [-l] [-h]\n\nWhere:\n    --cipher module\n    -c module\n        Cipher module to use.  Default: %(ciphermodule)s\n\n    --aslong\n    -l\n        Print the encoded message blocks as long integers instead of base64\n        encoded strings\n\n    --help\n    -h\n        Print this help message\n'
  104.     ciphermodule = 'AES'
  105.     aslong = 0
  106.     
  107.     def usage(code, msg = None):
  108.         if msg:
  109.             print msg
  110.         
  111.         print usagemsg % {
  112.             'program': sys.argv[0],
  113.             'ciphermodule': ciphermodule }
  114.         sys.exit(code)
  115.  
  116.     
  117.     try:
  118.         (opts, args) = getopt.getopt(sys.argv[1:], 'c:l', [
  119.             'cipher=',
  120.             'aslong'])
  121.     except getopt.error:
  122.         msg = None
  123.         usage(1, msg)
  124.  
  125.     if args:
  126.         usage(1, 'Too many arguments')
  127.     
  128.     for opt, arg in opts:
  129.         if opt in ('-h', '--help'):
  130.             usage(0)
  131.             continue
  132.         if opt in ('-c', '--cipher'):
  133.             ciphermodule = arg
  134.             continue
  135.         if opt in ('-l', '--aslong'):
  136.             aslong = 1
  137.             continue
  138.     
  139.     module = __import__('Crypto.Cipher.' + ciphermodule, None, None, [
  140.         'new'])
  141.     a = AllOrNothing(module)
  142.     print 'Original text:\n=========='
  143.     print __doc__
  144.     print '=========='
  145.     msgblocks = a.digest(__doc__)
  146.     print 'message blocks:'
  147.     for i, blk in map(None, range(len(msgblocks)), msgblocks):
  148.         print '    %3d' % i,
  149.         if aslong:
  150.             print bytes_to_long(blk)
  151.             continue
  152.         print base64.encodestring(blk)[:-1]
  153.     
  154.     b = AllOrNothing(module)
  155.     text = b.undigest(msgblocks)
  156.     if text == __doc__:
  157.         print 'They match!'
  158.     else:
  159.         print 'They differ!'
  160.  
  161.