home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / cryptography.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  7.0 KB  |  237 lines

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