def _get_encryption_object(self, key): if self.type == "aes": return AES.new(key, AES.MODE_ECB) elif self.type == "serpent": return Serpent.new(key, Serpent.MODE_ECB) elif self.type == "twofish": return TwoFish.new(key, TwoFish.MODE_ECB) else: return False
def _get_encryption_object(self,key): if self.type == "aes": return AES.new(key, AES.MODE_ECB) elif self.type == "serpent": return Serpent.new(key, Serpent.MODE_ECB) elif self.type == "twofish": return TwoFish.new(key, TwoFish.MODE_ECB) else: return False
if cip <> cipher.encrypt(msg): print 'ERROR! for TDES2/3 in %i'%i if msg <> cipher.decrypt(cip): print 'DECRYPTION ERROR! for DES in %i'%i # Serpent128/192/256 print "Serpent" from CryptoPlus.Cipher import python_Serpent for d in dict_serpent128,dict_serpent192,dict_serpent256: for i in range(0,len(d)/3): msg = d['msg%i'%i].decode('hex') key = d['key%i'%i].decode('hex') cip = d['cip%i'%i].decode('hex') cipher = python_Serpent.new(key,python_Serpent.MODE_ECB) if cip <> cipher.encrypt(msg): print 'ERROR! for Serpent in %i'%i if msg <> cipher.decrypt(cip): print 'DECRYPTION ERROR! for Serpent in %i'%i # CMAC-AES128/192/256 print "CMAC-AES" from CryptoPlus.Cipher import python_AES for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256: for i in range(0,len(d)/4): msg = d['msg%i'%i].decode('hex') key = d['key%i'%i].decode('hex') if msg == '\x00':
from CryptoPlus.Cipher import python_Serpent import time key = '000102030405060708090A0B0C0D0E0F'.decode('hex') IV = '00000000000000000000000000000000'.decode('hex') plaintext = ('33B3DC87EDDD9B0F6A1F407D14919365' * 32 * 1000).decode('hex') cipher_init_time = time.time() cipher = python_Serpent.new(key, python_Serpent.MODE_CBC, IV) cipher_text = cipher.encrypt(plaintext) cipher_finish_time = time.time() cipher_time = cipher_finish_time - cipher_init_time print '\nTiempo de cifrado -> ' + str(cipher_time) decipher_init_time = time.time() decipher = python_Serpent.new(key, python_Serpent.MODE_CBC, IV) decipher_text = decipher.decrypt(cipher_text).encode('hex').upper() decipher_finish_time = time.time() decipher_time = decipher_finish_time - decipher_init_time print 'Tiempo de descifrado -> ' + str(decipher_time)
def run_session(sess_key): cipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) decipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) while True: login = input("\nLogin: "******"Password: "******"Authentication failed:", auth_result) def get_command(): cmd, *args = input("\nEnter command: ").split() return cmd, args while True: command, args = get_command() if command == 'r': if len(args) == 0: print("You need to specify file name.") continue filename = args[0] send_msg(CMD.RECV, cipher) send_msg(filename, cipher) print("Sent filename to server.") result = recv_msg(decipher, True) if result == MSG.SUCCESS: text = recv_msg(decipher, True) print("Received text from server.") print(text) elif result == MSG.ERROR: error_text = recv_msg(decipher, True) print("Failed to receive text from server:", error_text) elif result == MSG.EXP_SESS: print("Session expired.") return True else: print("Invalid result.") elif command == 'q': send_msg(CMD.QUIT, cipher) print("Quit.") return False elif command == 's': if len(args) == 0: print("You need to specify file name.") continue filename = args[0] text = read_file(filename) send_msg(CMD.SEND, cipher) send_msg(text, cipher) print("Sent text to server.") send_msg(filename.split(sep='/')[-1], cipher) print("Sent filename to server.") result = recv_msg(decipher, True) if result == MSG.EXP_SESS: print("Session expired.") return True elif command == 'g': private_key = generate_new_key() print("Generated new private RSA key.") else: print("Unknown command.")
unpad = lambda s : s[0:-ord(s[-1])] encoded = open("../encrypted", "r").read() ha = SHA256.new() ha.update(encoded) check = ha.digest().encode("hex") if(check != "6b39ac2220e703a48b3de1e8365d9075297c0750e9e4302fc3492f98bdf3a0b0"): print "BAD INPUT" sys.exit() IV = '5353544943323031352d537461676533'.decode('hex') print IV obj = python_Serpent.new(key.decode('hex'), python_Serpent.MODE_CBC, IV) print "Trying" decoded = obj.decrypt(encoded)[:-2] print "Done" hashed = SHA256.new() hashed.update(decoded) res = hashed.digest().encode("hex") if(res == "7beabe40888fbbf3f8ff8f4ee826bb371c596dd0cebe0796d2dae9f9868dd2d2"): print "Yesss !" else: print "Failed " f = open("decrypted", "w") f.write(decoded)
def run_session(sess_key): cipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) decipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) while True: login = recv_msg(decipher) password = recv_msg(decipher) is_auth, user_dir = authenticate(login, password) if is_auth: send_msg(MSG.SUCCESS, cipher) break else: send_msg("invalid username/password", cipher) t1 = time.clock() while True: cmd = recv_msg(decipher, True) if cmd == CMD.RECV: t2 = time.clock() if t2 - t1 > SESSION_MAX_TIME: print("Session expired.") c.recv(4096) send_msg(MSG.EXP_SESS, cipher) return True filename = recv_msg(decipher, True) print("Received filename from client.") print(filename) path = os.path.join(user_dir, filename) text = read_file(path) if text is None: send_msg(MSG.ERROR, cipher) text = "No such file!" else: send_msg(MSG.SUCCESS, cipher) send_msg(text, cipher) print("Sent text to client.") elif cmd == CMD.SEND: t2 = time.clock() if t2 - t1 > SESSION_MAX_TIME: print("Session expired.") c.recv(4096) c.recv(4096) send_msg(MSG.EXP_SESS, cipher) return True text = recv_msg(decipher, True) print("Received text from client.") print(text) filename = recv_msg(decipher, True) print("Received filename from client.") print(filename) path = os.path.join(user_dir, filename) save_file(text, path) send_msg(MSG.SUCCESS, cipher) elif cmd == CMD.QUIT: return False else: print("Invalid cmd.")
def run_session(sess_key): nonlocal cipher, decipher cipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) decipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) while not login_event.is_set(): time.sleep(1) while not code_event.is_set(): time.sleep(1) login_event.clear() code_event.clear() while True: command, args = get_command() if command == 'r': if len(args) == 0: print("You need to specify file name.") continue filename = args[0] send_msg(CMD.RECV) send_msg(filename) print("Sent filename to server.") result = recv_msg(decode=True) if result == MSG.SUCCESS: text = recv_msg(decode=True) print("Received text from server.") print(text) elif result == MSG.ERROR: error_text = recv_msg(decode=True) print("Failed to receive text from server:", error_text) elif result == MSG.EXP_SESS: print("Session expired.") expired_event.set() return True else: print("Invalid result.") elif command == 'q': send_msg(CMD.QUIT) print("Quit.") return False elif command == 's': if len(args) == 0: print("You need to specify file name.") continue filename = args[0] text = read_file(filename) short_name = filename.split(sep='/')[-1] text = '\0'.join([short_name, text]) send_msg(CMD.SEND) send_msg(text) print("Sent text to server.") result = recv_msg(decode=True) if result == MSG.EXP_SESS: print("Session expired.") expired_event.set() return True elif command == 'g': private_key = generate_new_key() print("Generated new private RSA key.") else: print("Unknown command.")
if cip != cipher.encrypt(msg): print('ERROR! for TDES2/3 in %i'%i) if msg != cipher.decrypt(cip): print('DECRYPTION ERROR! for DES in %i'%i) # Serpent128/192/256 print("Serpent") from CryptoPlus.Cipher import python_Serpent for d in dict_serpent128,dict_serpent192,dict_serpent256: for i in range(0,len(d)//3): msg = codecs.decode(d['msg%i'%i], 'hex') key = codecs.decode(d['key%i'%i], 'hex') cip = codecs.decode(d['cip%i'%i], 'hex') cipher = python_Serpent.new(key,python_Serpent.MODE_ECB) if cip != cipher.encrypt(msg): print('ERROR! for Serpent in %i'%i) if msg != cipher.decrypt(cip): print('DECRYPTION ERROR! for Serpent in %i'%i) # CMAC-AES128/192/256 print("CMAC-AES") from CryptoPlus.Cipher import python_AES for d in dict_cmac_aes128,dict_cmac_aes192,dict_cmac_aes256: for i in range(0,len(d)//4): msg = codecs.decode(d['msg%i'%i], 'hex') key = codecs.decode(d['key%i'%i], 'hex') if msg == b'\x00':
def run_session(sess_key): cipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) decipher = serpent.new(key=sess_key, mode=serpent.MODE_CFB, segment_size=16) while True: login = recv_msg(decipher) password = recv_msg(decipher) is_auth, user_dir = authenticate(login, password) if is_auth: send_msg(MSG.SUCCESS, cipher) break else: send_msg("invalid username/password", cipher) totp = pyotp.TOTP('base32secret3232', interval=60) with open(os.path.join(user_dir, 'userinfo.json')) as f: data = json.load(f) chat_id = data['chat_id'] token = data['token'] code = send_code(totp, token, chat_id) while True: client_code = recv_msg(decipher, True) if totp.verify(client_code): send_msg(MSG.SUCCESS, cipher) break elif client_code == code: send_msg(MSG.CODE_EXPIRED, cipher) code = send_code(totp, token, chat_id) else: send_msg(MSG.CODE_INVALID, cipher) t1 = time.time() while True: cmd = recv_msg(decipher, True) t2 = time.time() if t2 - t1 > SESSION_MAX_TIME: print("Session expired.") c.recv(4096) send_msg(MSG.EXP_SESS, cipher) return True if cmd == CMD.RECV: filename = recv_msg(decipher, True) print("Received filename from client.") print(filename) path = os.path.join(user_dir, filename) text = read_file(path) if text is None: send_msg(MSG.ERROR, cipher) text = "No such file!" else: send_msg(MSG.SUCCESS, cipher) send_msg(text, cipher) print("Sent text to client.") elif cmd == CMD.SEND: text = recv_msg(decipher, True) print("Received text from client.") filename, text = text.split(sep='\0') print(filename) print(text) path = os.path.join(user_dir, filename) save_file(text, path) send_msg(MSG.SUCCESS, cipher) elif cmd == CMD.QUIT: return False else: print("Invalid cmd.")