def export_data(self, entrystore, password): "Exports data from an entrystore" # check and pad password if password is None: raise base.PasswordError password = util.pad_right(password[:32], 32, "\0") # generate XML data = RevelationXML.export_data(self, entrystore) # compress data, and right-pad with the repeated ascii # value of the pad length data = zlib.compress(data) padlen = 16 - (len(data) % 16) if padlen == 0: padlen = 16 data += chr(padlen) * padlen # generate an initial vector for the CBC encryption iv = util.random_string(16) # encrypt data AES.block_size = 16 AES.key_size = 32 data = AES.new(password, AES.MODE_CBC, iv).encrypt(data) # encrypt the iv, and prepend it to the data with a header data = self.__generate_header() + AES.new(password).encrypt(iv) + data return data
def export_data(self, entrystore, password): "Exports data from an entrystore" # check and pad password if password is None: raise base.PasswordError password = util.pad_right(password[:32], 32, "\0") # generate XML data = RevelationXML.export_data(self, entrystore) # compress data, and right-pad with the repeated ascii # value of the pad length data = zlib.compress(data.encode()) padlen = 16 - (len(data) % 16) if padlen == 0: padlen = 16 data += bytearray((padlen, )) * padlen # generate an initial vector for the CBC encryption iv = os.urandom(16) data = AES.new(password.encode("utf8"), AES.MODE_CBC, iv).encrypt(data) # encrypt the iv, and prepend it to the data with a header data = self.__generate_header().encode("utf8") + AES.new( password.encode("utf8"), AES.MODE_ECB).encrypt(iv) + data return data
def import_data(self, input, password): "Imports data into an entrystore" # check and pad password if password is None: raise base.PasswordError password = util.pad_right(password[:32], 32, "\0") # check the data self.check(input) dataversion = self.__parse_header(input[:12]) # handle only version 1 data files if dataversion != 1: raise base.VersionError # fetch and decrypt the initial vector for CBC decryption AES.block_size = 16 AES.key_size = 32 cipher = AES.new(password) iv = cipher.decrypt(input[12:28]) # decrypt the data input = input[28:] if len(input) % 16 != 0: raise base.FormatError cipher = AES.new(password, AES.MODE_CBC, iv) input = cipher.decrypt(input) # decompress data padlen = ord(input[-1]) for i in input[-padlen:]: if ord(i) != padlen: raise base.PasswordError input = zlib.decompress(input[0:-padlen]) # check and import data if input.strip()[:5] != "<?xml": raise base.PasswordError entrystore = RevelationXML.import_data(self, input) return entrystore