HLS encryption uses AES-128 to encrypt videos. This method works with all HLS-compatible players and requires key management and token services for access control. HLS encryption is commonly used in high-security-demand fields such as online education platforms and exclusive content streaming. This topic describes how to implement HLS encryption for video delivery and decryption for video playback.
Working principle
ApsaraVideo VOD employs the envelope encryption technology, where service providers generate a data key (DK) and an enveloped data key (EDK) through Alibaba Cloud Key Management Service (KMS). The DK encrypts the video content, while the EDK is stored alongside the encrypted video. During playback, the decryption service retrieves the DK to decrypt the video.
If you want to validate the decryption URL, you can enable M3U8 encryption and rewrite (by default, MtsHlsUriToken
is rewritten). This adds an extra layer of encryption to HLS data access.
Prerequisites
Your Alibaba Cloud account has sufficient balance. HLS encryption is a free service, but since encryption requires video transcoding, there will be service fees for the transcoding operations. For pricing details, see Media transcoding.
ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.
ApsaraVideo VOD is authorized to access KMS. You can authorize ApsaraVideo VOD to access KMS on the Cloud Resource Access Authorization page.
An accelerated domain is added to ApsaraVideo VOD. For more information, see Add a domain name for CDN.
The ApsaraVideo VOD SDK is integrated. In this topic, ApsaraVideo VOD SDK for Java is used as an example.
Encryption and decryption process
Upload and encryption
Decryption and playback
(Recommended) M3U8 encryption and rewrite enabled
M3U8 encryption and rewrite disabled
Encrypt a video
Upload your video and receive a callback.
To ensure that videos supporting HLS encryption are not automatically transcoded, you must use the system's built-in No Transcoding template group when uploading them to ApsaraVideo VOD. This prevents automatic transcoding from being triggered during the upload process.
Upload the video by using the ApsaraVideo VOD console or API.
Configure event notifications. When you receive the FileUploadComplete callback message, it means the video has been successfully uploaded to ApsaraVideo VOD.
Set up a key management service.
Create a service key.
The service key is the main encryption key in KMS and must be used to generate HLS encryption keys. Failure to create a service key will result in errors when you call the GenerateKMSDataKey operation.
In the upper-left corner of the page, select the region in which you want to create a service key.
NoteThe service key must be created in the same region as the origin server where your video is stored. For example, if your video is stored in China (Shanghai), the service key must also be created in China (Shanghai).
Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose Configuration Management > Media Processing > HLS Encryption.
On the HLS Encryption page, click Create Service Key.
After the service key is created, the system prompts The service key is created. You can view the service key in the Key Information section.
NoteIf you have been notified that the service key has been successfully created but still cannot view the key, it may be due to a missing service-linked role. You can reauthorize to restore the role and then refresh the page to view the service key.
Generate a DK.
To generate a DK and an EDK, call the GenerateKMSDataKey operation without the need for any input parameters. Upon success, CiphertextBlob in the response provides the AES_128 formatted EDK which must be cached for HLS encryption and transcoding, while Plaintext in the response contains the DK.
Create a transcoding template group for HLS encryption.
During the HLS encryption process, you need two transcoding template groups: the built-in No Transcoding template group and a custom transcoding template group. Here's how to create a custom transcoding template group for HLS encryption:
Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose Configuration Management > Media Processing > Transcoding Template Groups.
On the Transcoding Template Groups page, click Create Transcoding Template Group.
In the Basic Parameters section, set Encapsulation Format to hls. In the Advanced Parameters section, turn on Video Encryption and set Encryption Method to Alibaba Cloud Proprietary Cryptography. As for other parameters, configure them based on your actual needs. For information about parameter description, see Transcoding templates.
After creating the transcoding template group, find its ID on the Transcoding Template Groups page. Save this ID, as you will need it later for HLS encryption and transcoding.
Initiate an HLS encryption and transcoding request.
Call the SubmitTranscodeJobs operation to start HLS encryption and transcoding.
If you have configured event notifications in ApsaraVideo VOD, video transcoding completes when you receive either a StreamTranscodeComplete or TranscodeComplete callback.
View the HLS encryption result.
After the transcoding is complete, you can determine whether HLS encryption is successful by using the following methods:
Method 1: Log on to the ApsaraVideo VOD console. In the left-side navigation pane, choose Media Files > Audio/Video. On the Video and Audio page, find the video that you uploaded and click Manage in the Actions column. On the Video URL tab, if video outputs in formats other than the MP4 format exist, and HLS Encryption is tagged on M3U8 files, HLS encryption is successful.
Method 2: Copy the URL of an M3U8 file on which HLS Encryption is tagged, run the
curl -v "URL of the M3U8 file"
command, and then check the result. Ifthe decryption URL that you specify when you start HLS encryption is the same as the value of DecryptKeyUri
, HLS encryption is successful.Method 3: Call the GetTranscodeTask operation and pass the value of the JobId parameter that is returned in Step 4. If the value of TranscodeTemplateId in the response matches the ID of the transcoding template group created in Step 3 and TranscodeJobStatus shows "Transcoding succeeded," HLS encryption is successful.
(Recommended) M3U8 encryption and rewrite
After you enable the M3U8 encryption and rewrite feature, the system will automatically add encryption parameters (such as the encryption algorithm, key URI, and authentication parameters) after the #EXT-X-KEY tag in the M3U8 playlist file. When the client processes the rewritten M3U8 playlist file, it uses the key URI (which includes authentication parameters) to request the decryption key. The client then decrypts the transport stream (TS) files by using this key and the specified algorithm, ensuring encrypted access to HLS streams.
Step 1: Enable parameter pass-through for HLS encryption
Log on to the ApsaraVideo VOD console and turn on Parameter Pass-through for HLS Encryption.
After you enable Parameter Pass-through for HLS Encryption, the M3U8 playlist file can be modified. Specifically, it involves modifying the URI in the #EXT-X-KEY tag by appending the parameters carried in the client request at the end. By default, MtsHlsUriToken is rewritten.
Prerequisites
Cross-origin resource sharing (CORS) is configured.
Alibaba Cloud Gov Cloud does not support parameter pass-through for HLS encryption.
Procedure
Log on to the ApsaraVideo VOD console.
In the left-side navigation pane, find Configuration Management.
Choose CDN Configuration > Domain Names. The Domain Names page is displayed.
Find the domain name that you want to configure and click Configure in the Actions column.
In the left-side navigation pane of the domain name, click Video Related.
In the Encrypted Playback section, turn on Parameter Pass-through for HLS Encryption.
Once you enable this feature, the system will automatically handle authentication by rewriting the Token parameter when you pass HLS encryption parameters. The rewritten parameter is
MtsHlsUriToken
, and its value istest
. During CDN decryption and playback,MtsHlsUriToken=test
will be appended to the end of the URI in the#EXT-X-KEY
tag of the M3U8 playlist file.
Step 2: Send a request that includes the MtsHlsUriToken parameter
Send a request to the point of presence (POP) to access the M3U8 playlist file and include MtsHlsUriToken in the request.
To obtain MtsHlsUriToken, you must set up a token service.
The following sample code shows how to obtain MtsHlsUriToken. The following table describes the parameters in the sample code. You can modify them based on your business needs.
Step 3: Decrypt the playback file
After receiving the client request, the POP will decrypt the playback file upon successful authentication.
If the value of MtsHlsUriToken
generated in Step 2 is test,
Alibaba Cloud CDN will append MtsHlsUriToken=test
to the end of the URI in the #EXT-X-KEY
tag of the M3U8 playlist file during decryption and playback.
You need to implement the authentication validation logic on your own. For reference, you can review the sample code provided for setting up a decryption service with M3U8 encryption and rewrite enabled in the Play videos section.
Play videos
(Recommended) M3U8 encryption and rewrite enabled
Set up a decryption service.
Set up a local HTTP service to decrypt your video.
Call the DecryptKMSDataKey operation to decrypt the DK. The
PlainText
value in the response contains the DK. This DK is the Base64-decoded version of thePlainText
parameter returned in the response to the GenerateKMSDataKey operation.If you want to verify the decryption URL, enable M3U8 encryption and rewrite. By default,
MtsHlsUriToken
is rewritten. This way, your data is protected during HLS data access. The code used to set up a decryption service varies based on whether the M3U8 encryption and rewrite feature is enabled or not. The following section describes the sample code:Obtain the playback URL and credential of your encrypted video.
Call the GetVideoPlayAuth operation to obtain the playback credential and call the GetPlayInfo operation to obtain the playback URL.
Play the encrypted video.
HLS encryption supports all HLS-compatible players. You can use a self-developed player or ApsaraVideo Player to play encrypted videos.
If you use ApsaraVideo Player to play encrypted videos, you must obtain the token and authentication information. For more information, see How to play encrypted videos. If you use a third-party player, implement the playback logic on your own.
The following procedure describes how ApsaraVideo Player tests the playback:
(Recommended) M3U8 encryption and rewrite enabled
M3U8 encryption and rewrite disabled
Set up a decryption service.
Set up a local HTTP service to decrypt your video.
Call the DecryptKMSDataKey operation to decrypt the DK. The
PlainText
value in the response contains the DK. This DK is the Base64-decoded version of thePlainText
parameter returned in the response to the GenerateKMSDataKey operation.Sample code:
Obtain the playback URL and credential of your encrypted video.
Call the GetVideoPlayAuth operation to obtain the playback credential and call the GetPlayInfo operation to obtain the playback URL.
Play the encrypted video.
HLS encryption supports all HLS-compatible players. You can use a self-developed player or ApsaraVideo Player to play encrypted videos.
If you use ApsaraVideo Player to play encrypted videos, you must obtain the token and authentication information. For more information, see How to play encrypted videos. If you use a third-party player, implement the playback logic on your own.
The following procedure describes how ApsaraVideo Player tests the playback:
M3U8 encryption and rewrite disabled