def encrypt(etype, key, msg_type, data): if etype != RC4_HMAC: raise NotImplementedError('Only RC4-HMAC supported!') k1 = HMAC.new(key, pack('<I', msg_type)).digest() data = random_bytes(8) + data chksum = HMAC.new(k1, data).digest() k3 = HMAC.new(k1, chksum).digest() return chksum + ARC4.new(k3).encrypt(data)
def decrypt(etype, key, msg_type, encrypted): # MAGIC if etype != RC4_HMAC: return '31337313373133731337' + str(etype) chksum = encrypted[:16] data = encrypted[16:] k1 = HMAC.new(key, pack('<I', msg_type)).digest() k3 = HMAC.new(k1, chksum).digest() data = ARC4.new(k3).decrypt(data) if HMAC.new(k1, data).digest() != chksum: raise ValueError('Decryption failed! (checksum error)') return data[8:]