from Crypto.Cipher import AES import struct import requests def RemovePadding(data, IsPkcs7 = True): dataLen = len(data) if IsPkcs7: iPadEnd = struct.unpack('B', data[dataLen-1])[0] iPadStart = struct.unpack('B', data[dataLen-iPadEnd])[0] if iPadEnd == iPadStart: data = data[0:dataLen - iPadStart] else: i = dataLen - 1 while i >= (dataLen - 16): if data[i] == '\x80': data = data[0:i] break i -= 1 return data def Decript(data,key,IV = None): if len(key) % 16: print("Khong the giai ma file .ts") return if IV is None: IV = data[0:16] data = data[16:] datalen = len(data) decryptor = AES.new(key,AES.MODE_CBC,IV) chucksize = 64*1024 dataOut = b'' i = 0 while True: chunk = data[i:i+chucksize] i += len(chunk) if i >= datalen: chunk = decryptor.decrypt(chunk) chunk = RemovePadding(chunk) dataOut += chunk break else: dataOut += decryptor.decrypt(chunk) return dataOut if __name__ == "__main__": r_ts = requests.get("https://stream.unica.vn/unica/_definst_/unica/232/23198/720.mp4/media_w96813910_tkd293emF0b2tlbmVuZFRpbWU9MTU0ODY5ODMxMSZ3b3d6YXRva2Vuc3RhcnRUaW1lPTE1NDg2OTExMTEmd293emF0b2tlbnVzZXJJZD0xMzc5NjImd293emF0b2tlbmhhc2g9TmtfQXd6VnN4YWs2MkthSmdTMHMxTGF3OGx1cjF5WVVTT3hLZV9RbjA3WT0=_0.ts") data = r_ts.content r_key = requests.get("https://handler.unica.vn:8080/handler?channel=unica&videoId=23198&wowzasessionid=96813910") key = r_key.content data_out = Decript(data,key) with open('data_out.ts','wb') as f: if data_out: f.write(data_out)