def _encrypt_packet(self, toserver_pkt, iv, mode, password): if mode == 1: cycle = [iv] if password: cycle = [iv, password] for key in cycle: toserver_pkt = ''.join([ chr(p ^ i) for p, i in itertools.izip( itertools.imap(ord, toserver_pkt), itertools.imap(ord, itertools.cycle(key))) ]) elif mode == 16: import mcrypt m = mcrypt.MCRYPT('rijndael-256', 'cfb') iv_size = m.get_iv_size() key_size = m.get_key_size() key = ['\0'] * key_size key[0:len(password)] = password m.init(''.join(key), iv[:iv_size]) toserver_pkt = ''.join([m.encrypt(x) for x in toserver_pkt]) elif mode == 3: password += '\0' * (24 - len(password)) iv_size = 8 if len(iv) >= Crypto.Cipher.DES3.block_size: iv = iv[:iv_size] else: iv += self.random_pool.get_bytes(iv_size - iv) myDes = Crypto.Cipher.DES3.new(password, Crypto.Cipher.DES3.MODE_CFB, iv) toserver_pkt = ''.join(myDes.encrypt(toserver_pkt)) #print "toserver_pkt: "+toserver_pkt else: print "no supported encryption_mode" return toserver_pkt