# Secure channel encryption (/en/realtime-media/voice/build/secure-and-protect-channels/media-stream-encryption/react-native)

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

Media stream encryption encrypts audio and video streams with a unique [key](https://en.wikipedia.org/wiki/Public_key_certificate) and [salt](https://en.wikipedia.org/wiki/Salt_\(cryptography\)) controlled by your app server. Encryption ensures that only authorized users in a channel can see and hear each other.

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

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

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

    **Data transfer process**

    ![Encrypted media stream transfer process](https://assets-docs.agora.io/images/video-sdk/encrypt_media_streams_dataTransferProcess.svg)

    ## Prerequisites [#prerequisites-7]

    Ensure that you have implemented the [SDK quickstart](../index.mdx) in your project.

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

    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, run:

       ```bash
       openssl rand -hex 32
       ```

       To generate a random Base64-encoded, 32-byte salt on your server, run:

       ```bash
       openssl rand -base64 32
       ```

    2. Implement client-side logic.

    <CalloutContainer type="info">
      <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, use a new key and salt each time you enable media stream encryption.
      </CalloutDescription>
    </CalloutContainer>

    Obtain the string key and Base64-encoded salt from your server, convert the salt to a 32-byte array, and call `enableEncryption` before joining a channel. Set the encryption mode to `Aes128Gcm2` or `Aes256Gcm2` and pass the key and salt to the SDK.

    ```typescript
    const encryptionMode = EncryptionMode.Aes128Gcm2;
    const encryptionKey = await getEncryptionKeyFromServer();
    const encryptionKdfSalt = await getEncryptionSaltBytesFromServer();

    if (!encryptionKey) {
      throw new Error('encryptionKey is invalid');
    }

    rtcEngine.enableEncryption(true, {
      encryptionMode,
      encryptionKey,
      encryptionKdfSalt,
    });
    ```

    ## Reference [#reference-7]

    ### Sample projects [#sample-projects-1]

    * [Electron encryption example](https://github.com/AgoraIO-Extensions/Electron-SDK/blob/main/example/src/renderer/examples/advanced/Encryption/Encryption.tsx)
    * [React Native encryption example](https://github.com/AgoraIO-Extensions/react-native-agora/blob/main/example/src/examples/advanced/Encryption/Encryption.tsx)

    ### API reference [#api-reference-7]

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

    
  
      
  
