Ejemplo n.º 1
0
def AES(key):
    try:
        from crypto.cipher.rijndael import Rijndael
        from crypto.cipher.base     import noPadding
        return Rijndael(key, keySize=32, blockSize=16, padding=noPadding())
    except ImportError:
        from crypto.cipher.rijndael import Rijndael
        from crypto.cipher.base     import noPadding
        return Rijndael(key, keySize=32, blockSize=16, padding=noPadding())
        def CBCtestVector(key,iv,pt,kct):
            """ CBC test vectors using AES algorithm """
            key,iv,pt,kct = a2b_hex(key),a2b_hex(iv),a2b_p(pt),a2b_p(kct)
            alg = AES_CBC(key, padding=noPadding())

            self.assertEqual( alg.encrypt(pt,iv=iv), kct )
            self.assertEqual( alg.decrypt(iv+kct),   pt )
 def testAutoIV(self):
     k   = a2b_hex('2b7e151628aed2a6abf7158809cf4f3c')
     alg = AES_CBC(key=k, padding=noPadding())
     pt  = a2b_hex('6bc1bee22e409f96e93d7e117393172a')
     ct  = alg.encrypt(pt)
     dct = alg.decrypt(ct)
     self.assertEqual( dct, pt )  # 'AES_CBC auto IV error'
Ejemplo n.º 4
0
    def __init__(self, blockCipherInstance, autoNonce=None, macSize=8, nonceSize=13):
        """ CCM algorithms are created by initializing with a BlockCipher instance
                blockCipherInstance -> typically AES_ECB
                autoNonce -> sets the intial value of a nonce for automatic nonce
                             creation (not available yet)
                macSize   -> size of MAC field can be = 4, 6, 8, 10, 12, 14, or 16
                nonceSize -> size of nonce in bytes (default 13)
                the counter size is blockSize-nonceSize-1
        """
        self.baseCipher = blockCipherInstance
        self.name       = self.baseCipher.name + '_CCM'
        self.blockSize  = self.baseCipher.blockSize
        self.keySize    = self.baseCipher.keySize

        self.baseCipher.padding = noPadding()   # baseCipher should NOT pad!!


        self.M = macSize        # Number of octets
        if  not((3 < self.M < 17) and (macSize%2==0)) :
            raise InitCryptoError, 'CCM, M (size of auth field) is out of bounds'

        self.nonceSize  = nonceSize
        self.L = self.baseCipher.blockSize - self.nonceSize - 1
        if not(1 < self.L < 9) :
            raise InitCryptoError, 'CCM, L (size of length field) is out of bounds'
        self.reset()
        def RijndaelTestVec(i, key, pt, ct):
            """ Run single AES test vector with any legal blockSize
                and any legal key size. """
            bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex(ct)
            kSize = len(bkey)
            bSize = len(cipherText) # set block size to length of block
            alg = Rijndael(bkey, keySize=kSize, blockSize=bSize, padding=noPadding())

            self.assertEqual( alg.encrypt(plainText),  cipherText )
            self.assertEqual( alg.decrypt(cipherText), plainText )
	def decodeSubtitles(self, id, iv, data):
		compressed = True
		key = self.generateKey(id)
		iv = Common().ByteArrayToString(Base64Decoder().decode(iv))
		data = Common().ByteArrayToString(Base64Decoder().decode(data))
		data = iv+data
		cipher = AES_CBC(key, padding=noPadding(), keySize=32)
		decryptedData = cipher.decrypt(data)
		if compressed:
			return zlib.decompress(decryptedData)
		else:
			return decryptedData
Ejemplo n.º 7
0
 def decode(self, id, iv, data, compressed=True):
     key = generateKey(id)
     iv = base64.b64decode(iv)
     data = base64.b64decode(data)
     data = iv + data
     cipher = AES_CBC(key, padding=noPadding(), keySize=32)
     decryptedData = cipher.decrypt(data)
     
     if compressed:
         return zlib.decompress(decryptedData)
     else:
         return decryptedData
