home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from hashlib import sha1 as _DefaultHash
-
- class Mode:
- ECB = 1
- CBC = 2
- CFB1 = 3
- CFB8 = 4
- CFB64 = 5
- CFB128 = 6
- OFB = 7
- CTR = 8
- DEFAULT = ECB
-
-
- def _key_prep(key, hash = _DefaultHash, length = 16):
- return hash(key).digest()[:length]
-
-
- def pad(s, sz, padchar = '\x00'):
- extralen = (sz - len(s) % sz) % sz
- return s + extralen * padchar
-
-
- def unpad(s, padchar = '\x00'):
- return s.rstrip(padchar)
-
-
- def _encrypt(key, plain, padchar = '\x00'):
- Cipher = AES
- import Crypto.Cipher
- cipher = Cipher.new(key)
- plain = pad(plain, cipher.block_size, padchar)
- return cipher.encrypt(plain)
-
-
- def _decrypt(key, crypt, padchar = '\x00'):
- Cipher = AES
- import Crypto.Cipher
- cipher = Cipher.new(key)
- return cipher.decrypt(crypt).rstrip(padchar)
-
-
- def cipher_functions(key, padchar = '\x00', mode = Mode.DEFAULT):
-
- def _encrypt(plain):
- return encrypt(key, plain, padchar = padchar, mode = mode)
-
-
- def _decrypt(crypt):
- return decrypt(key, crypt, padchar = padchar, mode = mode)
-
- return (_encrypt, _decrypt)
-
-
- def hash(s, raw = True, Hash = _DefaultHash):
- if raw:
-
- digest = lambda x: x.digest()
- else:
-
- digest = lambda x: x.hexdigest()
- return digest(Hash(s))
-
-
- def encrypt(key, plain, iv = None, padchar = '\x00', mode = Mode.DEFAULT):
- if iv is None:
- iv = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
-
- aes = OpenSSL_AES(key, mode, iv)
- if padchar is not None:
- plain = pad(plain, aes.block_size, padchar)
-
- return aes.encrypt(plain)
-
-
- def decrypt(key, crypt, iv = None, padchar = '\x00', mode = Mode.DEFAULT):
- if iv is None:
- iv = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
-
- aes = OpenSSL_AES(key, mode, iv)
- if padchar is None:
- return aes.decrypt(crypt)
- return unpad(aes.decrypt(crypt), padchar)
-
-
- try:
- from M2Crypto import m2
- except ImportError:
- has_AES = False
- has_DES3 = False
-
- has_AES = True
- has_DES3 = True
-
- class OpenSSL_AES(object):
-
- def __init__(self, key, mode, IV):
- if len(key) not in (16, 24, 32):
- raise AssertionError()
- len(key) not in (16, 24, 32)
- if mode < 1 or mode > 9:
- raise AssertionError()
- mode > 9
- if len(IV) != 16:
- raise AssertionError()
- len(IV) != 16
- self.mode = mode
- self.isBlockCipher = True
- self.block_size = 16
- self.implementation = 'openssl'
- if len(key) == 16:
- self.name = 'aes128'
- elif len(key) == 24:
- self.name = 'aes192'
- elif len(key) == 32:
- self.name = 'aes256'
- else:
- raise AssertionError()
- self.key = len(key) == 16
- self.IV = IV
-
-
- def _createContext(self, encrypt):
- context = m2.cipher_ctx_new()
- keybits = len(self.key) * 8
- mode = [
- None,
- 'ecb',
- 'cbc',
- 'cfb1',
- 'cfb8',
- 'cfb64',
- 'cfb128',
- 'ofb',
- 'ctr'][self.mode]
- if mode is None:
- raise AssertionError
- mode is None
- ciph_type_name = 'aes_%d_%s' % (keybits, mode)
- cipherType = getattr(m2, ciph_type_name)()
- m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
- return context
-
-
- def encrypt(self, plaintext):
- if not plaintext:
- return ''
- context = self._createContext(1)
- ciphertext = m2.cipher_update(context, plaintext)
- m2.cipher_ctx_free(context)
- self.IV = ciphertext[-(self.block_size):]
- return ciphertext
-
-
- def decrypt(self, ciphertext):
- if not ciphertext:
- return ''
- context = self._createContext(0)
- plaintext = m2.cipher_update(context, ciphertext + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
- plaintext = plaintext[:len(ciphertext)]
- m2.cipher_ctx_free(context)
- self.IV = ciphertext[-(self.block_size):]
- return plaintext
-
-
- AES = OpenSSL_AES
-
- class OpenSSL_TripleDES(object):
-
- def __init__(self, key, mode, IV):
- if len(key) != 24:
- raise ValueError()
- len(key) != 24
- if mode != 2:
- raise ValueError()
- mode != 2
- if len(IV) != 8:
- raise ValueError()
- len(IV) != 8
- self.isBlockCipher = True
- self.block_size = 8
- self.implementation = 'openssl'
- self.name = '3des'
- self.key = key
- self.IV = IV
-
-
- def _createContext(self, encrypt):
- context = m2.cipher_ctx_new()
- cipherType = m2.des_ede3_cbc()
- m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
- return context
-
-
- def encrypt(self, plaintext):
- if not plaintext:
- return ''
- context = self._createContext(1)
- ciphertext = m2.cipher_update(context, plaintext)
- m2.cipher_ctx_free(context)
- self.IV = ciphertext[-(self.block_size):]
- return ciphertext
-
-
- def decrypt(self, ciphertext):
- if not ciphertext:
- return ''
- context = self._createContext(0)
- plaintext = m2.cipher_update(context, ciphertext + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
- plaintext = plaintext[:len(ciphertext)]
- m2.cipher_ctx_free(context)
- self.IV = ciphertext[-(self.block_size):]
- return plaintext
-
-
- DES3 = OpenSSL_TripleDES
-
- def nonce(len_ = 16):
- return m2.rand_bytes(len_)
-
-
- def _main():
- key = _key_prep('this is my key')
- plaintext = 'abcd' * 80
- old = encrypt(key, plaintext)
- new = _encrypt(key, plaintext)
- print (old, new), old == new
- print (decrypt(key, new), _decrypt(key, old))
- print 'yay'
-
- if __name__ == '__main__':
- _main()
-
-