Media Stream Encryption
Introduction
To improve data security, Agora supports encrypting users' media streams during real-time engagement. You can choose from the following encryption options according to your needs:
- Built-in encryption: Use the preset encryption mode in the SDK to encrypt the media streams.
- Customized encryption: Use the packet observer provided by the SDK to customize the encryption mode of media streams.
The following diagram describes the encrypted data transmission process:
Sample project
Agora provides the following open-source sample projects on GitHub.
- iOS: StreamEncryption
- macOS: StreamEncryption
You can download them and refer to the source code.
Implementation
Before enabling media-stream encryption, ensure that you refer to the appropriate Quickstart Guide to implement the basic real-time communication functions in your project.
Use built-in encryption
1. Integrate the encryption library (iOS only)
Since v3.2.0, the AgoraRtcCryptoLoader.framework
encryption library in the Agora iOS SDK has been merged into the AgoraRtcKit.framework
library. If you use the SDK of v3.2.0 and latter version, you can use the built-in encryption scheme after integrating the AgoraRtcKit.framework
library.
If you use the SDK of versions ealier than v3.2.0, after integrating the AgoraRtcKit.framework
, you also need to integrate the AgoraRtcCryptoLoader.framework
encryption library and import the class as follows:
- Choose either of the following ways to integrate the encryption library:
Through CocoaPods
a. Ensure that you have installed CocoaPods before the following steps. See the installation guide in Getting Started with CocoaPods.
b. In Terminal, go to the project path and run the pod init
command to create a Podfile
in the project folder.
c. Open the Podfile
, delete all contents and input the following contents. Remember to change Your App
to the target name of your project, and change version
to the version of the SDK which you want to integrate.
d. Go back to Terminal, and run the pod install
command to install the Agora SDK. Once you successfully install the SDK, it shows Pod installation complete!
in Terminal, and you can see an xcworkspace
file in the project folder.
e. Open the generated xcworkspace
file in Xcode.
Through your local storage
a. Copy AgoraRtcCryptoLoader.framework
from the SDK package to the project folder.
b. Open Xcode (take the Xcode 11.0 as an example), go to the TARGETS > Project Name > General > Frameworks, Libraries, and Embedded Content menu, click Add Other... after clicking + to add AgoraRtcCryptoLoader.framework
. To ensure that the signature of the dynamic library is the same as the signature of the app, you need to set the Embed attribute of the dynamic library to Embed & Sign.
- To import the AgoraRtcCryptoLoader library, refer to the following sample code:
2. Generate the key
- Refer to the following command to randomly generate a 32-byte key in the string format through OpenSSL on your server.
- The client gets the
key
in the string format from the server and passes it to the SDK in theenableEncryption
method.
3. Generate the salt
- Refer to the following command to randomly generate a Base64-encoded, 32-byte salt through OpenSSL on the server. You can also refer to the C++ sample code provided by Agora on GitHub to randomly generate a salt in the byte array format and convert it to Base64 on the server.
-
The client gets the Base64 salt from the server.
-
The client decodes the salt value from Base64 encoding to NSData of length 32, and then passes it to the SDK in the
enableEncryption
method.
4. Enable encryption
Before joining a channel, call enableEncryption
to enable the built-in encryption.
As of v3.4.5, Agora recommends using the AES_128_GCM2
or AES_256_GCM2
encryption mode and setting the key and salt.
GCM2
encryption modes use a more secure KDF (Key Derivation Function) and support setting the salt. If you choose other encryption modes, you only need to set the encryption mode and key.Sample code
API reference
Use customized encryption
Agora provides the IPacketObserver
class and the registerPacketObserver
method in C++ to enable customized encryption.
.mm
file..mm
file: #include <AgoraRtcKit/IAgoraRtcEngine.h>
.Refer to the following steps to implement customized encryption:
-
Implement your customized encryption algorithm through the
IPacketObserver
class in a.mm
file. -
Before joining a channel, register the packet observer, so that you can receive events during audio or video packet transmission.
In the
.mm
file, callregisterPacketObserver
to register the packet observer. -
Unregister the packet observer after leaving the channel.
In the
.mm
file, callregisterPacketObserver(NULL)
to unregister the packet observer.