Advertisement
KShah

Untitled

May 9th, 2022
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.01 KB | None | 0 0
  1. import argparse
  2. import string
  3.  
  4. alphabet = string.ascii_lowercase
  5. alphabet_high = string.ascii_uppercase
  6.  
  7. # Кол-во символов в тексте Шекспира
  8. total = 5458198
  9. arr_count_low = [0.04482505031880485, 0.008527173253883424, 0.012217951785552668, 0.024509737462803657,
  10.                  0.07413087616096008, 0.01260544230898183, 0.010449419387131065, 0.04001430508750324,
  11.                  0.03630941933583208, 0.0004968672811063285, 0.00535194948955681, 0.026778251723370974,
  12.                  0.017511273867309322, 0.03955957625575327, 0.05155382783841847, 0.008523875462194666,
  13.                  0.00044043840109867763, 0.03827160539064358, 0.03938625898144406, 0.05312650805265767,
  14.                  0.021035880339994994, 0.006227146761623525, 0.013354957075576959, 0.0008588915242722964,
  15.                  0.015622555282897396, 0.00020134850366366336]
  16. arr_count_high = [0.008150308948118043, 0.002823825738824425, 0.0039384793296249054, 0.0028732926141558073,
  17.                   0.007801659082356484, 0.002145946336135113, 0.0020453636896279686, 0.003382435008770294,
  18.                   0.010224253499048586, 0.00037869641225913756, 0.0011351731835305351, 0.004371039672800437,
  19.                   0.0029079194268877753, 0.005008612732627142, 0.006084242455110643, 0.0021873519428939735,
  20.                   0.00021582214496432706, 0.005307612512407941, 0.006231177395909786, 0.0072917838451444965,
  21.                   0.0025885832650262963, 0.0006558941247642537, 0.003022242872098081, 0.00011102565352154686,
  22.                   0.0016670336986675822, 9.74680654677606e-05]
  23.  
  24.  
  25. def pos_in_array(el):
  26.     key = 65  # ord('A')
  27.     if el == el.lower():
  28.         key = 97  # ord('a')
  29.  
  30.     pos_elem = ord(el) - key
  31.     return pos_elem
  32.  
  33.  
  34. def caesar_decode(el, val):
  35.     if el == el.lower():
  36.         pos_elem = ord(el) - 97
  37.     else:
  38.         pos_elem = ord(el) - 65
  39.  
  40.     real_pos_elem = (pos_elem - val + 26 * 2) % 26
  41.     if el == el.lower():
  42.         return alphabet[real_pos_elem]
  43.     else:
  44.         return alphabet_high[real_pos_elem]
  45.  
  46.  
  47. def caesar_encode(el, val):
  48.     if el == el.lower():
  49.         pos_elem = ord(el) - 97
  50.     else:
  51.         pos_elem = ord(el) - 65
  52.  
  53.     real_pos_elem = (pos_elem + val) % 26
  54.     if el == el.lower():
  55.         return alphabet[real_pos_elem]
  56.     else:
  57.         return alphabet_high[real_pos_elem]
  58.  
  59.  
  60. def decoder_caesar(key: int, input: str, output: str):
  61.     input_file = open(input, "r")
  62.     output_file = open(output, "w")
  63.  
  64.     for line in input_file:
  65.         new_line = []
  66.         for elem in line:
  67.             if elem in alphabet or elem in alphabet_high:
  68.                 new_line.append(caesar_decode(elem, key))
  69.             else:
  70.                 new_line.append(elem)
  71.  
  72.         output_file.write("".join(str(x) for x in new_line))
  73.  
  74.  
  75. def decoder_vigenere(word: str, input: str, output: str):
  76.     input_file = open(input, "r")
  77.     output_file = open(output, "w")
  78.  
  79.     for line in input_file:
  80.         new_line = []
  81.         for i in range(len(line)):
  82.             new_elem = (pos_in_array(line[i]) - pos_in_array(word[i % len(word)]) + 26 * 10) % 26
  83.             if line[i] in alphabet or line[i] in alphabet_high:
  84.                 new_line.append(chr(new_elem + 97) if line[i] in alphabet else chr(new_elem + 65))
  85.             else:
  86.                 new_line.append(line[i])
  87.  
  88.         output_file.write("".join(str(x) for x in new_line))
  89.  
  90.  
  91. def decoder(cipher: str, key: str, input: str, output: str):
  92.     if cipher == "caesar":
  93.         key = int(key)
  94.         decoder_caesar(key, input, output)
  95.  
  96.     if cipher == "vigenere":
  97.         decoder_vigenere(key, input, output)
  98.  
  99.  
  100. def encoder_caesar(key: int, input: str, output: str):
  101.     input_file = open(input, "r")
  102.     output_file = open(output, "w")
  103.  
  104.     for line in input_file:
  105.         new_line = []
  106.         for elem in line:
  107.             if elem in alphabet or elem in alphabet_high:
  108.                 new_line.append(caesar_encode(elem, key))
  109.             else:
  110.                 new_line.append(elem)
  111.  
  112.         output_file.write("".join(str(x) for x in new_line))
  113.  
  114.  
  115. def encoder_vigenere(word: str, input: str, output: str):
  116.     input_file = open(input, "r")
  117.     output_file = open(output, "w")
  118.  
  119.     for line in input_file:
  120.         new_line = []
  121.         for i in range(len(line)):
  122.             new_elem = (pos_in_array(line[i]) + pos_in_array(word[i % len(word)]) + 26 * 10) % 26
  123.             if line[i] in alphabet or line[i] in alphabet_high:
  124.                 new_line.append(chr(new_elem + 97) if line[i] in alphabet else chr(new_elem + 65))
  125.             else:
  126.                 new_line.append(line[i])
  127.  
  128.         output_file.write("".join(str(x) for x in new_line))
  129.  
  130.  
  131. def encoder(cipher: str, key: str, input: str, output: str):
  132.     if cipher == "caesar":
  133.         key = int(key)
  134.         encoder_caesar(key, input, output)
  135.  
  136.     if cipher == "vigenere":
  137.         encoder_vigenere(key, input, output)
  138.  
  139.  
  140. def hacker(input: str, output: str):
  141.     input_file = open(input, "r")
  142.     output_file = open(output, "w")
  143.  
  144.     arr = []
  145.     for line in input_file:
  146.         arr.append(line)
  147.  
  148.     min_diff = float('inf')
  149.     ans_key = 0
  150.  
  151.     for key in range(0, 26):
  152.         count_low_letters = [0] * 26
  153.         count_high_letters = [0] * 26
  154.  
  155.         file_total = 0
  156.  
  157.  
  158.         for line in arr:
  159.             for elem in line:
  160.                 file_total += 1
  161.                 new_elem = caesar_decode(elem, key)
  162.                 pos = pos_in_array(new_elem)
  163.                 if elem in alphabet:
  164.                     count_low_letters[pos] += 1
  165.                 if elem in alphabet_high:
  166.                     count_high_letters[pos] += 1
  167.  
  168.         for i in range(26):
  169.             count_low_letters[i] = count_low_letters[i] / file_total
  170.             count_high_letters[i] = count_high_letters[i] / file_total
  171.  
  172.         curr_diff = 0
  173.         for i in range(26):
  174.             curr_diff += (arr_count_low[i] - count_low_letters[i]) ** 2
  175.             curr_diff += (arr_count_high[i] - count_high_letters[i]) ** 2
  176.  
  177.         if curr_diff < min_diff:
  178.             min_diff = curr_diff
  179.             ans_key = key
  180.  
  181.     print(ans_key)
  182.     for line in arr:
  183.         new_line = []
  184.         for elem in line:
  185.             if elem in alphabet or elem in alphabet_high:
  186.                 new_line.append(caesar_decode(elem, ans_key))
  187.             else:
  188.                 new_line.append(elem)
  189.  
  190.         output_file.write("".join(str(x) for x in new_line))
  191.  
  192.  
  193. parser = argparse.ArgumentParser()
  194. parser.add_argument("action", type=str, help="Decode/Encode/Hack")
  195. parser.add_argument("--cipher", type=str, help="caesar/vigenere")
  196. parser.add_argument("--key", type=str, help="None if Hacker run")
  197. parser.add_argument("--input_file", type=str)
  198. parser.add_argument("--output_file", type=str)
  199.  
  200. args = parser.parse_args()
  201.  
  202. if args.action == "decode":
  203.     decoder(args.cipher, args.key, args.input_file, args.output_file)
  204. elif args.action == "encode":
  205.     encoder(args.cipher, args.key, args.input_file, args.output_file)
  206. else:
  207.     hacker(args.input_file, args.output_file)
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement