# BYOK 加密

> BYOK 加密 — Turso 文件
> Canonical HTML: https://tursodb.cn/zh-tw/docs/cloud/encryption/
> Upstream Source (MIT): https://docs.turso.tech/cloud/encryption
> Language: zh-TW

---
在 Turso Cloud 中，所有資料庫都在磁碟區層級進行靜態加密，這是我們 SOC2 標準的一部分。詳情請參閱[我們的信任中心](https://trust.turso.tech)。

Pro 與 Enterprise 客戶可以選擇使用您自行提供的金鑰來加密資料庫（Bring-Your-Own-Key Encryption，或稱 BYOK），詳情如下所述。

BYOK 加密在伺服器層級進行，確保任何人在任何時刻都無法存取您的資料。事實上，由於可以用不同的金鑰加密不同的資料庫，因此您可以為不同客戶的資料庫使用不同的金鑰。如果金鑰本身由您的客戶提供，BYOK 加密便能向您的客戶保證，即使是您也無法存取他們的資料。

Turso Cloud 的靜態資料 BYOK 加密採用業界標準的 AEAD 演算法。每個資料庫頁面都會個別加密，確保在所有環境中的資料完整性與安全性。BYOK 加密適用於 Pro 與 [Enterprise](https://tur.so/turso-enterprise-chat) 方案。

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

**金鑰由您保管。** 每個資料庫都可以使用只有您能控制的專屬金鑰進行加密。Turso 永遠不會看到或儲存您的加密金鑰。加密可與遠端查詢和同步無縫搭配運作。

</div></div>


* **頁面層級加密**：每個 4 KiB 頁面都會使用唯一的 nonce 個別加密
* **AEAD 演算法**：支援 AEGIS-256、AES-GCM 與 ChaCha20Poly1305
* **資料完整性**：內建驗證標籤可防止竄改與損毀
* **記憶體內金鑰**：加密金鑰永遠不會儲存在磁碟上

採用 BYOK 加密的 Turso Cloud 非常適合：

<div class="doc-cards">
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>AI 代理</span></div><p class="doc-card-desc">管理一群處理個人私密資料的可信代理</p></div>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>金融科技應用程式</span></div><p class="doc-card-desc">符合敏感財務資料的法規要求</p></div>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>注重隱私的產品</span></div><p class="doc-card-desc">以強大的資料保護建立使用者信任</p></div>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>醫療保健應用程式</span></div><p class="doc-card-desc">保護病患資料並符合 HIPAA 法規</p></div>
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>AI/ML 應用程式</span></div><p class="doc-card-desc">保護訓練資料與模型輸出</p></div>
</div>


## 加密範圍

<div class="doc-cards">
<div class="doc-card doc-card-static"><div class="doc-card-title"><span>已加密</span></div><p class="doc-card-desc">* 磁碟上與 S3 上的資料庫檔案及預寫日誌（WAL）檔案。</p></div>
</div>


## 產生加密金鑰

以 base64 格式產生安全的加密金鑰。金鑰大小取決於您選擇的加密演算法：

<div class="doc-tabs" data-doc-tabs>
<div class="doc-tab-list" role="tablist">
<button type="button" role="tab" class="doc-tab-btn is-active" aria-selected="true" data-tab-idx="0">256 位元（32 位元組）</button>
<button type="button" role="tab" class="doc-tab-btn" aria-selected="false" data-tab-idx="1">128 位元（16 位元組）</button>
</div>
<div class="doc-tab-panel is-active" role="tabpanel" data-tab-panel="0">

適用於 AEGIS-256 系列與 AES-256-GCM：

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

輸出範例：

```
YUfkdsD2SJe4KzTB8n6CjwCgs1YIW6k1xxMEguRuI5s=
```

</div>
<div class="doc-tab-panel" role="tabpanel" data-tab-panel="1" hidden>

適用於 AEGIS-128 系列與 AES-128-GCM：

```bash
openssl rand -base64 16
```

輸出範例：

```
XG1iWMFBFYEYrmYxjWDf5w==
```

</div>
</div>


<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">警告</div><div class="doc-callout-body">

**請安全地保存您的金鑰！** 如果您遺失加密金鑰，將無法存取加密的資料庫。金鑰永遠不會儲存在 Turso Cloud 的任何位置。

</div></div>


## 建立加密資料庫

**1. 以加密方式啟動**

在資料庫建立命令中使用 `--remote-encryption-key`、`--remote-encryption-cipher` 旗標，並指定您的加密演算法與金鑰：

```bash
turso db create my-secret-db --remote-encryption-key "YOUR_SECRET_KEY" --remote-encryption-cipher aegis128l
```

將 `YOUR_SECRET_KEY` 替換為您上方產生的金鑰。您也可以設定 `TURSO_DB_REMOTE_ENCRYPTION_KEY` 環境變數，而不必每次都傳遞旗標。

**2. 建立並插入資料**

使用 `--remote-encryption-key` 旗標啟動 shell，建立資料表並插入一些資料：

```bash
turso db shell my-secret-db --remote-encryption-key "YOUR_SECRET_KEY"
```

```sql
CREATE TABLE secrets (id INT, data TEXT);
INSERT INTO secrets VALUES (1, 'sensitive information');
INSERT INTO secrets VALUES (2, 'confidential data');
```

**3. 驗證加密**

離開 shell（輸入 `.quit`），然後嘗試在沒有金鑰的情況下開啟資料庫：

```bash
turso db shell my-secret-db
```

您將無法存取資料。該資料庫已在雲端中加密。

## 上傳現有資料庫

若要上傳現有的加密資料庫，請在建立時提供加密演算法與金鑰旗標：

```bash
turso db create my-secret-db --remote-encryption-key "YOUR_SECRET_KEY" --remote-encryption-cipher aegis128l --from-file my-db.db
```

## 分支與時間點還原（PITR）

加密資料庫支援[分支](/zh-tw/docs/features/branching/)與[時間點還原（PITR）](/zh-tw/docs/features/point-in-time-recovery/)。新分支將使用相同的加密演算法與相同的加密金鑰。建立時，您需要指定與父資料庫相同的加密金鑰：

```bash
turso db create my-secret-db-fork --remote-encryption-key "YOUR_SECRET_KEY" --from-db my-secret-db
```

## 重新加密（Rekeying）

目前尚不支援重新加密。不過，您可以匯出資料庫，並以不同的演算法與金鑰建立新的資料庫：

```bash
turso db export my-secret-db --remote-encryption-key "YOUR_SECRET_KEY"
turso db create my-new-secret-db --remote-encryption-key "YOUR_NEW_SECRET_KEY" --remote-encryption-cipher aegis256 --from-file my-secret-db.db
```

## 支援的加密演算法

Turso Cloud 支援三種 AEAD 加密演算法的多個變體，在效能與相容性之間提供不同的取捨。

### AES-GCM

經 NIST 核准的標準，適合法規遵循需求。在業界廣受支援。

| 加密演算法 | 金鑰大小 | 演算法代碼 | 使用情境 |
| - | - | - | - |
| **AES-128-GCM** | 128 位元（16 位元組） | `aes128gcm` | 符合 128 位元需求 |
| **AES-256-GCM** | 256 位元（32 位元組） | `aes256gcm` | 符合法規的最高安全性 |

### ChaCha20-Poly1305

| 加密演算法 | 金鑰大小 | 演算法代碼 | 使用情境 |
| - | - | - | - |
| **ChaCha20-Poly1305** | 256 位元（32 位元組） | `chacha20poly1305` | AES 的替代方案 |

### AEGIS

為速度最佳化的現代高效能加密演算法系列。**建議用於大多數使用情境。**

| 加密演算法 | 金鑰大小 | 演算法代碼 | 使用情境 |
| - | - | - | - |
| **AEGIS-128L** | 128 位元（16 位元組） | `aegis128l` | 效能均衡，128 位元安全性 |
| **AEGIS-128X2** | 128 位元（16 位元組） | `aegis128x2` | 2 倍平行處理 |
| **AEGIS-128X4** | 128 位元（16 位元組） | `aegis128x4` | 4 倍平行處理，最高速度 |
| **AEGIS-256** | 256 位元（32 位元組） | `aegis256` | 均衡的 256 位元安全性 |
| **AEGIS-256X2** | 256 位元（32 位元組） | `aegis256x2` | 2 倍平行處理，256 位元 |
| **AEGIS-256X4** | 256 位元（32 位元組） | `aegis256x4` | 4 倍平行處理，最高速度與安全性 |

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

**選擇加密演算法：**

* 128 位元加密請使用 `aegis128l`，256 位元加密請使用 `aegis256`（預設建議）
* 若使用 AES，128 位元加密請使用 `aes128gcm`，256 位元加密請使用 `aes256gcm` 或 `chacha20poly1305`

</div></div>


## 未來工作

開發中的未來加密功能：

* **加密現有資料庫**：將未加密的資料庫遷移為加密格式
* **金鑰輪替**：在無資料遺失的情況下更新加密金鑰
