HLS encryption uses the AES-128 algorithm, is compatible with all HLS players, and requires integration with key management and token services. It is widely used in scenarios that require a high level of security, such as online education and exclusive video streaming. This topic describes how to encrypt your videos by using HLS encryption and decrypt them for playback.
How it works
ApsaraVideo VOD uses envelope encryption. Your application generates a data key (DK) and an enveloped data key (EDK) using Alibaba Cloud Key Management Service (KMS). After the DK is used to encrypt a video, the encrypted video and the EDK are stored. During playback, a decryption service obtains the DK to decrypt the video.
If you need to secure decryption URLs, you can enable M3U8 encryption and rewrite (the default rewrite parameter is MtsHlsUriToken) to add another layer of encryption to protect HLS data access. For more information about how to enable this feature and its technical principles, see Configure M3U8 encryption and rewrite.
Prerequisites
Alibaba Cloud video encryption, specifically HLS encryption, is a free service. However, it requires video transcoding to perform the encryption, and transcoding operations incur fees. For more information about billing, see Media transcoding billing.
You have activated ApsaraVideo VOD. For more information, see Activate ApsaraVideo VOD.
You have authorized ApsaraVideo VOD to access your Key Management Service (KMS) resources. You can grant the required permissions on the Cloud Resource Access Authorization page.
You have configured an accelerated domain name in ApsaraVideo VOD. For more information, see Add an accelerated domain name.
You have installed the ApsaraVideo VOD server-side SDK. This topic uses the SDK for Java as an example. For more information, see Server-side SDK.
Encryption and decryption workflow
Upload and encryption workflow

Decryption and playback workflow
With rewrite (recommended)

Without rewrite

Encrypt a video
Upload a video and receive callbacks.
To prevent videos that support HLS encryption from being automatically transcoded, select the built-in No Transcoding template group when you upload videos to ApsaraVideo VOD. This prevents the upload from triggering a transcoding job.
Upload the video by using the ApsaraVideo VOD console or by calling an API operation for media upload.
Configure event notifications for ApsaraVideo VOD. When you receive a callback for a Video Upload Complete event, it indicates that the video is uploaded to ApsaraVideo VOD. For information about how to configure event notifications, see Event notifications.
Set up an encryption service.
Create a Service Key.
A Service Key is the primary encryption key in Key Management Service (KMS) required to generate keys for HLS encryption. If you do not create a Service Key, the call to the GenerateKMSDataKey operation will fail.
In the upper-left corner of the page, click the region ID to switch to the region where you want to create the Service Key.
NoteThe Service Key must be in the same region as your video's origin server. For example, if your video is stored in China (Shanghai), you must create the Service Key in the China (Shanghai) region.

Log on to the ApsaraVideo VOD console, and select Configuration Management > Media Processing > HLS Standard Encryption.
On the HLS Encryption page, click Create Service Key.
After the key is created, a message appears indicating that the Service Key is created. You can then view your Service Key in the Key Information section.
NoteIf a success message is displayed but you cannot see the Service Key, a service-linked role might be missing. You can click reauthorize to restore the role and then refresh the page to view the Service Key.
Call an API to generate a data key.
Call the GenerateKMSDataKey operation to generate a data key (DK) and an enveloped data key (EDK). You do not need to pass any parameters. On success, the returned
CiphertextBlobis the EDK in AES-128 format. Cache this value for the HLS encryption transcoding job. The returnedPlaintextis the DK.
Create a transcoding template group for HLS encryption.
The HLS encryption process requires two transcoding template groups: the built-in No Transcoding template group and a custom HLS encryption template group that you must create. Follow these steps to create the HLS encryption template group:
Log on to the ApsaraVideo VOD console, and select Configuration Management > Media Processing > Transcoding Template Groups.
On the Transcoding Template Groups page, create a template group for HLS encryption.
Set Encapsulation Format to hls. In the Advanced Parameters section, you must enable Video Encryption and select Alibaba Cloud Encryption as the encryption method. Configure other parameters based on your business requirements. For detailed parameter descriptions, see Transcoding templates.
After the template is created, you can view the ID of the transcoding template group on the transcoding template group list page. Save this ID to initiate the HLS encryption transcoding job.

Initiate an HLS encryption transcoding job.
Call the SubmitTranscodeJobs operation to initiate the HLS encryption transcoding job.
If you have configured event notifications for ApsaraVideo VOD, receiving a callback message for a Single Definition Transcoding Complete or All Definitions Transcoding Complete event indicates that the video transcoding is complete.
Verify the HLS encryption result.
After transcoding is complete, you can use one of the following three methods to verify that the HLS encryption was successful.
Method 1: Log on to the ApsaraVideo VOD console and select Media Files > Audio/Video > Manage > Video URL. On the Video URL page, if the video has outputs in multiple formats, such as an original file in MP4 format, you only need to check whether the M3U8 video stream is labeled with HLS Standard Encryption. If the label is present, it indicates that HLS encryption is successful.

