Overview
Thecrypto_utils module provides AES-256-CBC encryption/decryption functionality for handling encrypted provider data and M3U playlist content. It supports multiple decryption keys and automatic key rotation.
Classes
KeyInfo
Dataclass representing AES encryption key information.AES encryption key (32 bytes for AES-256)
Initialization vector for CBC mode (16 bytes)
Functions
decrypt_data()
Decrypts base64-encoded encrypted data using configured AES keys.Base64-encoded encrypted data (whitespace and line breaks are automatically stripped)
Decrypted plaintext string, or
None if decryption fails with all available keys- Cleans base64 input (removes
\n,\r, spaces, tabs) - Decodes from base64 to ciphertext bytes
- Attempts decryption with each available key (key1, key2)
- Returns the first successful decryption result
- Returns
Noneif all keys fail
- Starts with
{(JSON object) - Starts with
[(JSON array) - Contains
"http"(URL content)
decrypt_content()
Decrypts M3U playlist content with custom encryption scheme or returns raw content if already decrypted.Encrypted or plaintext M3U content
Decrypted M3U content, or original content if decryption fails or content is already plaintext
The function first checks if content is already valid M3U by looking for markers:
#EXTM3U#EXTINF#KODIPROP
-
Extract encrypted data parts:
- Part 1: Characters 0-10
- Part 2: Characters 34 to -54 (middle section)
- Part 3: Last 10 characters
- Combined:
part1 + part2 + part3
-
Extract embedded key and IV:
- IV (base64): Characters 10-34
- Key (base64): Characters -54 to -10
-
Decrypt using AES-256-CBC:
- Decode key, IV, and encrypted data from base64
- Use AES.MODE_CBC with embedded key and IV
- Unpad using PKCS5/PKCS7 padding
try_decrypt()
Attempts to decrypt ciphertext using a specific key and IV.Raw encrypted bytes (not base64-encoded)
KeyInfo object containing the AES key and IV to use
Decrypted and validated UTF-8 string, or
None if decryption/validation fails- Creates AES cipher in CBC mode with provided key and IV
- Decrypts the ciphertext
- Removes PKCS5/PKCS7 padding (last byte indicates padding length)
- Decodes to UTF-8 string
- Validates decrypted content
- Starts with
{(JSON object) - Starts with
[(JSON array) - Contains
"http"(case-insensitive)
Internal Functions
hex_string_to_bytes()
Converts hexadecimal string to bytes.Hexadecimal string (e.g., “0123456789abcdef”)
Byte representation of the hex string
parse_key_info()
Parses a secret string in the formatkey_hex:iv_hex into a KeyInfo object.
Secret string in format
"key_hex:iv_hex" (e.g., "0123...abcd:fedc...3210")KeyInfo object with parsed key and IV bytes
keys()
Loads and returns all configured decryption keys.Dictionary mapping key names (
"key1", "key2") to KeyInfo objectsKeys are loaded from secret files:
SECRET1fromresources/secret1.txtSECRET2fromresources/secret2.txt
key_hex:iv_hexConfiguration
Secret Files
The module reads encryption keys from:| File | Path | Content Format |
|---|---|---|
secret1.txt | {ADDON_PATH}/resources/secret1.txt | key_hex:iv_hex |
secret2.txt | {ADDON_PATH}/resources/secret2.txt | key_hex:iv_hex |
Encryption Details
Algorithm: AES-256-CBCPadding: PKCS5/PKCS7
Key Size: 256 bits (32 bytes)
IV Size: 128 bits (16 bytes)
Library: PyCryptodome (
Cryptodome.Cipher.AES)