Ejemplo n.º 8
0
def decrypt_gz(key, cipher_text):
    ''' Decrypt cipher_text using key.
    decrypt(str, str) -> cleartext (gzipped xml)

    This function will use the underlying, available, cipher module.
    '''
    if USE_PYCRYPTO:
        # Extract IV
        c = AES.new(key)
        iv = c.decrypt(cipher_text[12:28])
        # Decrypt data, CBC mode
        c = AES.new(key, AES.MODE_CBC, iv)
        ct = c.decrypt(cipher_text[28:])
    else:
        # Extract IV
        c = rijndael.Rijndael(key, keySize=len(key), padding=noPadding())
        iv = c.decrypt(cipher_text[12:28])
        # Decrypt data, CBC mode
        bc = rijndael.Rijndael(key, keySize=len(key), padding=noPadding())
        c = cbc.CBC(bc, padding=noPadding())
        ct = c.decrypt(cipher_text[28:], iv=iv)
    return ct
Ejemplo n.º 9
0
 def __init__(self, blockCipherInstance, padding = padWithPadLen()):
     """ CBC algorithms are created by initializing with a BlockCipher instance """
     self.baseCipher = blockCipherInstance
     self.name       = self.baseCipher.name + '_CBC'
     self.blockSize  = self.baseCipher.blockSize
     self.keySize    = self.baseCipher.keySize
     self.padding    = padding
     self.baseCipher.padding = noPadding()   # baseCipher should NOT pad!!
     self.r          = Random()            # for IV generation, currently uses
                                           # mediocre standard distro version     <----------------
     import time
     newSeed = time.ctime()+str(self.r)    # seed with instance location
     self.r.seed(newSeed)                  # to make unique
     self.reset()
Ejemplo n.º 10
0
    def decode_subtitles(self, id, iv, data):
        compressed = True
        key = self.generate_key(id)
        iv = base64.b64decode(iv)
        data = base64.b64decode(data)
        data = iv+data
        #cipher = AES.new(key, AES.MODE_CBC, iv)
        cipher = AES_CBC(key, padding=noPadding(), keySize=32)
        decryptedData = cipher.decrypt(data)

        if compressed:
            return zlib.decompress(decryptedData)
        else:
            return decryptedData
Ejemplo n.º 11
0
 def IcedollTestVec(i, key, pt, ct):
     """ Run single AES test vector with any legal blockSize
         and any legal key size. """
     bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex(ct)
     kSize = len(bkey)
     bSize = len(cipherText) # set block size to length of block
     alg = Icedoll(bkey, keySize=kSize, blockSize=bSize, padding=noPadding())
     cct = alg.encrypt(plainText)
     print 'pt    =',b2a_p(plainText)
     print 'ct    =',b2a_p(cct)
     dcct = alg.decrypt(cct)
     #print '_dcct',b2a_p(dcct)
     self.assertEqual( dcct, plainText )
     self.assertEqual( alg.encrypt(plainText),  cipherText )
     self.assertEqual( alg.decrypt(cipherText), plainText )
Ejemplo n.º 12
0
    def testDctEqPt(self):
        """ test of plaintext = decrypt(encrypt(plaintext)) """
        alg = Icedoll( 16*chr(0), padding=noPadding())
        pt  = 16*4*'a'             # block aligned
        ct  = alg.encrypt(pt)
        print 'ct  = ',b2a_p(ct)
        dct = alg.decrypt(ct)
        print 'dct = ',b2a_p(dct)
        assert(pt == dct), 'pt != dct'

        alg = Icedoll( 16*chr(0))  # autoPad
        pt  = 17*4*'a'             # non-block aligned
        ct  = alg.encrypt(pt)
        print 'ct  = ',b2a_p(ct)
        dct = alg.decrypt(ct)
        print 'dct = ',b2a_p(dct)
        assert(pt == dct), 'pt != dct'
