Beispiel #1
0
	def decrypt(self, strData=None, hexData=None):
		"""
		Decrypt the string data or hexa data string
		
		@param strData: data encrypted
		@type strData: string/none

		@param hexData: data in clear with hexa representation (example: bbaa01ffef)
		@type hexData: string/none

		@return: decrypted data
		@rtype: string
		"""
		if not len(self.key):
			raise Exception('No key defined')
		
		if strData is None and hexData is None:
			raise Exception('No data defined')

		# encrypt
		if strData is not None: 
			enc = data_str
			
		if hexData is not None: 
			data_int = common.decodeHex(hexData, 2)
			data_str = []
			for d in data_int: data_str.append( chr(d) )
			enc = ''.join(data_str)
		
		enc = base64.b64decode(enc)
		iv = enc[:AESCRYPTO.block_size]
		cipher = AESCRYPTO.new(self.getKey(strKey=True), AESCRYPTO.MODE_CBC, iv)
		cipher_ret = self.unpad(cipher.decrypt(enc[AESCRYPTO.block_size:]))
		
		return cipher_ret
Beispiel #2
0
    def decrypt(self, strData=None, hexData=None):
        """
		Decrypt the string data or hexa data string
		
		@param strData: data encrypted
		@type strData: string/none

		@param hexData: data in clear with hexa representation (example: bbaa01ffef)
		@type hexData: string/none

		@return: decrypted data
		@rtype: string
		"""
        if not len(self.key):
            raise Exception('No key defined')

        if strData is None and hexData is None:
            raise Exception('No data defined')

        if self.cipher is not None:
            raise Exception('Cipher not ready')

        # encrypt
        if strData is not None:
            cipher_ret = self.cipher.decrypt(strData)
        if hexData is not None:
            data_int = common.decodeHex(hexData, 2)
            data_str = []
            for d in data_int:
                data_str.append(chr(d))
            cipher_ret = self.cipher.decrypt("".join(data_str))

        return cipher_ret
Beispiel #3
0
    def encrypt(self, strData=None, hexData=None, hexdigit=False):
        """
		Encrypt the string data or hexa data string and return an encrypted string
		
		@param strData: data in clear
		@type strData: string/none

		@param hexData: data in clear with hexa representation (example: bbaa01ffef)
		@type hexData: string/none
		
		@param hexdigit: return hexadecimal representation (default=False) 
		@type hexdigit: boolean
		
		@return: encrypted data
		@rtype: string
		"""
        if not len(self.key):
            raise Exception('No key defined')

        if strData is None and hexData is None:
            raise Exception('No data defined')

        # encrypt str
        if strData is not None:
            raw = strData

        if hexData is not None:
            data_int = common.decodeHex(hexData, 2)
            data_str = []
            for d in data_int:
                data_str.append(chr(d))
            raw = ''.join(data_str)

        raw = self.pad(raw)
        iv = Random.new().read(AESCRYPTO.block_size)
        cipher = AESCRYPTO.new(self.getKey(strKey=True), AESCRYPTO.MODE_CBC,
                               iv)
        cipher_ret = base64.b64encode(iv + cipher.encrypt(raw))

        # convert to int
        ret_int = []
        for c in cipher_ret:
            ret_int.append(ord(c))

        # convert to hex or not ?
        ret = []
        if hexdigit:
            for d in ret_int:
                ret.append("%02x" % d)
        else:
            for d in ret_int:
                ret.append(chr(d))

        ret_str = ''.join(ret)
        return ret_str
Beispiel #4
0
    def encrypt(self, strData=None, hexData=None, hexdigit=False):
        """
		Encrypt the string data or hexa data string and return an encrypted string
		
		@param strData: data in clear
		@type strData: string/none

		@param hexData: data in clear with hexa representation (example: bbaa01ffef)
		@type hexData: string/none
		
		@param hexdigit: return hexadecimal representation (default=False) 
		@type hexdigit: boolean
		
		@return: encrypted data
		@rtype: string
		"""
        if not len(self.key):
            raise Exception('No key defined')

        if strData is None and hexData is None:
            raise Exception('No data defined')

        if self.cipher is not None:
            raise Exception('Cipher not ready')

        # encrypt
        if strData is not None:
            cipher_ret = self.cipher.encrypt(strData)
        if hexData is not None:
            data_int = common.decodeHex(hexData, 2)
            data_str = []
            for d in data_int:
                data_str.append(chr(d))
            cipher_ret = self.cipher.encrypt("".join(data_str))

        # convert to int
        ret_int = []
        for c in cipher_ret:
            ret_int.append(ord(c))

        # convert to hex or not ?
        ret = []
        if hexdigit:
            for d in ret_int:
                ret.append("%02x" % d)
        else:
            for d in ret_int:
                ret.append(chr(d))

        ret_str = ''.join(ret)
        return ret_str
