def test(): assert xor([1, 1, 0, 1], [1, 0, 0, 1]) == [0, 1, 0, 0] assert get_residue([0, 1, 1, 1, 0, 0, 0], [1, 0, 1, 1]) == [0, 0, 1, 0] data = [0, 1, 1, 1] encoded = encode(data) assert encoded == [0, 1, 1, 1, 0, 1, 0] assert encode([1, 1, 1]) == [0, 1, 1, 1, 0, 1, 0] decoded, err = decode(encoded) assert (decoded, err) == (data, False)
def main(): print( "Welcome to Hu-Tucker archiver.\nDeveloped by Alex Borzikov aka Borlehandro." ) mode = input("Enter \"code\" to encoding or \"decode\" to decoding\n") if mode == "code": in_filename = input("Enter input filename:") out_filename = input("Enter output filename (without extension):") encode(in_filename, out_filename) elif mode == "decode": in_filename = input("Enter input filename (with .huta extension):") out_filename = input( "Enter output filename (with original file extension):") decode(in_filename, out_filename)
def modify(item): """Modify the existed target's username and password pair""" if len(argv) == 4: if box.has_key(item) == True: pw = argv[3] code_pw = code.encode(pw) new_line = '%s:%s\n' % (item, code_pw) # The most explict way to modify the line, is read out then write back, # a little more operations, but clear enough f = open("data", "r") lines = f.readlines() f.close() f = open("data", "w") for line in lines: if item in line: f.write(new_line) else: f.write(line) f.close() # for line in fileinput.input("data", inplace=True): # if item in line: # print "%s" % new_line #redirect the stdout to the file, so print works # else: # print "%s" % line else: print "The target '%s' not exist." % item exit(1) else: print "Incorrect command:\t", print " ".join([str(x) for x in argv]) exit(1)
def dot_mark(give_name, recv_name): code_key = '23333333' plain = recv_name cipher = code.encode(plain, code_key) text = '{} -> {}'.format(give_name, cipher) filename = 'Xmas_random.txt' with open(filename, 'a+', encoding='utf-8', newline='\n') as f: f.write(text + '\n')
def test__encode(self): for i, (tokens, exp_graph, exp_table) in enumerate([ ([], {}, {}), (TOKENS, GRAPH, TABLE), ]): with self.subTest(i=i, params=(tokens, exp_graph, exp_table)): graph, table = encode(tokens) assert_that(graph, 'encode').is_equal_to(exp_graph) assert_that(table, 'encode').is_equal_to(exp_table)
def jiami_des(line): line = encode(line) # 如果长度不等于16的倍数,那么在前面填充0,使其到达16的倍数 if len(line) % 16 != 0: print(len(line)) line = line.zfill(16 * (len(line) / 16 + 1)) print(">>") print(len(line)) c = desencode(line, '0f1571c947') return c
def add(item): """Add a new target to the data file, do encrpyt automatically""" # there is no argc in python, get the number of argc by len(argv) if len(argv) == 4: pw = argv[3] code_pw = code.encode(pw) line = "%s:%s\n" % (item, code_pw) #do I need f.close()? - no, will be auto closed ASAP the file object is garbage collected with open("data", "a") as f: # f.write("%s\n" % line) # Risk here, the '\' may be misunderstand, use %r contain the '' f.write(line) # not require format is better else: print "Incorrect command:\t", print " ".join([str(x) for x in argv])
def dot_mark(plain): key = '23333333' cipher = code.encode(plain, key) print('Encode successfully!') return cipher
def test_alphabet(self): self.assertEqual(alphabet_numbers, code.encode(ascii_lowercase))