Advertisement
informaticage

Encode decode file as bin

Feb 3rd, 2021
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. import sys
  2. import random
  3.  
  4. # Reads file located in the 'file_path' returning its content
  5. def read_file(file_path):
  6.     try:
  7.         file_wrapper = open(file_path, "rb")
  8.         file_content = list(file_wrapper.read())
  9.         file_wrapper.close()
  10.  
  11.         return file_content
  12.     except IOError as io_error:
  13.         print(f'Cant access file {file_path}', io_error)
  14.         # Returns empty bytearray
  15.         return []
  16.  
  17. # Writes content to file
  18. def write_file(file_path, content):
  19.     try:
  20.         file_wrapper = open(file_path, 'wb')
  21.         content_as_bytes = bytearray(content)
  22.         file_wrapper.write(content_as_bytes)
  23.         file_wrapper.close()
  24.  
  25.         return file_path
  26.     except IOError as io_error:
  27.         print(f'Cant access file {file_path}', io_error)
  28.         # Returns empty path
  29.         return ""
  30.    
  31. # Generates a key with length min(content_length, key_length_limit)
  32. def generate_key(content_length, key_length_limit):
  33.     key_length = min(content_length, key_length_limit)
  34.  
  35.     generated_key = [random.randint(0, 255) for b in range(key_length)]
  36.     return generated_key
  37.  
  38. # Applies a key to file content
  39. def encode_decode(file_content, encription_key):
  40.     content_length = len(file_content)
  41.     key_length = len(encription_key)
  42.  
  43.     encoded_content = []
  44.     encoded_byte_index = 0
  45.     while(encoded_byte_index < content_length):
  46.         encoded_content = [
  47.             byte ^ encription_key[encoded_byte_index % key_length] for byte in file_content
  48.         ]
  49.         encoded_byte_index += 1
  50.  
  51.     return encoded_content
  52.  
  53. def main(file_path):
  54.     # Check if file exist
  55.     file_content = read_file(file_path)
  56.     print('File content:', file_content)
  57.  
  58.     # Generates a random key with the specified max length
  59.     key_max_length = 5
  60.     generated_key = generate_key(len(file_content), key_max_length)
  61.     print('Key content', generated_key)
  62.  
  63.     # Encodes the content using the specified key
  64.     encoded_content = encode_decode(file_content, generated_key)
  65.     print('Encoded content', encoded_content)
  66.  
  67.     # Writed the encoded content to a file
  68.     encoded_file_path = write_file(file_path + '_encoded', encoded_content)
  69.  
  70.     # Reads the file just created back in memory
  71.     file_content = read_file(encoded_file_path)
  72.  
  73.     # Decodes the file to show the inversion of the encoding
  74.     decoded_content = encode_decode(file_content, generated_key)
  75.     print('Decoded content from file {encoded_file_path}', decoded_content)
  76.  
  77. if __name__ == "__main__":
  78.     # Parsing arguments
  79.     if(len(sys.argv) != 2):
  80.         error_message = f'Wrong arguments amount, usage: {sys.argv[0]} [filename.ext]'
  81.         print(error_message)
  82.  
  83.     # Passing argument filepath to main
  84.     main(sys.argv[1])
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement