android - Malformed encryption mp3 to m3u8 -


i have python function encoding mp3 m3u8. function allows me generate m3u8 file along ts chunks.

i can read "playlist" using native player on ios. unfortunately can't achieve using android-mediaplayer (i got error media_error_malformed).

the catch is, if in python, use openssl via subprocess, works. spawning new process expensive , want avoid :

 cmd = ["openssl",             "aes-128-cbc",             "-e",             "-in", path,             "-out", dest_path+".openssl.ts",             "-iv", ("%d" % iv_counter).zfill(32),             "-k", keyhex]         subprocess.check_call(cmd) 

using openssl or implementation produces same m3u8 file, same numbers of ts files , these ts files have same weight.

the explanation find implementation wrong. know may hard debug, maybe jump @ at first reading. here function doing encryption :

from crypto import random crypto.cipher import aes def encrypt(manifest, chunks, enc_dir):     os.makedirs(enc_dir)      # random key     key = random.new().read(16)     keyhex = key.encode('hex')      # encrypt each chunk     iv_counter, (_, path) in enumerate(chunks):         open(path, "rb") chunk:             chunk_data = chunk.read()          # pkcs#7 padding         pad = 16 - (len(chunk_data) % 16)         chunk_data += chr(pad) * pad          print "crypting using %s" % ("%d" % iv_counter).zfill(32)          # aes encryption         aes = aes.new(key, aes.mode_cbc, "%16x" % iv_counter)         chunk_data = aes.encrypt(chunk_data)          dest_path = os.path.join(enc_dir, os.path.basename(path))         #cmd = ["openssl",         #    "aes-128-cbc",         #    "-e",         #    "-in", path,         #    "-out", dest_path+".openssl.ts",         #    "-iv", ("%d" % iv_counter).zfill(32),         #    "-k", keyhex]         #subprocess.check_call(cmd)         open(dest_path, "wb") chunk:             chunk.write(chunk_data)          # write key file         key_file = os.path.join(enc_dir, os.path.splitext(os.path.basename(manifest))[0] + ".key")         open(key_file, "w") keyf:             keyf.write(key)         key_url = os.path.basename(key_file) #"file://" + os.path.abspath(key_file)          # write new manifest         dest_manifest = os.path.join(enc_dir, os.path.basename(manifest))         open(dest_manifest, "w") manifest:             manifest.write("#extm3u\n")             manifest.write("#ext-x-version:3\n")             manifest.write("#ext-x-media-sequence:0\n")             manifest.write("#ext-x-allow-cache:yes\n")             manifest.write("#ext-x-targetduration:6\n")             manifest.write("#ext-x-key:method=aes-128,uri=\"%s\"\n" % key_url)             extinf, path in chunks:                 manifest.write("%s\n%s\n" % (extinf, os.path.basename(path)))             manifest.write("#ext-x-endlist\n") 

edit, if can helps : have coded small java function decrypt ts file produced openssl , our homemade code. file produced openssl fine got bad padding exception 1 produced our python code.

the problem coming initial vector (iv). openssl waiting number parameter (in hexadecimal), in string format of 16 characters.

your code returns number in hexadecimal, in ascii format:

    >>> iv_counter = 11111111     >>> print("%16x" % iv_counter)     '          a98ac7' 

however, expected value is:

    '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9\x8a\xc7' 

to achieve it, must replace with:

    >>> print(("%032x" % iv_counter).decode("hex"))     '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9\x8a\xc7' 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -