def recvFile(file_name: str, hash_code: str, encoding: str, fromer: str): mkdir(euser.get()) btext = b'' while True: data, addr = client_msg.recvfrom(buffer) btext += data if btext[-7:] == b"__end__": break btext = btext[:-7] hexstr = binascii.b2a_hex(btext) filebyte = cryptocommon.hexstr_to_bytelist(hexstr) hashbytelist = md5hash.hash(filebyte) while fromer not in certificate_list.keys(): # wait for certificate askCert(fromer) time.sleep(0.2) friend_certificate = certificate_list[fromer] friend_pbk = friend_certificate["pbk"] hash_decode = cipher_rsa.numlist_to_decryption_str(hash_code, friend_pbk[0], friend_pbk[1]) hashhexstr = cryptocommon.bytelist_to_hexstr(hashbytelist) if hashhexstr == hash_decode: file = open(euser.get() + "\\" + file_name, 'w') file.write((btext).decode('gb18030')) file.close() text.insert(tkinter.INSERT, "you have received a file from "+fromer+"\n") else: text.insert(tkinter.INSERT, "received some broken files.\n")
def connectServer(): global ck ipStr = eip.get() portStr = eport.get() userStr = euser.get() pwd = cryptocommon.bytelist_to_hexstr(md5hash.hash(cryptocommon.asciistr_to_bytelist(epwd.get()))) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: client.connect((ipStr, int(portStr))) # client.send((userStr + pwd).encode()) data = client.recv(1024).decode() print(data) if data == "error": messagebox.showerror("ERROR", "Wrong account or password, please try again.") raise Exception except Exception: return ck = client t1 = threading.Thread(target=tcp_msg) t1.start() t2 = threading.Thread(target=udp_msg) t2.start() t3 = threading.Thread(target=resend_certificate) t3.start() chatRoom()
def sendFile(): friend_certificate = certificate_list[comboxlist_friend.get()] friend_address = tuple(friend_certificate["address"]) file_path = filedialog.askopenfilename() file = open(file_path, 'rb') stext = file.read() hexstr = binascii.b2a_hex(stext) filebyte = cryptocommon.hexstr_to_bytelist(hexstr) hashbytelist = md5hash.hash(filebyte) hashhexstr = cryptocommon.bytelist_to_hexstr(hashbytelist) hashhexstr = cipher_rsa.str_encryption(hashhexstr, pvk[0], pvk[1]) # signature file_name = file_path.split("/")[-1] stream = {"type": "file", "name": file_name, "hash": hashhexstr, "encoding": chardet.detect(stext)["encoding"], "sender": euser.get()} client_msg.sendto(json.dumps(stream).encode(), friend_address) time.sleep(0.8) stext += b"__end__" epoch = len(stext) // buffer res = int(len(stext) - epoch * buffer) print("sending file::::") t = 0 for i in range(epoch): t = i time.sleep(0.1) client_msg.sendto(stext[i * buffer: (i + 1) * buffer], friend_address) if res != 0: client_msg.sendto(stext[t * buffer:t * buffer + res], friend_address)
def main(): message = "FF00CA9634" msgbytelist = cryptocommon.hexstr_to_bytelist(message) hashbytelist = md5hash.hash(msgbytelist, printdebug=True) hashhexstr = cryptocommon.bytelist_to_hexstr(hashbytelist) print('Message string (hex): {}'.format(message)) print("Message bytelist: {}".format(msgbytelist)) print("Hash bytelist: {}".format(hashbytelist)) print("MD5 hash (hex): {}".format(hashhexstr)) print("") print("-" * 100) print("") message = "the quick brown fox" msgbytelist = cryptocommon.asciistr_to_bytelist(message) hashbytelist = sha256hash.hash(msgbytelist, printdebug=True) hashhexstr = cryptocommon.bytelist_to_hexstr(hashbytelist) print('Message string: "{}"'.format(message)) print("Message bytelist: {}".format(msgbytelist)) print("Hash bytelist: {}".format(hashbytelist)) print("SHA-256 hash (hex): {}".format(hashhexstr))
def main(): message = "FF00CA9634" msgbytelist = cryptocommon.hexstr_to_bytelist(message) hashbytelist = md5hash.hash(msgbytelist, printdebug=True) hashhexstr = cryptocommon.bytelist_to_hexstr(hashbytelist) print(f"Message string (hex): {message}") print(f"Message bytelist: {msgbytelist}") print(f"Hash bytelist: {hashbytelist}") print(f"MD5 hash (hex): {hashhexstr}") print() print("-" * 100) print() message = "the quick brown fox" msgbytelist = cryptocommon.asciistr_to_bytelist(message) hashbytelist = sha256hash.hash(msgbytelist, printdebug=True) hashhexstr = cryptocommon.bytelist_to_hexstr(hashbytelist) print(f'Message string: "{message}"') print(f"Message bytelist: {msgbytelist}") print(f"Hash bytelist: {hashbytelist}") print(f"SHA-256 hash (hex): {hashhexstr}")
def main(): message = "3243F6A8885A308D313198A2E0370734" key = "2B7E151628AED2A6ABF7158809CF4F3C" plaintextbytelist = cryptocommon.hexstr_to_bytelist(message) keybytelist = cryptocommon.hexstr_to_bytelist(key) ciphertextbytelist = aescipher.encrypt(plaintextbytelist, keybytelist, printdebug=True) ciphertexthexstr = cryptocommon.bytelist_to_hexstr(ciphertextbytelist) assert aescipher.decrypt(ciphertextbytelist, keybytelist) == plaintextbytelist print('Plaintext (hex): {}'.format(message)) print('Ciphertext (hex): {}'.format(ciphertexthexstr)) print('Key (hex): {}'.format(key)) print("Plaintext (bytelist): {}".format(plaintextbytelist)) print("Ciphertext (bytelist): {}".format(ciphertextbytelist)) print("Key (bytelist): {}".format(keybytelist))
0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391, ] _ROTATION_AMOUNTS = [ 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21, ] if __name__ == "__main__": print( cryptocommon.bytelist_to_hexstr( hash(cryptocommon.asciistr_to_bytelist("111"))))