Beispiel #5
0
    def encrypt(self, strData=None, hexData=None, hexdigit=False):
        """
		Encrypt the string data or hexa data string and return an encrypted string
		
		@param strData: data in clear
		@type strData: string/none

		@param hexData: data in clear with hexa representation (example: bbaa01ffef)
		@type hexData: string/none
		
		@param hexdigit: return hexadecimal representation (default=False) 
		@type hexdigit: boolean
		
		@return: encrypted data
		@rtype: string
		"""
        if not len(self.key):
            raise Exception('No key defined')

        if strData is None and hexData is None:
            raise Exception('No data defined')

        # initialize
        self.ksa()
        self.pgra()

        # convert data to a list of int
        data_int = []
        if strData is not None:
            for c in strData:
                data_int.append(ord(c))
        if hexData is not None:
            data_int = common.decodeHex(hexData, 2)

        # xor
        ret_int = []
        for a in xrange(len(data_int)):
            ret_int.append(data_int[a] ^ self.keyStream[a])

        ret = []
        if hexdigit:
            for d in ret_int:
                ret.append("%02x" % d)
        else:
            for d in ret_int:
                ret.append(chr(d))

        ret_str = ''.join(ret)
        return ret_str
Beispiel #6
0
    def __init__(self, hexKey=None, strKey=None):
        """
		Blowfish algorithm implementation
		"""
        self.p = None
        self.s0 = None
        self.s1 = None
        self.s2 = None
        self.s3 = None
        if hexKey is not None:
            self.setKey(common.decodeHex(hexKey, 2))
        if strKey is not None:
            key = []
            for c in strKey:
                key.append(ord(c))
            self.setKey(key)
Beispiel #7
0
    def setKey(self, hexKey=None, strKey=None):
        """
		Set the key string or hexadecimal representation passed in argument
		
		@param strKey: string key
		@type strKey: string/none
		
		@param hexKey: hexadecimal key
		@type hexKey: string/none
		"""
        if strKey is not None:
            for c in strKey:
                self.key.append(ord(c))
        elif hexKey is not None:
            self.key = common.decodeHex(hexKey, 2)
        else:
            raise Exception('No key defined')
Beispiel #8
0
    def encrypt(self, strData=None, hexData=None, hexdigit=False):
        """
		Encrypts a given string using the preprocessed key.

		@param strData: data string to decrypt
		@type strData: string/none
		
		@param hexData: a hexadecimal string (ex: AADEFFBB01)
		@type hexData: string/none

		@return: data encrypted
		@rtype: string
		"""
        if strData is None and hexData is None:
            raise Exception('No data defined')

        nBytes = self.encryptor.blockSize << 2

        # perform ECB encryption
        if self.mode == MODE_ECB:
            # normalize data to a list of 32bits number
            if strData is not None:
                aWords = common.decodeBytes(strData, 4)
            if hexData is not None:
                aWords = common.decodeHex(hexData, 8)

            if (len(aWords) % self.encryptor.blockSize):
                raise Exception(
                    "length of byte string must be a multiple of %s to encrypt."
                    % nBytes)

            e = self.encryptor.encrypt(aWords)  # e is a list of 32bits numbers
            r = common.encodeBytes(e)  # convert to a list of string

        # perform CBC encryption
        if self.mode == MODE_CBC:
            r = self.encryptor.encrypt(strData)  # return list of string

        str_encrypted = "".join(r)
        if hexdigit:
            return binascii.hexlify(str_encrypted)
        else:
            return str_encrypted
Beispiel #9
0
    def setKey(self, hexKey=None, strKey=None):
        """
		Set the key to intialize the encryption/decryption.

		@param hexKey: a hexadecimal string representing 32 to 448 bits (8 to 56 bytes)
		@type hexKey: string/none
	
		@param strKey: a key string representing 32 to 448 bits (8 to 56 bytes)
		@type strKey: string/none
		"""
        if strKey is not None:
            for c in strKey:
                self.key.append(ord(c))
            self.blowfish.setKey(self.key)
        elif hexKey is not None:
            key = common.decodeHex(hexKey, 2)
            self.blowfish.setKey(key)
            self.key = key
        else:
            raise Exception('No key defined')
Beispiel #10
0
    def decrypt(self, strData=None, hexData=None, hexdigit=False):
        """
		Decrypts a given hexadecimal string using the preprocessed key.

		@param strData: data string to decrypt
		@type strData: string/none
		
		@param hexData: a hexadecimal string
		@type hexData: string/none

		@return: data decrypted
		@rtype: string
		"""
        if strData is None and hexData is None:
            raise Exception('No data defined')

        # perform ECB decryption
        if self.mode == MODE_ECB:
            nHexes = self.encryptor.blockSize << 3

            if strData is not None:
                aWords = common.decodeBytes(strData, 4)

            if hexData is not None:
                aWords = common.decodeHex(hexData, 8)

            if (len(aWords) % self.encryptor.blockSize):
                raise Exception(
                    "length of hexadecimal string must be a multiple of %s to decrypt."
                    % nHexes)

            # perform decryption
            d = self.encryptor.decrypt(aWords)
            rBytes = common.encodeBytes(d)

            return "".join(rBytes)

        # perform CBC decryption
        if self.mode == MODE_CBC:
            r = self.encryptor.decrypt(strData)
            return "".join(r)