aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/aes.py
diff options
context:
space:
mode:
authorXie Yanbo <xieyanbo@gmail.com>2022-10-11 20:55:09 +0800
committerGitHub <noreply@github.com>2022-10-11 13:55:09 +0100
commitc91cbf60729af93c4677864aa6c8b74b576146ca (patch)
tree56020436e3de9bd9182fcfa438be501db615b55d /youtube_dl/aes.py
parent11b284c81fe2988813c817918536fc3a5630870a (diff)
downloadyoutube-dl-c91cbf60729af93c4677864aa6c8b74b576146ca.tar.xz
[netease] Get netease music download url through player api (#31235)
* remove unplayable song from test * compatible with python 2 * using standard User_Agent, fix imports * use hash instead of long description * fix lint * fix hash
Diffstat (limited to 'youtube_dl/aes.py')
-rw-r--r--youtube_dl/aes.py37
1 files changed, 35 insertions, 2 deletions
diff --git a/youtube_dl/aes.py b/youtube_dl/aes.py
index d0de2d93f..a94a41079 100644
--- a/youtube_dl/aes.py
+++ b/youtube_dl/aes.py
@@ -8,6 +8,18 @@ from .utils import bytes_to_intlist, intlist_to_bytes
BLOCK_SIZE_BYTES = 16
+def pkcs7_padding(data):
+ """
+ PKCS#7 padding
+
+ @param {int[]} data cleartext
+ @returns {int[]} padding data
+ """
+
+ remaining_length = BLOCK_SIZE_BYTES - len(data) % BLOCK_SIZE_BYTES
+ return data + [remaining_length] * remaining_length
+
+
def aes_ctr_decrypt(data, key, counter):
"""
Decrypt with aes in counter mode
@@ -76,8 +88,7 @@ def aes_cbc_encrypt(data, key, iv):
previous_cipher_block = iv
for i in range(block_count):
block = data[i * BLOCK_SIZE_BYTES: (i + 1) * BLOCK_SIZE_BYTES]
- remaining_length = BLOCK_SIZE_BYTES - len(block)
- block += [remaining_length] * remaining_length
+ block = pkcs7_padding(block)
mixed_block = xor(block, previous_cipher_block)
encrypted_block = aes_encrypt(mixed_block, expanded_key)
@@ -88,6 +99,28 @@ def aes_cbc_encrypt(data, key, iv):
return encrypted_data
+def aes_ecb_encrypt(data, key):
+ """
+ Encrypt with aes in ECB mode. Using PKCS#7 padding
+
+ @param {int[]} data cleartext
+ @param {int[]} key 16/24/32-Byte cipher key
+ @returns {int[]} encrypted data
+ """
+ expanded_key = key_expansion(key)
+ block_count = int(ceil(float(len(data)) / BLOCK_SIZE_BYTES))
+
+ encrypted_data = []
+ for i in range(block_count):
+ block = data[i * BLOCK_SIZE_BYTES: (i + 1) * BLOCK_SIZE_BYTES]
+ block = pkcs7_padding(block)
+
+ encrypted_block = aes_encrypt(block, expanded_key)
+ encrypted_data += encrypted_block
+
+ return encrypted_data
+
+
def key_expansion(data):
"""
Generate key schedule