HLS encryption
HLS encryption prevents unauthorized access to video content and is widely used in online education and finance. Alibaba Cloud supports two encryption methods: Alibaba Cloud proprietary cryptography (recommended) and HLS encryption. This topic explains how HLS encryption works in ApsaraVideo Media Processing (MPS) and how to implement it.
How it works
Key concepts
MPS uses envelope encryption. Your service calls KMS to generate a data key (DK) and an enveloped data key (EDK). The DK encrypts the video. The encrypted file and EDK are stored together. The player obtains the DK from your decryption service to decrypt and play the video.
HLS encryption requires you to protect the data key (DK).
|
Concept |
Description |
|
Data key (DK) |
A plaintext key used to encrypt videos. |
|
Enveloped data key (EDK) |
A ciphertext key produced by encrypting the DK with envelope encryption. Used to recover the plaintext DK during decryption. |
|
Resource Access Management (RAM) |
Manages user identities and resource access permissions. What is RAM? |
|
Key Management Service (KMS) |
Provides key management, data encryption, and credential security. What is Key Management Service? |
|
Object Storage Service (OSS) |
Stores media resources processed by MPS. What is OSS? |
|
Content Delivery Network (CDN) |
Delivers content and dynamically modifies the decryption URI in M3U8 files during HLS playback. What is Alibaba Cloud CDN? |
Encryption flow
The encryption flow works as follows:

-
Activate MPS, OSS, RAM, KMS, and CDN if not already activated.
-
Authorize MPS to access KMS.
NoteMPS calls the GenerateDataKey operation of KMS to generate a DK and an EDK during encryption.
-
Configure the output OSS bucket domain as a CDN-accelerated domain. Set up the CNAME record and origin fetch host.
-
Create an encryption workflow. Specify the output OSS bucket and the Key URI.
The Key URI is your decryption service endpoint. MPS writes it into the M3U8 file after encryption.
-
Upload the video and specify the encryption workflow during upload.
-
After upload, MPS automatically triggers encryption and transcoding.
MPS calls GenerateDataKey to generate a DK and an EDK, encrypts the video with the DK, then writes the Key URI and EDK to the M3U8 file.
-
MPS stores the M3U8 file and the TS files in the output OSS bucket.
Decryption flow
The decryption and playback flow works as follows:

