def toTxt(key,iv,seq,msg): assert(len(key) == 16) assert(len(iv) == 8) seq_txt = struct.pack('I',seq) pkl = cPickle.dumps(msg,cPickle.HIGHEST_PROTOCOL) txt = seq_txt + pkl.encode('zip') enc = xtea.crypt(key,txt,iv) result = enc.encode('base64').strip() return result
def toTxt(key, iv, seq, msg): assert (len(key) == 16) assert (len(iv) == 8) seq_txt = struct.pack('I', seq) pkl = cPickle.dumps(msg, cPickle.HIGHEST_PROTOCOL) txt = seq_txt + pkl.encode('zip') enc = xtea.crypt(key, txt, iv) result = enc.encode('base64').strip() return result
def fromTxt(key, iv, expected_seq, txt): seq_size = struct.calcsize('I') dec = xtea.crypt(key, txt.decode('base64'), iv) seq = struct.unpack('I', dec[:seq_size])[0] if (seq != expected_seq): raise MsgSequenceError(seq, expected_seq) pkl = dec[seq_size:].decode('zip') msg = cPickle.loads(pkl) return msg
def fromTxt(key,iv,expected_seq,txt): seq_size = struct.calcsize('I') dec = xtea.crypt(key,txt.decode('base64'),iv) seq = struct.unpack('I',dec[:seq_size])[0] if(seq != expected_seq): raise MsgSequenceError(seq,expected_seq) pkl = dec[seq_size:].decode('zip') msg = cPickle.loads(pkl) return msg
def toFlag(self,txt): txt = txt.strip() size = struct.calcsize("<IIId") if(len(txt) <= 3+size): raise FlagParseException(txt) if(txt.startswith("FLG") == False): raise FlagParseException(txt) try: enc = base64.urlsafe_b64decode(txt[3:]) dec = xtea.crypt(self.key,enc,self.iv) packed = dec[:size] sig = dec[size:] except Exception as e: raise FlagParseException(str(e)) if(sig != hmac.new(self.phrase,packed,hashlib.md5).digest()): raise FlagParseException(txt) teamId,serviceId,round,timestamp = struct.unpack("<IIId",packed) return Flag(teamId,serviceId,round,timestamp)
def toFlag(self, txt): txt = txt.strip() size = struct.calcsize("<BBId") if (len(txt) <= 3 + size): raise FlagParseException(txt) if (txt.startswith("FLG") == False): raise FlagParseException(txt) try: enc = base64.urlsafe_b64decode(txt[3:]) dec = xtea.crypt(self.key, enc, self.iv) packed = dec[:size] sig = dec[size:] except Exception as e: raise FlagParseException(str(e)) if (sig != hmac.new(self.phrase, packed, hashlib.md5).digest()[:12]): raise FlagParseException(txt) teamId, serviceId, round, timestamp = struct.unpack("<BBId", packed) return Flag(teamId, serviceId, round, timestamp)
def toTxt(self,flag): packed = struct.pack("<IIId",flag.teamId,flag.serviceId,flag.round,flag.timestamp) sig = hmac.new(self.phrase,packed,hashlib.md5).digest() enc = xtea.crypt(self.key,packed+sig,self.iv) return "FLG"+base64.urlsafe_b64encode(enc)
def toTxt(self, flag): packed = struct.pack("<BBId", flag.teamId, flag.serviceId, flag.round, flag.timestamp) sig = hmac.new(self.phrase, packed, hashlib.md5).digest()[:12] enc = xtea.crypt(self.key, packed + sig, self.iv) return "FLG" + base64.urlsafe_b64encode(enc)