def _decrypt(self, stream): """ Build the master key from header settings and key-hash list. Start reading from `stream` after the header and decrypt all the data. Remove padding as needed and feed into hashed block reader, set as in-buffer. """ super(KDB4File, self)._decrypt(stream) ciphername = self.header.ciphers.get(self.header.CipherID, self.header.CipherID) if ciphername == 'AES': data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) elif ciphername == 'Twofish': data = twofish_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) else: raise IOError('Unsupported decryption type: %s' % codecs.encode(ciphername, 'hex')) length = len(self.header.StreamStartBytes) if self.header.StreamStartBytes == data[:length]: # skip startbytes and wrap data in a hashed block io self.in_buffer = HashedBlockIO(initial_bytes=data[length:]) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')
def _decrypt(self, stream): super(KDB3File, self)._decrypt(stream) if self.header.encryption_flags[self.header.Flags-1] == 'AES': data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) elif self.header.encryption_flags[self.header.Flags-1] == 'Twofish': data = twofish_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) else: raise IOError('Unsupported encryption type: %s'%self.header.encryption_flags.get(self.header['Flags']-1, self.header['Flags']-1)) if self.header.ContentHash == sha256(data): # put data in bytes io self.in_buffer = io.BytesIO(data) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')
def _decrypt(self, stream): super(KDB3File, self)._decrypt(stream) data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) if self.header.ContentHash == sha256(data): # put data in bytes io self.in_buffer = io.BytesIO(data) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')
def _decrypt(self, stream): """ Build the master key from header settings and key-hash list. Start reading from `stream` after the header and decrypt all the data. Remove padding as needed and feed into hashed block reader, set as in-buffer. """ super(KDB4File, self)._decrypt(stream) data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) length = len(self.header.StreamStartBytes) if self.header.StreamStartBytes == data[:length]: # skip startbytes and wrap data in a hashed block io self.in_buffer = HashedBlockIO(initial_bytes=data[length:]) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')
def _decrypt(self, stream): """ Build the master key from header settings and key-hash list. Start reading from `stream` after the header and decrypt all the data. Remove padding as needed and feed into hashed block reader, set as in-buffer. """ super(KDB4File, self)._decrypt(stream) data = aes_cbc_decrypt(stream.read(), self.master_key, self.header.EncryptionIV) data = unpad(data) length = len(self.header.StreamStartBytes) if self.header.StreamStartBytes == data[:length]: # skip startbytes and wrap data in a hashed block io self.in_buffer = HashedBlockIO(bytes=data[length:]) # set successful decryption flag self.opened = True else: raise IOError('Master key invalid.')
def decrypt(self, encrypted_message): encrypted_message = base64.b64decode(encrypted_message) message = libkeepass_crypto.aes_cbc_decrypt(encrypted_message, self.key, self.iv) return libkeepass_crypto.unpad(message)