Ejemplo n.º 13
0
	def ooyalaDecrypt(self, data):
		
		print "Ooyala: --> Attempting to decrypt SMIL..."
		
		crypt = {'KEY':'4b3d32bed59fb8c54ab8a190d5d147f0e4f0cbe6804c8e0721175ab68b40cb01','IV':'00020406080a0c0ea0a2a4a6a8aaacae'}
		decodedByteArray = Base64Decoder().decode(data)
		data = Common().ByteArrayToString(decodedByteArray)
		aes_key = unhexlify(crypt['KEY'])
		iv_bytes = iv=unhexlify(crypt['IV'])
		d = iv_bytes + data
		cipher = AES_CBC(aes_key, padding=noPadding(), keySize=32)
		v = cipher.decrypt(d)
		length = struct.unpack('>I', v[:4])[0]
		compressed = v[4:length+4]
		decompressed = zlib.decompress(compressed)
		
		print "Ooyala: --> SMIL decrypted successfully."
		
		return decompressed[16:]
Ejemplo n.º 14
0
 def IcedollTestVec(i, key, pt, ct):
     """ Run single AES test vector with any legal blockSize
         and any legal key size. """
     bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex(
         ct)
     kSize = len(bkey)
     bSize = len(cipherText)  # set block size to length of block
     alg = Icedoll(bkey,
                   keySize=kSize,
                   blockSize=bSize,
                   padding=noPadding())
     cct = alg.encrypt(plainText)
     print 'pt    =', b2a_p(plainText)
     print 'ct    =', b2a_p(cct)
     dcct = alg.decrypt(cct)
     #print '_dcct',b2a_p(dcct)
     self.assertEqual(dcct, plainText)
     self.assertEqual(alg.encrypt(plainText), cipherText)
     self.assertEqual(alg.decrypt(cipherText), plainText)
Ejemplo n.º 15
0
	def ooyalaDecrypt(self, data):
		
		print "Ooyala: --> Attempting to decrypt SMIL..."
		
		crypt = {'KEY':'4b3d32bed59fb8c54ab8a190d5d147f0e4f0cbe6804c8e0721175ab68b40cb01','IV':'00020406080a0c0ea0a2a4a6a8aaacae'}
		decodedByteArray = Base64Decoder().decode(data)
		data = Common().ByteArrayToString(decodedByteArray)
		aes_key = unhexlify(crypt['KEY'])
		iv_bytes = iv=unhexlify(crypt['IV'])
		d = iv_bytes + data
		cipher = AES_CBC(aes_key, padding=noPadding(), keySize=32)
		v = cipher.decrypt(d)
		length = struct.unpack('>I', v[:4])[0]
		compressed = v[4:length+4]
		decompressed = zlib.decompress(compressed)
		
		print "Ooyala: --> SMIL decrypted successfully."
		
		return decompressed[16:]
Ejemplo n.º 16
0
 def __init__(self, key=None, padding=padWithPadLen(), keySize=16):
     CBC.__init__(self, AES(key, noPadding(), keySize), padding)
     self.name = 'AES_CBC'
Ejemplo n.º 17
0
 def decrypt_func(data):
     cipher = AES_CBC(encryption_key, padding=noPadding(),
         keySize=len(encryption_key))
     return cipher.decrypt(iv + data)
Ejemplo n.º 18
0
 def __init__(self, key=None, padding=padWithPadLen(), keySize=16):
     CBC.__init__( self, AES(key, noPadding(), keySize), padding)
     self.name       = 'AES_CBC'
Ejemplo n.º 19
0
 def __init__(self, seed=defaultSeed):
     self.__algorithm = Rijndael(padding=noPadding(),keySize=32, blockSize=32)
     self.reset()
     self.reseed(seed)
Ejemplo n.º 20
0
 def decrypt_func(data):
     cipher = AES_CBC(encryption_key,
                      padding=noPadding(),
                      keySize=len(encryption_key))
     return cipher.decrypt(iv + data)
Ejemplo n.º 21
0
 def __init__(self, seed=defaultSeed):
     self.__algorithm = Rijndael(padding=noPadding(),
                                 keySize=32,
                                 blockSize=32)
     self.reset()
     self.reseed(seed)