Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument("func", type=str, help="name of function")
- parser.add_argument("--cipher", type=str, choices=['caesar', 'vigener', 'vernom'], help="type of code", default="")
- parser.add_argument("--key", help="key of code - word(videner) or int(caesar)", default="")
- parser.add_argument("--model-file", dest="modelFile", type=str, help="need statistic", default="")
- parser.add_argument("--input-file", dest = "inputFile", type = str, required=False, default="", help="read from this")
- parser.add_argument("--output-file", dest="outputFile", type = str, required=False, default="", help="write to this")
- parser.add_argument("--text-file", dest="textFile", type=str, required=False, default="", help="train_text")
- args = parser.parse_args()
- args = vars(parser.parse_args())
- textFile = args["textFile"]
- inputFile = args["inputFile"]
- cipher = args['cipher']
- key = args['key']
- outputFile = args['outputFile']
- modelFile = args['modelFile']
- func = args["func"]
- #
- # try:
- # f = globals()[args.func]
- # f(args.cipher, args.key, args.model, args.input, args.output, args.text)
- # except KeyError:
- # pass
- def shifr(cipher, key, inputFile, outputFile):
- if (inputFile == ""):
- Str_all = input()
- else:
- file = open(inputFile, "r")
- Str_all = file.read()
- if cipher == "caesar":
- str_ans = cesar(key, Str_all)
- else:
- if cipher == "vigenere":
- str_ans = vigenere(key, Str_all)
- else:
- return "Name of cipher is not correct"
- if outputFile != "":
- f = open(outputFile, "w")
- f.write(str_ans)
- else:
- print(str_ans)
- def cesar(key, Str):
- ans = [""]*len(Str)
- for i in range(len(Str)):
- ans[i] = Str[i]
- if ans[i].isalpha():
- if ans[i].isupper():
- ans[i] = BigChr[(ord(ans[i]) - ord('A') + key) % 26]
- else:
- ans[i] = smallChr[(ord(ans[i]) - ord('a') + key) % 26]
- return "".join(map(str, ans))
- def vigenere(key, Str):
- ans = [""]*len(Str)
- for i in range(len(Str)):
- x = i % len(key)
- ans[i] = Str[i]
- if ans[i].isalpha():
- y = key[x].lower()
- if ans[i].isupper():
- ans[i] = BigChr[(ord(ans[i]) - ord('A') + ord(y) - ord('a')) % 26]
- else:
- ans[i] = smallChr[(ord(ans[i]) - ord('a') + ord(y) - ord('a')) % 26]
- return "".join(map(str, ans))
- def deshifr(cipher, key, inputFile, outputFile):
- if (inputFile == ""):
- str_all = input()
- else:
- file = open(inputFile, "r")
- str_all = file.read()
- ans = ""
- if cipher == "caesar":
- new_key = -key
- ans = cesar(new_key, str_all)
- else:
- if cipher == "vigenere":
- new_key = ""
- for i in range(len(key)):
- if key[i].isupper():
- new_key += BigChr[25 - ord(key[i]) - ord('A')]
- else:
- new_key += smallChr[25 - ord(key[i]) - ord('a')]
- ans = vigenere(new_key, str_all)
- else:
- ans = "The name of shifr is not correct"
- if outputFile != "":
- file = open(outputFile, "w")
- file.write(ans)
- else:
- print(ans)
- def train(text):
- w = [0] * 26
- cnt = [0] * 26
- cnt_all = 0
- for i in range(text):
- if text[i].isalpha():
- cnt_all += 1
- cnt[ord(text[i].lower()) - ord('a')] += 1
- for i in range(26):
- w[i] = cnt[i]/cnt_all
- return w
- def hack(inputFile, outputFile, modelFile):
- if (inputFile != ""):
- file = open(inputFile, "r")
- str_all = file.read()
- else:
- str_all = input()
- model_file = open(modelFile, "r")
- model = list(model_file.read())
- new_model = ""
- for i in range(1, len(model) - 1):
- new_model += model[i]
- model = new_model.split(", ")
- min_disp = 27
- min_key = -1
- for key in range(26):
- str_try = cesar(26 - key, str_all)
- disp = train(str_try)
- curr_disp = 0
- for i in range(26):
- curr_disp += (disp[i] - model[i]) ** 2
- if (curr_disp < min_disp):
- min_disp = curr_disp
- min_key = key
- if outputFile != "":
- file = open(outputFile, "w")
- file.write(cesar(26-min_key, str_all))
- else:
- print(cesar(26-min_key, str_all))
- global BigChr
- global smallChr
- BigChr = [" "]*26
- smallChr = [" "]*26
- str_ans = ""
- for i in range(ord('A'), ord('Z') + 1):
- BigChr[i - ord('A')] = chr(i)
- for i in range(ord('a'), ord('z') + 1):
- smallChr[i - ord('a')] = chr(i)
- if func == "encode":
- shifr(cipher, key, inputFile, outputFile)
- if func == "decode":
- deshifr(cipher, key, inputFile, outputFile)
- if func == "train":
- file = open(textFile, "r")
- Text = file.read()
- mod = train(Text)
- file = open(modelFile, "w")
- file.write(mod)
- if func == "hack":
- hack(inputFile, outputFile, modelFile)
- # shifr("caesar", 5, "kimono")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement