Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import string
- alphabet = string.ascii_lowercase
- alphabet_high = string.ascii_uppercase
- # Кол-во символов в тексте Шекспира
- total = 5458198
- arr_count_low = [0.04482505031880485, 0.008527173253883424, 0.012217951785552668, 0.024509737462803657,
- 0.07413087616096008, 0.01260544230898183, 0.010449419387131065, 0.04001430508750324,
- 0.03630941933583208, 0.0004968672811063285, 0.00535194948955681, 0.026778251723370974,
- 0.017511273867309322, 0.03955957625575327, 0.05155382783841847, 0.008523875462194666,
- 0.00044043840109867763, 0.03827160539064358, 0.03938625898144406, 0.05312650805265767,
- 0.021035880339994994, 0.006227146761623525, 0.013354957075576959, 0.0008588915242722964,
- 0.015622555282897396, 0.00020134850366366336]
- arr_count_high = [0.008150308948118043, 0.002823825738824425, 0.0039384793296249054, 0.0028732926141558073,
- 0.007801659082356484, 0.002145946336135113, 0.0020453636896279686, 0.003382435008770294,
- 0.010224253499048586, 0.00037869641225913756, 0.0011351731835305351, 0.004371039672800437,
- 0.0029079194268877753, 0.005008612732627142, 0.006084242455110643, 0.0021873519428939735,
- 0.00021582214496432706, 0.005307612512407941, 0.006231177395909786, 0.0072917838451444965,
- 0.0025885832650262963, 0.0006558941247642537, 0.003022242872098081, 0.00011102565352154686,
- 0.0016670336986675822, 9.74680654677606e-05]
- def pos_in_array(el):
- key = 65 # ord('A')
- if el == el.lower():
- key = 97 # ord('a')
- pos_elem = ord(el) - key
- return pos_elem
- def caesar_decode(el, val):
- if el == el.lower():
- pos_elem = ord(el) - 97
- else:
- pos_elem = ord(el) - 65
- real_pos_elem = (pos_elem - val + 26 * 2) % 26
- if el == el.lower():
- return alphabet[real_pos_elem]
- else:
- return alphabet_high[real_pos_elem]
- def caesar_encode(el, val):
- if el == el.lower():
- pos_elem = ord(el) - 97
- else:
- pos_elem = ord(el) - 65
- real_pos_elem = (pos_elem + val) % 26
- if el == el.lower():
- return alphabet[real_pos_elem]
- else:
- return alphabet_high[real_pos_elem]
- def decoder_caesar(key: int, input: str, output: str):
- input_file = open(input, "r")
- output_file = open(output, "w")
- for line in input_file:
- new_line = []
- for elem in line:
- if elem in alphabet or elem in alphabet_high:
- new_line.append(caesar_decode(elem, key))
- else:
- new_line.append(elem)
- output_file.write("".join(str(x) for x in new_line))
- def decoder_vigenere(word: str, input: str, output: str):
- input_file = open(input, "r")
- output_file = open(output, "w")
- for line in input_file:
- new_line = []
- for i in range(len(line)):
- new_elem = (pos_in_array(line[i]) - pos_in_array(word[i % len(word)]) + 26 * 10) % 26
- if line[i] in alphabet or line[i] in alphabet_high:
- new_line.append(chr(new_elem + 97) if line[i] in alphabet else chr(new_elem + 65))
- else:
- new_line.append(line[i])
- output_file.write("".join(str(x) for x in new_line))
- def decoder(cipher: str, key: str, input: str, output: str):
- if cipher == "caesar":
- key = int(key)
- decoder_caesar(key, input, output)
- if cipher == "vigenere":
- decoder_vigenere(key, input, output)
- def encoder_caesar(key: int, input: str, output: str):
- input_file = open(input, "r")
- output_file = open(output, "w")
- for line in input_file:
- new_line = []
- for elem in line:
- if elem in alphabet or elem in alphabet_high:
- new_line.append(caesar_encode(elem, key))
- else:
- new_line.append(elem)
- output_file.write("".join(str(x) for x in new_line))
- def encoder_vigenere(word: str, input: str, output: str):
- input_file = open(input, "r")
- output_file = open(output, "w")
- for line in input_file:
- new_line = []
- for i in range(len(line)):
- new_elem = (pos_in_array(line[i]) + pos_in_array(word[i % len(word)]) + 26 * 10) % 26
- if line[i] in alphabet or line[i] in alphabet_high:
- new_line.append(chr(new_elem + 97) if line[i] in alphabet else chr(new_elem + 65))
- else:
- new_line.append(line[i])
- output_file.write("".join(str(x) for x in new_line))
- def encoder(cipher: str, key: str, input: str, output: str):
- if cipher == "caesar":
- key = int(key)
- encoder_caesar(key, input, output)
- if cipher == "vigenere":
- encoder_vigenere(key, input, output)
- def hacker(input: str, output: str):
- input_file = open(input, "r")
- output_file = open(output, "w")
- arr = []
- for line in input_file:
- arr.append(line)
- min_diff = float('inf')
- ans_key = 0
- for key in range(0, 26):
- count_low_letters = [0] * 26
- count_high_letters = [0] * 26
- file_total = 0
- for line in arr:
- for elem in line:
- file_total += 1
- new_elem = caesar_decode(elem, key)
- pos = pos_in_array(new_elem)
- if elem in alphabet:
- count_low_letters[pos] += 1
- if elem in alphabet_high:
- count_high_letters[pos] += 1
- for i in range(26):
- count_low_letters[i] = count_low_letters[i] / file_total
- count_high_letters[i] = count_high_letters[i] / file_total
- curr_diff = 0
- for i in range(26):
- curr_diff += (arr_count_low[i] - count_low_letters[i]) ** 2
- curr_diff += (arr_count_high[i] - count_high_letters[i]) ** 2
- if curr_diff < min_diff:
- min_diff = curr_diff
- ans_key = key
- print(ans_key)
- for line in arr:
- new_line = []
- for elem in line:
- if elem in alphabet or elem in alphabet_high:
- new_line.append(caesar_decode(elem, ans_key))
- else:
- new_line.append(elem)
- output_file.write("".join(str(x) for x in new_line))
- parser = argparse.ArgumentParser()
- parser.add_argument("action", type=str, help="Decode/Encode/Hack")
- parser.add_argument("--cipher", type=str, help="caesar/vigenere")
- parser.add_argument("--key", type=str, help="None if Hacker run")
- parser.add_argument("--input_file", type=str)
- parser.add_argument("--output_file", type=str)
- args = parser.parse_args()
- if args.action == "decode":
- decoder(args.cipher, args.key, args.input_file, args.output_file)
- elif args.action == "encode":
- encoder(args.cipher, args.key, args.input_file, args.output_file)
- else:
- hacker(args.input_file, args.output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement