Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- def hex_to_text(hex_str):
- """
- Convert a hex string to text, replacing non-printable characters with '*'.
- """
- text = ''
- for i in range(0, len(hex_str), 2):
- try:
- byte = int(hex_str[i:i+2], 16)
- if chr(byte) in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-{}:.,;!?() ":
- text += chr(byte)
- else:
- text += '*'
- except ValueError:
- text += '*'
- return text
- def xor_column(cursor_x, cursor_y, new_char, lines, file_name):
- # Find the character in the position x, y in the original file
- try:
- with open(file_name, 'r') as file:
- orig_hexlines = file.readlines()
- except FileNotFoundError:
- print("File not found.")
- return
- #matrix of the original file containing the numerical values of the characters
- orig_lines = [[int(hex_str[i:i+2], 16) for i in range(0, len(hex_str), 2)] for hex_str in orig_hexlines]
- original_char = chr(orig_lines[cursor_y][cursor_x])
- # XOR it with the new character to get the key
- key = ord(original_char) ^ ord(new_char)
- # XOR the key with the corresponding character in the other lines to get the plaintext
- for row in range(len(lines)):
- if row != cursor_y:
- char = lines[row][cursor_x]
- xor_char = chr(ord(char) ^ key)
- lines[row] = lines[row][:cursor_x] + xor_char + lines[row][cursor_x+1:]
- return lines
- def main(stdscr):
- curses.curs_set(1) # Set cursor to visible
- stdscr.clear()
- stdscr.refresh()
- file_name = "message.enc"
- try:
- with open(file_name, 'r') as file:
- hex_lines = file.readlines()
- except FileNotFoundError:
- print("File not found.")
- return
- lines = [hex_to_text(line.strip()) for line in hex_lines]
- cursor_y, cursor_x = 0, 0
- #enable keypad
- stdscr.keypad(True)
- while True:
- stdscr.clear()
- for i, line in enumerate(lines):
- if 0 <= i < curses.LINES:
- stdscr.addstr(i, 0, line)
- stdscr.move(cursor_y, cursor_x)
- stdscr.refresh()
- key = stdscr.getch()
- if key == 450:
- cursor_y = max(0, cursor_y - 1)
- elif key == 456:
- cursor_y = min(len(lines) - 1, cursor_y + 1)
- elif key == 452:
- cursor_x = max(0, cursor_x - 1)
- elif key == 454:
- cursor_x = min(len(lines[cursor_y]) - 1, cursor_x + 1)
- else:
- if chr(key) in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-{}:.,;!?() ":
- try:
- lines[cursor_y] = lines[cursor_y][:cursor_x] + chr(key) + lines[cursor_y][cursor_x+1:]
- lines = xor_column(cursor_x, cursor_y, chr(key), lines, file_name)
- cursor_x = min(len(lines[cursor_y]), cursor_x + 1)
- except ValueError:
- pass
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement