def decrypt_des(input_path, output_path, key): key = des.DesKey(key.encode('utf8')) with open(input_path, 'rb') as input_file: contents = input_file.read() with open(output_path, 'wb') as output_file: output_file.write(key.decrypt(contents, padding=True))
def main(): # ecb key = des.DesKey(b'password') print('ECB') enc = key.encrypt(b'Hello, DES world') for b in enc: print(b) # cbc print('\nCBC') cbc = key.encrypt(b'Hello, DES world', b'initial!') for b in cbc: print(b) dec = key.decrypt(cbc, b'initial!') print('CBC decoded:', dec)
def handle_request(self, given_request: Request): """ Encrypt the message of the request to a byte string. :param given_request: a Request """ key = des.DesKey(given_request.key.encode('utf-8')) if not request.data_input: en_message = key.encrypt(given_request.result.encode('utf-8'), padding=True) else: en_message = key.encrypt(given_request.data_input.encode('utf-8'), padding=True) if not self.next_handler: print(en_message) return True given_request.result = en_message return self.next_handler.handle_request(given_request)
def handle_request(self, given_request: Request): """ Decrypt the message of given request to a string. :param given_request: a Request """ key = des.DesKey(request.key.encode('utf-8')) if not request.data_input: de_message = key.decrypt(ast.literal_eval(given_request.result), padding=True) else: de_message = key.decrypt(ast.literal_eval( given_request.data_input), padding=True) original_message = de_message.decode('utf-8') if not self.next_handler: print(original_message) return True given_request.result = original_message return self.next_handler.handle_request(given_request)
def handle_request(self, new_request): """ Decrypts the data in the request. :param new_request: a Request object :return: a tuple where the first element is a string that indicates successful handling of the request or not, or if there is a next handler, invokes the next handler to handle the request """ des_key = des.DesKey(b"%b" % new_request.key.encode('utf8')) if new_request.data_input: new_request.result = des_key.decrypt(ast.literal_eval( new_request.data_input), padding=True) else: with open(new_request.input_file, mode='rb') as file: data = file.read() new_request.result = des_key.decrypt(data, padding=True) if not self._next_handler: return "", True return self._next_handler.handle_request(new_request)
def handle_request(self, request: Request) -> (str, bool): """ Final handler in the chain. decrypts message and outputs. :param request: the response object :return: tuple(str, bool) Where str is an error message and bool indicates success or failure. """ key_OBJ = des.DesKey(bytes(request.key, 'utf-8')) message = request.data_input # message_as_bytes = ast.literal_eval(response.data_input) request.result = key_OBJ.decrypt(message, padding=True) if request.output: if request.output == 'print': print(request.result) else: with open(request.output, 'w+') as outfile: outfile.write(request.result.decode()) return "Succesfully Decrypted", True
def handle_request(self, request: Request) -> (str, bool): """ Last handler in the chain, Encrypts the message and outputs accordingly. :param request: the response object :return: tuple(str, bool) Where str is an error message and bool indicates success or failure. """ key_OBJ = des.DesKey(bytes(request.key, 'utf-8')) request.result = key_OBJ.encrypt(bytes(request.data_input, 'utf-8'), padding=True) return_string = f"Encrypted message: {request.result}" if request.output == 'print': print(return_string) else: if request.output: with open(request.output, 'wb') as outfile: outfile.write(request.result) return "Succesfully Encrypted", True
def decrypt(self, key, content): byte_key = key.encode('utf-8') des_key = des.DesKey(byte_key) content = ast.literal_eval(content) return des_key.decrypt(content, padding=True)
def encrypt(self, key, content): byte_key = key.encode('utf-8') byte_content = content.encode('utf-8') des_key = des.DesKey(byte_key) return des_key.encrypt(byte_content, padding=True)