The video encryption feature allows you to perform in-depth security processing on video content and prevent video data from being illegally acquired and transmitted. This feature is widely used to prevent video leaks and hotlinking in scenarios that require high security, such as online education and finance. ApsaraVideo Media Processing (MPS) supports two encryption methods: Alibaba Cloud proprietary cryptography and HTTP-Live-Streaming (HLS) encryption. We recommend that you use Alibaba Cloud proprietary cryptography. This topic describes how HLS encryption works and how to use HLS encryption. This helps you ensure video security by using HLS encryption and smoothly play videos.
How it works
Terms
MPS uses the envelope encryption technology to encrypt videos. You can use Alibaba Cloud Key Management Service (KMS) to generate a data key (DK) and an enveloped data key (EDK). Then, you can use the DK to encrypt a video and store the encrypted video together with the EDK. If you want to play the video, the player uses a decryption service to obtain the DK and decrypt the video.
HLS encryption requires you to preserve your DKs.
Term | Description |
DK | A DK is also known as a plaintext key, which is used for video encryption. |
EDK | An EDK is also known as a ciphertext data key, which is generated by using the envelope encryption technology. It is used to decrypt a DK and obtain the plaintext data key. |
RAM | Resource Access Management (RAM) is a service provided by Alibaba Cloud. It allows you to manage user identities and resource access permissions. For more information, see What is RAM? |
KMS | KMS is an end-to-end service platform for key management, data encryption, and secret management. KMS provides simple, reliable, secure, and standard-compliant capabilities to encrypt and protect data and manage secrets. For more information, see What is KMS? |
Object Storage Service (OSS) | OSS is a data storage service provided by Alibaba Cloud. Media files for MPS jobs are stored in OSS buckets. For more information, see What is OSS? |
Alibaba Cloud CDN | In the process of HLS encryption, Alibaba Cloud CDN dynamically modifies the decryption uniform resource identifier (URI) in the M3U8 file and returns the decryption URI to the player. For more information, see What is Alibaba Cloud CDN? |
Encryption process
The following figure shows the process of encrypting a video in MPS.
Activate the following services: MPS, OSS, RAM, KMS, and CDN.
Grant MPS the permissions to access KMS.
NoteThis ensures that MPS can call the GenerateDataKey operation of KMS to generate DKs and EDKs during video encryption.
Configure a CDN-accelerated domain name for the OSS bucket that stores the output files of MPS jobs. Add a CNAME record and configure the origin host for the CDN-accelerated domain name.
Create a workflow for video encryption and specify information such as the OSS bucket that stores output files and the key URI.
The key URI specifies the endpoint of your services. The data about the key URI is contained in the M3U8 file generated by MPS after video encryption.
Upload the video to be encrypted and specify the created workflow for the video.
After the video is uploaded, MPS automatically triggers the workflow.
Then, MPS calls the GenerateDataKey operation to generate a DK and an EDK, and uses the DK to encrypt the video. After the video is encrypted, MPS writes the data about the key URI and EDK to the M3U8 file.
MPS stores the M3U8 file and the TS file in the OSS bucket that stores output files.
Decryption process
The following figure shows the process of decrypting an HLS-encrypted video.
Construct a token issuance service to generate the MtsHlsUriToken parameter.
ImportantThe token issuance service is used to generate the MtsHlsUriToken parameter.
Call the Decrypt operation of KMS to construct a decryption service and return the DK to the player.
ImportantAfter you call the Decrypt operation of KMS to obtain the Base64-encoded DK from KMS, decode the DK by using the Base64 algorithm and return the decoded DK to the player.
Call the QueryMediaList operation of MPS to query the OSS URL of the M3U8 file, add the MtsHlsUriToken parameter to the OSS URL, and then return the OSS URL to the player.
The player uses the MtsHlsUriToken parameter and the DK to request the streaming URL of the video from CDN. CDN modifies the M3U8 file and returns the key URI and EDK to the player. Then, the player decrypts and plays the video.
Code logic
In the processes of HLS encryption and decryption, you must implement the following code logic:
Create a workflow for video encryption.
NoteAlthough you can create a workflow in the MPS console, we recommend that you create a workflow by using a server SDK.
Construct a token issuance service to generate the MtsHlsUriToken parameter, which is used as the decryption token. Verify the decryption token. We recommend that you use each decryption token only once.
Call the Decrypt operation of KMS to construct a decryption service. Decode the DK by using the Base64 algorithm and return the decoded DK to the player.
Prerequisites
Before you use HLS encryption in MPS, make the following preparations:
Activate relevant Alibaba Cloud services, including MPS, OSS, KMS, RAM, and CDN.
If you have not activated these services, perform the following steps:
Activate MPS. For more information, see Activate MPS.
Activate OSS. For more information, see Activate OSS.
Activate KMS. For more information, see Purchase a dedicated KMS instance.
Activate Alibaba Cloud CDN. For more information, see Activate Alibaba Cloud CDN.
Grant MPS the permissions to access KMS.
Log on to the RAM console.
In the left-side navigation pane, choose Authorize. On the Grants page, click Authorize. The Grant Permission panel appears.
In the Principal search box, enter AliyunMtsDefaultRole and select a role that is created by the system and can be used in MPS.
In the search box of the Policy section, enter KMS. Select AliyunKMSFullAccess and click Grant permissions.
Then, MPS is granted the permissions to access KMS. After MPS receives a video encryption request, MPS can call the Decrypt operation of KMS to obtain the DK.
Configure a CDN-accelerated domain name for the OSS bucket that stores output files, and configure the origin host for the CDN-accelerated domain name. For more information, see Configure a domain name for CDN. If the CDN-accelerated domain name and the origin host are configured, skip this step.
NoteYou can enter the public domain name of the OSS bucket, such as
exampleBucket****.oss-cn-hangzhou.aliyuncs.com
. You can obtain the public domain name in the OSS console. Alternatively, you can select the OSS bucket that stores output files and requires content delivery acceleration within the same Alibaba Cloud account. Internal domain names of OSS buckets are not supported.
Encrypt a video
To encrypt a video, perform the following steps:
Create a workflow for video encryption.
To create a workflow, you must use an Alibaba Cloud SDK, and add MPS dependencies. You can view the sample code of workflow creation based on the programming language that you use. For more information, see the following table.
ImportantWhen you create a workflow, you must provide the key URI. During video encryption, MPS writes the key URI to the M3U8 file and stores the file in the OSS bucket that stores output files. An example of the key URI is
example.aliyundoc.com
.Programming language
SDK
Sample code
Java
Python
PHP
Upload a video to trigger the workflow. You can upload a video in the MPS console or OSS console.
NoteIf you specify the created workflow for the video in the upload configuration, after the video is uploaded, MPS automatically triggers the workflow.
After the video is encrypted, log on to the OSS console and view the M3U8 file in the OSS bucket that stores output files. The following sample code provides an example of the M3U8 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-ENDLIST
In this example, the key URI that you configure and the EDK are contained in the URI field.
Play an HLS-encrypted video
To play an HLS-encrypted video, perform the following steps:
Construct a token issuance service.
NoteYou must construct the token issuance service based on your encryption method to ensure a high security level.
Construct a decryption service.
Construct a local HTTP service to decrypt the video and obtain the decryption key. The following sample code provides examples on how to construct the decryption service in Java and Python.
Sample code in Java
Dependencies required by the SDK for Java:
Sample code for decryption by using the Base64 algorithm:
Sample code for decryption by using KMS:
Sample code in Python
Dependencies required by the SDK for Python:
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-kms
pip install aliyun-python-sdk-mts
Sample code in Python:
Call the QueryMediaList operation of MPS to query the streaming URL of the video.
You can call the operation in OpenAPI Explorer or by integrating an SDK.
Play the encrypted video.
You can use ApsaraVideo Player or a third-party player to play the encrypted video.
If you use a third-party player, specify the playback logic.
If you use ApsaraVideo Player, obtain the security token and authentication information as required before the video is played. For more information, see Play videos.
You can also use an online player to test the playback of the video encrypted by HLS encryption.
For example, you can use the ApsaraVideo Player diagnostics tool. When you use this tool, enter the streaming URL of the video in the Source field and click Play.
NoteOn the browser debugging page, you can see that the player sends a request to the authentication server, obtains the decryption key, and then decrypts and plays the video.
The following procedure describes how ApsaraVideo Player tests the playback:
After ApsaraVideo Player receives the streaming URL, it replaces the domain name of the OSS bucket with a CDN-accelerated domain name. Then, ApsaraVideo Player adds the MtsHlsUriToken parameter, which is used as the decryption token, to the CDN-accelerated domain name, and sends a request to CDN for a modified streaming URL. Sample request:
https://example.aliyundoc.com/test_01.m3u8?MediaId=fbbf98691ea44b7c82dd75c5bc8b****&MtsHlsUriToken=<Token>
.ImportantIf you use ApsaraVideo Player, the MtsHlsUriToken parameter is automatically added. If you use other players, you must manually add the MtsHlsUriToken parameter.
After CDN receives the request, it dynamically modifies the key URI in the M3U8 file and returns the modified streaming URL to the player. For example, if the original streaming URL is
https://example.aliyundoc.com?Ciphertext=aabbccddeeff&MediaId=fbbf98691ea44b7c82dd75c5bc8b****
, the returned streaming URL ishttps://example.aliyundoc.com?Ciphertext=aabbccddeeff&MediaId=fbbf98691ea44b7c82dd75c5bc8b****&MtsHlsUriToken=<Token>
.The player parses and accesses the URI in the EXT-X-KEY tag of the M3U8 file to obtain the decryption key. Call the Decrypt operation of KMS, decode the obtained DK by using the Base64 algorithm, and then return the decoded DK to the player. The player uses the DK to decrypt the TS file and plays the video.