home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / util / cryptography.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  6.9 KB  |  236 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from hashlib import sha1 as _DefaultHash
  5.  
  6. def _key_prep(key, hash = _DefaultHash, length = 16):
  7.     return hash(key).digest()[:length]
  8.  
  9.  
  10. def pad(s, sz, padchar = '\x00'):
  11.     extralen = (sz - len(s) % sz) % sz
  12.     return s + extralen * padchar
  13.  
  14.  
  15. def unpad(s, padchar = '\x00'):
  16.     return s.rstrip(padchar)
  17.  
  18.  
  19. def _encrypt(key, plain, padchar = '\x00'):
  20.     Cipher = AES
  21.     import Crypto.Cipher
  22.     cipher = Cipher.new(key)
  23.     plain = pad(plain, cipher.block_size, padchar)
  24.     return cipher.encrypt(plain)
  25.  
  26.  
  27. def _decrypt(key, crypt, padchar = '\x00'):
  28.     Cipher = AES
  29.     import Crypto.Cipher
  30.     cipher = Cipher.new(key)
  31.     return cipher.decrypt(crypt).rstrip(padchar)
  32.  
  33.  
  34. def cipher_functions(key, padchar = '\x00'):
  35.     
  36.     def _encrypt(plain):
  37.         return encrypt(key, plain, padchar = padchar)
  38.  
  39.     
  40.     def _decrypt(crypt):
  41.         return decrypt(key, crypt, padchar = padchar)
  42.  
  43.     return (_encrypt, _decrypt)
  44.  
  45.  
  46. class Mode:
  47.     ECB = 1
  48.     CBC = 2
  49.     CFB1 = 3
  50.     CFB8 = 4
  51.     CFB64 = 5
  52.     CFB128 = 6
  53.     OFB = 7
  54.     CTR = 8
  55.  
  56.  
  57. def hash(s, raw = True, Hash = _DefaultHash):
  58.     if raw:
  59.         
  60.         digest = lambda x: x.digest()
  61.     else:
  62.         
  63.         digest = lambda x: x.hexdigest()
  64.     return digest(Hash(s))
  65.  
  66.  
  67. def encrypt(key, plain, iv = None, padchar = '\x00', mode = Mode.ECB):
  68.     if iv is None:
  69.         iv = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  70.     
  71.     aes = OpenSSL_AES(key, mode, iv)
  72.     plain = pad(plain, aes.block_size, padchar)
  73.     return aes.encrypt(plain)
  74.  
  75.  
  76. def decrypt(key, crypt, iv = None, padchar = '\x00', mode = Mode.ECB):
  77.     if iv is None:
  78.         iv = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  79.     
  80.     aes = OpenSSL_AES(key, mode, iv)
  81.     return unpad(aes.decrypt(crypt), padchar)
  82.  
  83.  
  84. try:
  85.     from M2Crypto import m2
  86. except ImportError:
  87.     has_AES = False
  88.     has_DES3 = False
  89.  
  90. has_AES = True
  91. has_DES3 = True
  92.  
  93. class OpenSSL_AES(object):
  94.     
  95.     def __init__(self, key, mode, IV):
  96.         if len(key) not in (16, 24, 32):
  97.             raise AssertionError()
  98.         
  99.         if mode < 1 or mode > 9:
  100.             raise AssertionError()
  101.         
  102.         if len(IV) != 16:
  103.             raise AssertionError()
  104.         
  105.         self.mode = mode
  106.         self.isBlockCipher = True
  107.         self.block_size = 16
  108.         self.implementation = 'openssl'
  109.         if len(key) == 16:
  110.             self.name = 'aes128'
  111.         elif len(key) == 24:
  112.             self.name = 'aes192'
  113.         elif len(key) == 32:
  114.             self.name = 'aes256'
  115.         else:
  116.             raise AssertionError()
  117.         self.key = key
  118.         self.IV = IV
  119.  
  120.     
  121.     def _createContext(self, encrypt):
  122.         context = m2.cipher_ctx_new()
  123.         keybits = len(self.key) * 8
  124.         mode = [
  125.             None,
  126.             'ecb',
  127.             'cbc',
  128.             'cfb1',
  129.             'cfb8',
  130.             'cfb64',
  131.             'cfb128',
  132.             'ofb',
  133.             'ctr'][self.mode]
  134.         if mode is None:
  135.             raise AssertionError
  136.         
  137.         ciph_type_name = 'aes_%d_%s' % (keybits, mode)
  138.         cipherType = getattr(m2, ciph_type_name)()
  139.         m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
  140.         return context
  141.  
  142.     
  143.     def encrypt(self, plaintext):
  144.         if not plaintext:
  145.             return ''
  146.         
  147.         context = self._createContext(1)
  148.         ciphertext = m2.cipher_update(context, plaintext)
  149.         m2.cipher_ctx_free(context)
  150.         self.IV = ciphertext[-(self.block_size):]
  151.         return ciphertext
  152.  
  153.     
  154.     def decrypt(self, ciphertext):
  155.         if not ciphertext:
  156.             return ''
  157.         
  158.         context = self._createContext(0)
  159.         plaintext = m2.cipher_update(context, ciphertext + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  160.         plaintext = plaintext[:len(ciphertext)]
  161.         m2.cipher_ctx_free(context)
  162.         self.IV = ciphertext[-(self.block_size):]
  163.         return plaintext
  164.  
  165.  
  166. AES = OpenSSL_AES
  167.  
  168. class OpenSSL_TripleDES(object):
  169.     
  170.     def __init__(self, key, mode, IV):
  171.         if len(key) != 24:
  172.             raise ValueError()
  173.         
  174.         if mode != 2:
  175.             raise ValueError()
  176.         
  177.         if len(IV) != 8:
  178.             raise ValueError()
  179.         
  180.         self.isBlockCipher = True
  181.         self.block_size = 8
  182.         self.implementation = 'openssl'
  183.         self.name = '3des'
  184.         self.key = key
  185.         self.IV = IV
  186.  
  187.     
  188.     def _createContext(self, encrypt):
  189.         context = m2.cipher_ctx_new()
  190.         cipherType = m2.des_ede3_cbc()
  191.         m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
  192.         return context
  193.  
  194.     
  195.     def encrypt(self, plaintext):
  196.         if not plaintext:
  197.             return ''
  198.         
  199.         context = self._createContext(1)
  200.         ciphertext = m2.cipher_update(context, plaintext)
  201.         m2.cipher_ctx_free(context)
  202.         self.IV = ciphertext[-(self.block_size):]
  203.         return ciphertext
  204.  
  205.     
  206.     def decrypt(self, ciphertext):
  207.         if not ciphertext:
  208.             return ''
  209.         
  210.         context = self._createContext(0)
  211.         plaintext = m2.cipher_update(context, ciphertext + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  212.         plaintext = plaintext[:len(ciphertext)]
  213.         m2.cipher_ctx_free(context)
  214.         self.IV = ciphertext[-(self.block_size):]
  215.         return plaintext
  216.  
  217.  
  218. DES3 = OpenSSL_TripleDES
  219.  
  220. def nonce(len_ = 16):
  221.     return m2.rand_bytes(len_)
  222.  
  223.  
  224. def _main():
  225.     key = _key_prep('this is my key')
  226.     plaintext = 'abcd' * 80
  227.     old = encrypt(key, plaintext)
  228.     new = _encrypt(key, plaintext)
  229.     print (old, new), old == new
  230.     print (decrypt(key, new), _decrypt(key, old))
  231.     print 'yay'
  232.  
  233. if __name__ == '__main__':
  234.     _main()
  235.  
  236.