-
Build a token service to issue MtsHlsUriToken tokens.
ImportantThe token service issues MtsHlsUriTokens.
-
Build a decryption service that calls the KMS Decrypt API to obtain and return the DK to the player.
ImportantKMS returns the Base64-encrypted data key. Your service must Base64-decode it before returning it to the player.
-
Call QueryMediaList to get the M3U8 file URL. Append the MtsHlsUriToken and return the URL to the player.
-
The player sends a request with the MtsHlsUriToken to CDN to retrieve the M3U8 file. CDN rewrites the file to include your Key URI and the EDK, then returns it. The player requests the decryption key and plays the video.
Implementation overview
To implement the full HLS encryption and playback flow, you must build the following components:
-
Create an encryption workflow.
NoteYou can create a workflow in the console, but integrating a server-side SDK provides a more complete and efficient encryption service.
-
Build a token issuance and validation service for MtsHlsUriToken. Each token should be used only once.
-
Build a decryption service that calls KMS Decrypt. Base64-decode the plaintext key and return it to the player.
Prerequisites
Before you use HLS encryption, complete the following prerequisites:
-
Activate the required Alibaba Cloud services.
Activate MPS, OSS, KMS, RAM, and CDN if not already activated.
-
Activate MPS. Activate MPS
-
Activate OSS. Activate OSS
-
Activate KMS. Activate KMS
-
Activate RAM and grant permissions. Create a RAM user and grant permissions
-
Activate CDN. Activate CDN
-
-
Grant MPS access to KMS.
-
Log on to the RAM console.
-
Click Authorize to go to the Authorize page.
-
In the Principal search box, search for AliyunMtsDefaultRole and select the system-created role for MPS.
-
In the search box under Permissions, search for KMS, select AliyunKMSFullAccess, and then click OK.
After authorization, MPS can call KMS API operations to obtain data keys for video encryption.
-
-
Configure the CNAME record and origin fetch host for the output OSS bucket. Configure an accelerated domain name. Skip this step if already configured.
NoteYou can manually enter the public endpoint of an Alibaba Cloud OSS bucket, such as
exampleBucket****.oss-cn-hangzhou.aliyuncs.com, or select an OSS bucket that you want to accelerate in the same account. Endpoints for internal access within the same region are not supported.
Encrypt videos
To encrypt a video:
-
Create an encryption workflow.
Integrate an Alibaba Cloud SDK with MPS dependencies. Select your programming language for the code sample.
ImportantWhen you create the encryption workflow, provide your Key URI. MPS writes this URI to the M3U8 file and stores it in OSS during encryption. Example:
example.aliyundoc.com.Language
Integrate SDK
Encryption workflow code sample
Java
Python
PHP
Node.js
-
Upload a video to trigger encryption and transcoding. Upload from the MPS console or the OSS console. Upload videos
NoteSpecifying the encryption workflow during upload automatically triggers encryption and transcoding.
After encryption, view the M3U8 file in the output bucket on the OSS console. Sample file:
#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:5 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-KEY:METHOD=AES-128,URI="https://example.aliyundoc.com?Ciphertext=aabbccddeeff&MediaId=fbbf98691ea44b7c82dd75c5bc8b****" #EXTINF:4.127544, 15029611683170-00001.ts #EXT-X-ENDLISTThe URI contains the Key URI you configured and the EDK obtained from KMS.
Play HLS-encrypted videos
To decrypt and play the video:
-
Build a token service.
NoteBuild the token service based on your encryption logic to enhance video security.
-
Build a decryption service.
Build a local HTTP service to decrypt the video and return the decryption key. MPS provides code samples in Java and Python.
-
Java code sample
The Java SDK requires the following dependencies:
Base64 decryption sample:
KMS decryption sample:
-
Python code sample
The Python SDK requires the following dependencies:
-
pip install aliyun-python-sdk-core
-
pip install aliyun-python-sdk-kms
-
pip install aliyun-python-sdk-mts
The following is a Python code sample:
-
-
-
Call QueryMediaList to retrieve the playback URL.
Call this operation in API Explorer or integrate it into your service.
-
Play the encrypted video.
Use your own player or ApsaraVideo Player to play the encrypted video.
-
If you use a different player, implement the playback logic yourself.
-
If you use ApsaraVideo Player, obtain the token and authentication information, then start playback. Video playback
You can test playback with an online player.
For example, use the ApsaraVideo Player diagnostic tool. Paste the playback URL and click Play Video.
NoteDuring browser debugging, the player automatically requests the decryption key from the authentication server, then decrypts and plays the video.
The playback process in ApsaraVideo Player works as follows:
-
The player replaces the OSS domain with the CDN domain, appends MtsHlsUriToken, and requests the M3U8 file from CDN. Sample request:
https://example.aliyundoc.com/test_01.m3u8?MediaId=fbbf98691ea44b7c82dd75c5bc8b****&MtsHlsUriToken=<The token issued by your service>.ImportantApsaraVideo Player automatically appends the MtsHlsUriToken. If you use a different player, you must append the token manually.
-
CDN dynamically modifies the decryption URI in the M3U8 file. For example,
https://example.aliyundoc.com?Ciphertext=aabbccddeeff&MediaId=fbbf98691ea44b7c82dd75c5bc8b****becomeshttps://example.aliyundoc.com?Ciphertext=aabbccddeeff&MediaId=fbbf98691ea44b7c82dd75c5bc8b****&MtsHlsUriToken=<The token issued by your service>. -
The player accesses the URI in the EXT-X-KEY tag to obtain the decryption key. Your service calls Decrypt, Base64-decodes the plaintext key, and returns it to the player. The player uses the DK to decrypt the TS files for playback.
-