# Secure channel encryption (/en/realtime-media/video/build/secure-and-protect-channels/media-stream-encryption/android)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

Media stream encryption refers to encrypting audio and video streams in an app using a unique [key](https://en.wikipedia.org/wiki/Public_key_certificate) and [salt](https://en.wikipedia.org/wiki/Salt_\(cryptography\)) controlled by the app developer. Encryption ensures that only the authorized users in a channel see and hear each other. Video SDK provides built-in encryption methods that you can use to guarantee data confidentiality during transmission.

This article describes how to integrate Agora built-in media stream encryption into your app.

## Understand the tech [#understand-the-tech]

The following figure illustrates the process of data transfer with media stream encryption enabled.

**Data transfer process**

![EncryptMediaStream](https://assets-docs.agora.io/images/video-sdk/encrypt_media_streams_dataTransferProcess-web.svg)

## Prerequisites [#prerequisites]

Ensure that you have implemented the [SDK quickstart](/en/realtime-media/video/get-started-sdk) in your project.

## Implement media stream encryption [#implement-media-stream-encryption]

      
    To add built-in media stream encryption to your app, refer to the following steps:

    1. Generate a key and salt on your server

       * To generate a random 32-byte hexadecimal key on your server as a string, refer to the following `OpenSSL` command:

         ```shell
         # Generate a 32-byte hexadecimal key
         openssl rand -hex 32
         ```

       * To generate a random Base64-encoded, 32-byte salt on your server, refer to the following `OpenSSL` command:

         ```shell
         # Generate a Base64-encoded, 32-byte salt
         openssl rand -base64 32
         ```

    2. Implement client-side logic

       1. Obtain a String key and Base64-encoded salt from the server.

       2. Convert the salt from Base64 to `uint8_t`.

       3. Before joining the channel, call `enableEncryption` to set the `AES_128_GCM2` or `AES_256_GCM2` encryption mode, and pass the key and salt to the SDK.

       <CalloutContainer type="warning">
         <CalloutTitle>
           note
         </CalloutTitle>

         <CalloutDescription>
           * All users in a channel must use the same encryption mode, key, and salt. Discrepancies may lead to unexpected behavior, such as black screens or audio loss.
           * To ensure security, best practice is to use a new key and salt each time you enable media stream encryption.
         </CalloutDescription>
       </CalloutContainer>

       To implement this logic, refer to the following code:

       ```java
       import java.util.Base64;

       byte[] getEncryptionKdfSaltFromServer() {
         String base64Salt = "EncryptionKdfSaltInBase64Strings";
         return Base64.getDecoder().decode(base64Salt);
       }

       // Create an EncryptionConfig instance
       EncryptionConfig config = new EncryptionConfig();
       // Set the encryption mode to AES_128_GCM2
       config.encryptionMode = EncryptionMode.AES_128_GCM2;
       // Obtain the Base64-encoded salt from the server
       config.encryptionKdfSalt = getEncryptionKdfSaltFromServer();
       // Obtain the String type key from the server
       config.encryptionKey = getEncryptionKeyFromServer();
       int ret = rtcEngine.enableEncryption(true, config);
       if (ret != 0) {
         showAlert("Error", "enableEncryption call failed: " + ret + ", please check your params");
       }
       ```

    ### Development considerations [#development-considerations]

    The media stream encryption feature is supported in both communication and live broadcasting use-cases. However, in the live broadcasting use-case, Agora does not support pushing the encrypted media stream to CDN.

    ### API reference [#api-reference]

    * [`enableEncryption`](https://api-ref.agora.io/en/video-sdk/android/4.x/API/class_irtcengine.html#api_irtcengine_enableencryption)
    * [`EncryptionConfig`](https://api-ref.agora.io/en/video-sdk/android/4.x/API/class_encryptionconfig.html)
    * [`EncryptionMode`](https://api-ref.agora.io/en/video-sdk/android/4.x/API/namespaceagora_1_1rtc.html#a8fbe0baa82fa7db7ad7276f1fdb5cc53)

    
  
      
  
      
  
      
  
      
  
      
  
      
  
      
  
      
  