Method 2: Copy the URL of the M3U8 file that is labeled HLS Standard Encryption, run the
curl -v "M3U8 file URL"command, and check if the returned M3U8 content contains the key informationURI="<The decryption URI that you pass when you initiate standard encryption, which is the value of the DecryptKeyUri parameter>". If this information is present, it indicates that Standard Encryption is used and the encryption was successful.Method 3: Call the GetTranscodeTask operation, passing the
JobIdvalue returned in Step 4. If the returnedTranscodeTemplateIdmatches the ID of the transcoding template group created in Step 3 and theTranscodeJobStatusisTranscodeSuccess, the HLS encryption was successful.
M3U8 encryption and rewrite (recommended)
When you enable the M3U8 encryption and rewrite feature, the system appends encryption parameters, such as the encryption algorithm, key URI, and authentication parameters, after the #EXT-X-KEY tag in the HLS media playlist (M3U8 file). When a client parses the rewritten M3U8 file, it uses the key URI with authentication parameters to request the key. The client then uses the key and the specified algorithm to decrypt the TS segments, ensuring secure, encrypted access to the HLS stream.
Step 1: Enable Parameter Pass-through for HLS Encryption
In the ApsaraVideo VOD console, enable Parameter Pass-through for HLS Encryption.
After you enable Parameter Pass-through for HLS Encryption, the system rewrites the HLS M3U8 file by modifying the URI in the #EXT-X-KEY tag. It appends parameters from the client request to the URI. The default parameter name is MtsHlsUriToken.
Prerequisites
Cross-origin resource sharing (CORS) is configured. For more information, see Configure cross-origin resource sharing.
Alibaba Cloud Gov Cloud does not support this feature.
Procedure
Log on to the ApsaraVideo VOD console.
In the left-side navigation pane, click Configuration Management.
Click CDN Configuration > Domain Names to go to the Domain Names page.
Find the desired domain name and click Configure in the Actions column.
In the left-side navigation pane for the domain name, click Video Related.
In the Encrypted Playback section, turn on the Parameter Pass-through for HLS Encryption switch.

When this feature is enabled, it authenticates requests for Parameter Pass-through for HLS Encryption by rewriting the token authentication parameter. The name of the rewritten parameter is
MtsHlsUriToken, and its value istest. When the CDN decrypts the content for playback,MtsHlsUriToken=testis appended to the end of the URI in the#EXT-X-KEYtag of the M3U8 file.
Step 2: Request with MtsHlsUriToken
The client sends a request to the CDN node to access the M3U8 file, including the MtsHlsUriToken parameter.
To obtain the MtsHlsUriToken, you must build a token service that issues user tokens.
The token generated by the following code is the MtsHlsUriToken. The following table describes the parameters in the code that you need to change based on your requirements.
Step 3: Decrypt and play the file
After the CDN node receives the client request, it authenticates the request and, if successful, decrypts the file for playback.
If the value of the MtsHlsUriToken parameter generated in Step 2 is test, MtsHlsUriToken=test is appended to the end of the URI in the #EXT-X-KEY tag of the M3U8 file during CDN decryption and playback.
You must implement the authentication logic yourself. For reference, see the sample code for the decryption service in the M3U8 encryption and rewrite method in the Play the video section.
Play the video
With rewrite (recommended)
Set up a decryption service.
Set up a local HTTP service to decrypt the video.
Calling the Decrypt KMS Data Key Ciphertext operation returns the data key (DK) as
PlainText. This key is the result of Base64-decoding thePlainTextthat is returned by the Create KMS Data Key operation.If you need to perform security verification for decryption URLs, you can enable M3U8 encryption and rewrite (the default parameter name is
MtsHlsUriToken) to add another layer of encryption to protect the HLS data access process. The code implementation for the decryption service differs depending on whether you enable M3U8 encryption and rewrite. The details are as follows:Obtain the playback URL and credential.
Call the GetVideoPlayAuth operation to get the playback credential or call the GetPlayInfo operation to get the playback URL.
Play the encrypted video.
HLS encryption is compatible with all HLS players. You can use your own player or ApsaraVideo Player to play the encrypted video.
If you use ApsaraVideo Player, you must obtain a token and authentication information required by the player before starting playback. For more information, see Play encrypted videos. If you use a player other than ApsaraVideo Player, you must implement your own playback logic.
The following section describes the internal workflow for playback with ApsaraVideo Player:
With rewrite (recommended)
Without rewrite
Set up a decryption service.
Set up a local HTTP service to decrypt the video.
Call the Decrypt operation to decrypt the ciphertext of a KMS data key. The returned data key (DK), which is the
PlainTextvalue, is the result of base64-decoding thePlainTextvalue that is returned by the CreateDataKey operation.The following is a code sample:
Obtain the playback URL and credential.
Call the GetVideoPlayAuth operation to get the playback credential or call the GetPlayInfo operation to get the playback URL.
Play the encrypted video.
HLS encryption is compatible with all HLS players. You can use your own player or ApsaraVideo Player to play the encrypted video.
If you use ApsaraVideo Player, you must obtain a token and authentication information required by the player before starting playback. For more information, see Play encrypted videos. If you use a player other than ApsaraVideo Player, you must implement your own playback logic.
The following section describes the internal workflow for playback with ApsaraVideo Player:
Without rewrite