def caesar_hack(text, model_file): my_stat = get_stat(text) with open(model_file, "r") as f: true_stat = json.load(f) best_k = 0 best_distance = -1 for k in range(1, alphabet_size): shifted_stat = shift(my_stat, k) distance = find_model_distance(shifted_stat, true_stat) if distance < best_distance or best_distance == -1: best_distance = distance best_k = k return cipher.decode('caesar', best_k, text)
def refresh_token(self): # read the token from conf file conf_name = '/etc/config/cloudconnector/%s/cloudconnector.0.conf' % qpkg_name with open(conf_name) as fp: conf_json = json.load(fp) encrypted_token = urllib.unquote(conf_json['account_dict'][self.account_id]['auth']).decode('utf8') if qpkg_name == 'HybridCloudSync': key = 'g8nCsbKAT5jbSopITKhKlKlUJdTxD49w' else: key = 'P7U2RZrMwTGsS0fq5jKAR9KUuldpLD7k' token = json.loads(cipher.decode(encrypted_token, key)) self.access_token = token['access_token'] return token['access_token']
def refresh_token(self, retry_count = 5): # read the token from conf file conf_name = '/etc/config/cloudconnector/%s/cloudconnector.0.conf' % qpkg_name with open(conf_name) as fp: conf_json = json.load(fp) encrypted_token = urllib.unquote(conf_json['account_dict'][self.account_id]['auth']).decode('utf8') if qpkg_name == 'CloudDriveSync': key = 'P7U2RZrMwTGsS0fq5jKAR9KUuldpLD7k' else: key = 'g8nCsbKAT5jbSopITKhKlKlUJdTxD49w' token = json.loads(cipher.decode(encrypted_token, key)) self.access_token = token['access_token'] self.headers = dict(Authorization='Bearer %s' % self.access_token) self.get_rooturl() return token['access_token']
def operate(operation=""): output = "" #Inputs,help,failsafes if operation == "": operation=input("What would you like to do?\n") while " - " not in operation: operation=input("INVALID, please enter the input like this: (coding-type) (encode/decode):(key) - (message)\n").lower() if operation in ["exit",""," ","no thanks","let me out"]: print("Exiting...") return elif operation in ["help","suggestions","options","explain","what"]: explanation = input("What do you need help on?\n") if explanation in ["coding-type","codingtype","codes","(coding-type)"]: print("You can do any of these as your coding type:\n unicode, ascii, shift, flip, or customAlphabet") elif explanation in ["encode","decode","encode/decode","(encode/decode)"]: print("Choose whether you want to encode a message or decode a message.") elif explanation in ["key","(key)"]: print("Whatever 'key' you want to use to encode or decode your message.") elif explanation in ["message","(message)"]: print("Whatever message you want to encode or decode.") else: print("Sorry I didn't understand") operation = input("What would you like to do?\n") #Breaking up 'operation' into operation and message operation = operation.split(" - ") message = " - ".join(operation[1:]) #Breaking up 'operation' into different parts operation = operation[0].split(" ") encoder = operation[0] if ":" in operation[1]: #if key operation = operation[1].split(":") key = operation[1] operation = operation[0] haskey = True else: #no key operation = operation[1] haskey = False #Running the different ciphers if operation == "encode": #all encode operations if haskey: #all ciphers that have a key if encoder == "unicode": output = cipher.encode(message,int(key)) elif encoder == "shift": output = cipher.shift(message,int(key)) elif encoder == "customAlphabet": output = cipher.customAlph(message,key) else: #all ciphers that dont have a key if encoder == "ascii": output = cipher.asciiNums(message) elif encoder == "flip": output = cipher.flip(message) elif operation == "decode": #all encode operations if haskey: #all ciphers that have a key if encoder == "unicode": if key == "crack" or key == "break": output = cipher.crack(message) else: output = cipher.decode(message,-int(key)) elif encoder == "shift": if key == "crack" or key == "break": output = cipher.shiftbreak(message) else: output = cipher.shift(message,-int(key)) elif encoder == "customAlphabet": output = cipher.customAlph(message,key) else: #all ciphers that dont have a key if encoder == "ascii": output = cipher.asciiMessage(message) elif encoder == "flip": output = cipher.flip(message) else: print("INVALID Operation") #Output print("Your message is:\n" + output) return
import cipher import file_work import trainer import argparser args = argparser.parse_arguments() if args.func == 'encode': text = file_work.read_text(args.input_file) result = cipher.encode(args.cipher, int(args.key), text) file_work.write_text(result, args.output_file) elif args.func == 'decode': text = file_work.read_text(args.input_file) result = cipher.decode(args.cipher, int(args.key), text) file_work.write_text(result, args.output_file) elif args.func == 'train': text = file_work.read_text(args.text_file) trainer.train(text, args.model_file) else: text = file_work.read_text(args.input_file) result = trainer.caesar_hack(text, args.model_file) file_work.write_text(result, args.output_file)
async def Decode(phrase: str, shift: int, username: str = Depends(user_auth)): return {"message": decode(phrase, -abs(shift))}
def test_decode_space(): assert decode("Ā", shift) == " "
def test_decode_none(): assert decode("", shift) == ""
def test_decode_phrase_with_special(): assert decode(shifted, -abs(shift)) == phrase