Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. import argparse
  2.  
  3.  
  4. parser = argparse.ArgumentParser()
  5. parser.add_argument("func", type=str, help="name of function")
  6. parser.add_argument("--cipher", type=str, choices=['caesar', 'vigener', 'vernom'], help="type of code", default="")
  7. parser.add_argument("--key", help="key of code - word(videner) or int(caesar)", default="")
  8. parser.add_argument("--model-file", dest="modelFile", type=str, help="need statistic", default="")
  9. parser.add_argument("--input-file", dest = "inputFile", type = str, required=False, default="", help="read from this")
  10. parser.add_argument("--output-file", dest="outputFile", type = str, required=False, default="", help="write to this")
  11. parser.add_argument("--text-file", dest="textFile", type=str, required=False, default="", help="train_text")
  12. args = parser.parse_args()
  13.  
  14. args = vars(parser.parse_args())
  15. textFile = args["textFile"]
  16. inputFile = args["inputFile"]
  17. cipher = args['cipher']
  18. key = args['key']
  19. outputFile = args['outputFile']
  20. modelFile = args['modelFile']
  21. func = args["func"]
  22. #
  23. # try:
  24. # f = globals()[args.func]
  25. # f(args.cipher, args.key, args.model, args.input, args.output, args.text)
  26. # except KeyError:
  27. # pass
  28.  
  29. def shifr(cipher, key, inputFile, outputFile):
  30. if (inputFile == ""):
  31. Str_all = input()
  32. else:
  33. file = open(inputFile, "r")
  34. Str_all = file.read()
  35. if cipher == "caesar":
  36. str_ans = cesar(key, Str_all)
  37. else:
  38. if cipher == "vigenere":
  39. str_ans = vigenere(key, Str_all)
  40. else:
  41. return "Name of cipher is not correct"
  42. if outputFile != "":
  43. f = open(outputFile, "w")
  44. f.write(str_ans)
  45. else:
  46. print(str_ans)
  47.  
  48. def cesar(key, Str):
  49. ans = [""]*len(Str)
  50. for i in range(len(Str)):
  51. ans[i] = Str[i]
  52. if ans[i].isalpha():
  53. if ans[i].isupper():
  54. ans[i] = BigChr[(ord(ans[i]) - ord('A') + key) % 26]
  55. else:
  56. ans[i] = smallChr[(ord(ans[i]) - ord('a') + key) % 26]
  57. return "".join(map(str, ans))
  58.  
  59.  
  60.  
  61. def vigenere(key, Str):
  62. ans = [""]*len(Str)
  63. for i in range(len(Str)):
  64. x = i % len(key)
  65. ans[i] = Str[i]
  66. if ans[i].isalpha():
  67. y = key[x].lower()
  68. if ans[i].isupper():
  69. ans[i] = BigChr[(ord(ans[i]) - ord('A') + ord(y) - ord('a')) % 26]
  70. else:
  71. ans[i] = smallChr[(ord(ans[i]) - ord('a') + ord(y) - ord('a')) % 26]
  72. return "".join(map(str, ans))
  73.  
  74. def deshifr(cipher, key, inputFile, outputFile):
  75. if (inputFile == ""):
  76. str_all = input()
  77. else:
  78. file = open(inputFile, "r")
  79. str_all = file.read()
  80. ans = ""
  81. if cipher == "caesar":
  82. new_key = -key
  83. ans = cesar(new_key, str_all)
  84. else:
  85. if cipher == "vigenere":
  86. new_key = ""
  87. for i in range(len(key)):
  88. if key[i].isupper():
  89. new_key += BigChr[25 - ord(key[i]) - ord('A')]
  90. else:
  91. new_key += smallChr[25 - ord(key[i]) - ord('a')]
  92. ans = vigenere(new_key, str_all)
  93. else:
  94. ans = "The name of shifr is not correct"
  95. if outputFile != "":
  96. file = open(outputFile, "w")
  97. file.write(ans)
  98. else:
  99. print(ans)
  100.  
  101. def train(text):
  102. w = [0] * 26
  103. cnt = [0] * 26
  104. cnt_all = 0
  105. for i in range(text):
  106. if text[i].isalpha():
  107. cnt_all += 1
  108. cnt[ord(text[i].lower()) - ord('a')] += 1
  109. for i in range(26):
  110. w[i] = cnt[i]/cnt_all
  111. return w
  112.  
  113. def hack(inputFile, outputFile, modelFile):
  114. if (inputFile != ""):
  115. file = open(inputFile, "r")
  116. str_all = file.read()
  117. else:
  118. str_all = input()
  119. model_file = open(modelFile, "r")
  120. model = list(model_file.read())
  121. new_model = ""
  122. for i in range(1, len(model) - 1):
  123. new_model += model[i]
  124. model = new_model.split(", ")
  125. min_disp = 27
  126. min_key = -1
  127. for key in range(26):
  128. str_try = cesar(26 - key, str_all)
  129. disp = train(str_try)
  130. curr_disp = 0
  131. for i in range(26):
  132. curr_disp += (disp[i] - model[i]) ** 2
  133. if (curr_disp < min_disp):
  134. min_disp = curr_disp
  135. min_key = key
  136. if outputFile != "":
  137. file = open(outputFile, "w")
  138. file.write(cesar(26-min_key, str_all))
  139. else:
  140. print(cesar(26-min_key, str_all))
  141.  
  142.  
  143. global BigChr
  144. global smallChr
  145. BigChr = [" "]*26
  146. smallChr = [" "]*26
  147. str_ans = ""
  148. for i in range(ord('A'), ord('Z') + 1):
  149. BigChr[i - ord('A')] = chr(i)
  150. for i in range(ord('a'), ord('z') + 1):
  151. smallChr[i - ord('a')] = chr(i)
  152. if func == "encode":
  153. shifr(cipher, key, inputFile, outputFile)
  154. if func == "decode":
  155. deshifr(cipher, key, inputFile, outputFile)
  156. if func == "train":
  157. file = open(textFile, "r")
  158. Text = file.read()
  159. mod = train(Text)
  160. file = open(modelFile, "w")
  161. file.write(mod)
  162. if func == "hack":
  163. hack(inputFile, outputFile, modelFile)
  164. # shifr("caesar", 5, "kimono")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement