# Encryption（加密机制）

Deeplinks 使用对称密钥加密，密钥通过 [Diffie-Hellman 密钥交换](https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange)生成。虽然 deeplink 会话是以明文形式创建的，但会创建一个加密通道，防止会话令牌被劫持。

### **加密和解密工作流程**

Backpack 的 deeplinks 加密工作流如下：

#### **连接**

1. **\[dapp]**：在初次连接的 deeplink 请求中，应用应包含一个 `dapp_encryption_public_key` 查询参数。推荐为每次连接请求创建一个新的 x25519 密钥对。在所有方法中，此密钥对的公钥被称为 `dapp_encryption_public_key`。
2. **\[backpack]**：在处理连接 deeplink 时，Backpack 会生成一个新的 x25519 密钥对。
   * Backpack 会将此公钥作为 `wallet_encryption_public_key` 返回，并在连接响应中提供。
   * Backpack 会使用 Diffie-Hellman 方法，通过 `dapp_encryption_public_key` 和 `wallet_encryption_public_key` 的私钥生成共享密钥。
   * Backpack 会本地存储 `dapp_encryption_public_key` 与共享密钥之间的映射，用于后续 deeplinks 的解密操作。
3. **\[dapp]**：在接收到连接响应后，应用应通过 Diffie-Hellman 方法使用 `wallet_encryption_public_key` 和 `dapp_encryption_public_key` 的私钥创建共享密钥。该共享密钥将用于解密响应中的 `data` 字段。如果操作正确，用户的公钥将作为 JSON 对象中的一部分与应用共享。

#### 后续的 deeplinks&#x20;

1. **\[dapp]：**&#x5BF9;于任何后续方法（如 [SignAndSendTransaction ](/support-docs/cn/qian-bao/ji-shu-wen-dang/deeplinks/provider-fang-fa/signandsendtransaction-qian-ming-bing-fa-song-jiao-yi.md)和 [SignMessage](/support-docs/cn/qian-bao/ji-shu-wen-dang/deeplinks/provider-fang-fa/signmessage-qian-ming-xiao-xi.md)），应用程序应发送一个 `dapp_encryption_public_key`（与 Backpack 共享密钥的公钥部分），以及一个加密的有效载荷对象。
2. **\[backpack]**：一旦获得批准，Backpack 将把签名的响应加密为一个 JSON 对象，并将加密内容作为 `data=` 查询参数发送。
3. **\[dapp]**：收到deeplinks 响应后，应用程序应解密 `data=` 查询参数中的对象以查看签名。

### **加密资源 (Encryption Resources)**

要了解更多关于加密和解密的信息，请参考以下库：

**JavaScript**

* [TweetNaCl.js](https://github.com/dchest/tweetnacl-js)

**iOS**

* [TweetNaCl SwiftWrap](https://github.com/bitmark-inc/tweetnacl-swiftwrap)

**Android**

* [Tink](https://github.com/google/tink)
* [TweetNaCl Java](https://github.com/InstantWebP2P/tweetnacl-java)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://support.backpack.exchange/support-docs/cn/qian-bao/ji-shu-wen-dang/deeplinks/encryption-jia-mi-ji-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
