Advertisement
nicoviale_

Untitled

Apr 24th, 2024
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. import curses
  2.  
  3. def hex_to_text(hex_str):
  4.     """
  5.    Convert a hex string to text, replacing non-printable characters with '*'.
  6.    """
  7.     text = ''
  8.     for i in range(0, len(hex_str), 2):
  9.         try:
  10.             byte = int(hex_str[i:i+2], 16)
  11.             if chr(byte) in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-{}:.,;!?() ":  
  12.                 text += chr(byte)
  13.             else:
  14.                 text += '*'
  15.         except ValueError:
  16.             text += '*'
  17.     return text
  18.  
  19. def xor_column(cursor_x, cursor_y, new_char, lines, file_name):
  20.     # Find the character in the position x, y in the original file
  21.     try:
  22.         with open(file_name, 'r') as file:
  23.             orig_hexlines = file.readlines()
  24.     except FileNotFoundError:
  25.         print("File not found.")
  26.         return
  27.    
  28.     #matrix of the original file containing the numerical values of the characters
  29.     orig_lines = [[int(hex_str[i:i+2], 16) for i in range(0, len(hex_str), 2)] for hex_str in orig_hexlines]
  30.     original_char = chr(orig_lines[cursor_y][cursor_x])
  31.  
  32.     # XOR it with the new character to get the key
  33.     key = ord(original_char) ^ ord(new_char)
  34.  
  35.     # XOR the key with the corresponding character in the other lines to get the plaintext
  36.     for row in range(len(lines)):
  37.         if row != cursor_y:
  38.             char = lines[row][cursor_x]
  39.             xor_char = chr(ord(char) ^ key)
  40.             lines[row] = lines[row][:cursor_x] + xor_char + lines[row][cursor_x+1:]
  41.  
  42.     return lines
  43.  
  44. def main(stdscr):
  45.     curses.curs_set(1)  # Set cursor to visible
  46.     stdscr.clear()
  47.     stdscr.refresh()
  48.  
  49.     file_name = "message.enc"
  50.     try:
  51.         with open(file_name, 'r') as file:
  52.             hex_lines = file.readlines()
  53.     except FileNotFoundError:
  54.         print("File not found.")
  55.         return
  56.  
  57.     lines = [hex_to_text(line.strip()) for line in hex_lines]
  58.  
  59.     cursor_y, cursor_x = 0, 0
  60.  
  61.     #enable keypad
  62.     stdscr.keypad(True)
  63.  
  64.     while True:
  65.         stdscr.clear()
  66.  
  67.         for i, line in enumerate(lines):
  68.             if 0 <= i < curses.LINES:
  69.                 stdscr.addstr(i, 0, line)
  70.         stdscr.move(cursor_y, cursor_x)
  71.         stdscr.refresh()
  72.  
  73.         key = stdscr.getch()
  74.        
  75.         if key == 450:
  76.             cursor_y = max(0, cursor_y - 1)
  77.         elif key == 456:
  78.             cursor_y = min(len(lines) - 1, cursor_y + 1)
  79.         elif key == 452:
  80.             cursor_x = max(0, cursor_x - 1)
  81.         elif key == 454:
  82.             cursor_x = min(len(lines[cursor_y]) - 1, cursor_x + 1)
  83.         else:
  84.             if chr(key) in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-{}:.,;!?() ":  
  85.                 try:
  86.                     lines[cursor_y] = lines[cursor_y][:cursor_x] + chr(key) + lines[cursor_y][cursor_x+1:]
  87.                     lines = xor_column(cursor_x, cursor_y, chr(key), lines, file_name)
  88.                     cursor_x = min(len(lines[cursor_y]), cursor_x + 1)
  89.  
  90.                 except ValueError:
  91.                     pass
  92.  
  93. curses.wrapper(main)
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement