from Crypto.Hash import MD5
from binascii import a2b_hex, b2a_hex
from Crypto.Cipher import AES
def md5hex(data):
""" return hex string of md5 of the given string """
# type(data): bytes
# returns: bytes
h = MD5.new()
h.update(data)
return b2a_hex(h.digest())
def hexaescrypt(data, key):
c = AES.new(key.encode(), AES.MODE_ECB)
a = b2a_hex(c.encrypt(data))
print(a)
return a
def genurlkey(songid, md5origin, mediaver=4, fmt=1):
data_concat = b'\xa4'.join(_ for _ in [md5origin.encode(),
str(fmt).encode(),
str(songid).encode(),
str(mediaver).encode()])
data = b'\xa4'.join([md5hex(data_concat), data_concat]) + b'\xa4'
if len(data) % 16 != 0:
data += b'\0' * (16 - len(data) % 16)
print("md5originBytes:", md5origin.encode())
print("fmtBytes:", str(fmt).encode())
print("songidBytes:", str(songid).encode())
print("mediaverBytes:", str(mediaver).encode())
print("data_concat:", data_concat)
print("md5Hash:", md5hex(data_concat))
print("data:", data)
print("Padded data length:", len(data))
return hexaescrypt(data, "jo6aey6haid2Teih")
print(genurlkey("3599689","b29da881b5507b49e0d19957805d57ec",7,1))