

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 手動輪換金鑰
<a name="rotate-keys-manually"></a>

您可能想要建立新的 KMS 金鑰，並使用它來取代目前的 KMS 金鑰，而不是使用自動或隨需金鑰輪換。新的 KMS 金鑰擁有與目前 KMS 金鑰不同的密碼編譯資料，使用新的 KMS 金鑰與變更現有 KMS 金鑰中的金鑰材料具有相同的效果。將一個 KMS 金鑰更換為另一個 KMS 金鑰的程序稱為*手動金鑰輪換*。

![\[Diagram showing manual key rotation process with application, old key, and new key.\]](http://docs.aws.amazon.com/zh_tw/kms/latest/developerguide/images/key-rotation-manual.png)


 當您想要輪換不符合自動或隨需金鑰輪換資格的 KMS 金鑰時，例如非對稱 KMS 金鑰、HMAC KMS 金鑰和[自訂金鑰存放](key-store-overview.md#custom-key-store-overview)區中的 KMS 金鑰，手動輪換是不錯的選擇。

**注意**  
當您開始使用新的 KMS 金鑰時，請務必保持啟用原始 KMS 金鑰，以便 AWS KMS 可以解密原始 KMS 金鑰加密的資料。

在手動輪換 KMS 金鑰時，您還需要更新應用程式中對 KMS 金鑰 ID 或金鑰 ARN 的參考。[別名](kms-alias.md)會將易記名稱關聯至 KMS 金鑰，讓此程序更容易進行。您可在應用程式中使用別名來參考 KMS 金鑰。而後，在您想要變更應用程式使用的 KMS 金鑰時，變更別名的目標 KMS 金鑰即可，而無需編輯應用程式代碼。如需詳細資訊，請參閱[了解如何在應用程式中使用別名](alias-using.md)。

**注意**  
指向最新版本手動輪換 KMS 金鑰的別名是 [DescribeKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_DescribeKey.html)、[GetPublicKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GetPublicKey.html) 和密碼編譯操作的良好解決方案，例如 [DeriveSharedSecret](https://docs.aws.amazon.com/kms/latest/APIReference/API_DeriveSharedSecret.html)、[Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html)、[GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html)、[GenerateDataKeyPair](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyPair.html)、[GenerateMac](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateMac.html)、[VerifyMac](https://docs.aws.amazon.com/kms/latest/APIReference/API_VerifyMac.html)、[Sign](https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html) and [Verify](https://docs.aws.amazon.com/kms/latest/APIReference/API_Verify.html)。不允許在管理 KMS 金鑰的操作 (例如 [DisableKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_DisableKey.html) 或 [ScheduleKeyDeletion](https://docs.aws.amazon.com/kms/latest/APIReference/API_ScheduleKeyDeletion.html)) 中使用別名。  
在手動輪換對稱加密 KMS 金鑰上呼叫 [Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) 操作時，請省略 command 中的 `KeyId` 參數。 AWS KMS 自動使用加密加密文字的 KMS 金鑰。  
在使用非對稱 KMS 金鑰呼叫 `Decrypt` 或 [Verify](https://docs.aws.amazon.com/kms/latest/APIReference/API_Verify.html) 時，或是使用 HMAC KMS 金鑰呼叫 [VerifyMac](https://docs.aws.amazon.com/kms/latest/APIReference/API_VerifyMac.html) 時，`KeyId` 參數是必要項目。當 `KeyId` 參數的值是不再指向執行密碼編譯操作的 KMS 金鑰的別名時 (例如手動輪換金鑰時)，這些請求將失敗。若要避免此錯誤，您必須追蹤每個操作並為其指定正確的 KMS 金鑰。

若要變更別名的目標 KMS 金鑰，請使用 AWS KMS API 中的 [UpdateAlias](https://docs.aws.amazon.com/kms/latest/APIReference/API_UpdateAlias.html) 操作。例如，此命令會更新 `alias/TestKey` 別名以指向新的 KMS 金鑰。由於操作不會傳回任何輸出，範例使用 [ListAliases](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListAliases.html) 操作來顯示別名現在已與不同的 KMS 金鑰關聯，且 `LastUpdatedDate` 欄位已更新。ListAliases 命令使用 中的 [`query` 參數](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html#cli-usage-filter-client-side-specific-values) AWS CLI ，只取得`alias/TestKey`別名。

```
$ aws kms list-aliases --query 'Aliases[?AliasName==`alias/TestKey`]'
{
    "Aliases": [
        {
            "AliasArn": "arn:aws:kms:us-west-2:111122223333:alias/TestKey",
            "AliasName": "alias/TestKey",
            "TargetKeyId": "1234abcd-12ab-34cd-56ef-1234567890ab",
            "CreationDate": 1521097200.123,
            "LastUpdatedDate": 1521097200.123
        },
    ]
}


$ aws kms update-alias --alias-name alias/TestKey --target-key-id 0987dcba-09fe-87dc-65ba-ab0987654321
            
$ aws kms list-aliases --query 'Aliases[?AliasName==`alias/TestKey`]'
{
    "Aliases": [
        {
            "AliasArn": "arn:aws:kms:us-west-2:111122223333:alias/TestKey",
            "AliasName": "alias/TestKey",
            "TargetKeyId": "0987dcba-09fe-87dc-65ba-ab0987654321",
            "CreationDate": 1521097200.123,
            "LastUpdatedDate": 1604958290.722
        },
    ]
}
```