Menu
AWS Key Management Service
Developer Guide

Working With Keys

The examples in this topic use the AWS KMS API to create, view, enable, and disable AWS KMS customer master keys, and to generate data keys.

Creating a Customer Master Key

To create a customer master key, use the CreateKey operation.

This example uses the kmsClient client object that you created in Creating a Client.

JavaC#
Java

For details, see the createKey method in the AWS SDK for Java API Reference.

// Create a CMK // String desc = "Key for protecting critical data"; CreateKeyRequest req = new CreateKeyRequest().withDescription(desc); CreateKeyResult result = kmsClient.createKey(req);
C#

For details, see the CreateKey method in the AWS SDK for .NET.

// Create a CMK // String desc = "Key for protecting critical data"; CreateKeyRequest req = new CreateKeyRequest() { Description = desc }; CreateKeyResponse response = kmsClient.CreateKey(req);

Generating a Data Key

To generate a data key, use the GenerateDataKey operation. This operation returns plaintext and encrypted copies of the data key that it creates.

This example uses the kmsClient client object that you created in Creating a Client.

JavaC#
Java

For details, see the generateDataKey method in the AWS SDK for Java API Reference.

// Generate a data key // String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; GenerateDataKeyRequest dataKeyRequest = new GenerateDataKeyRequest(); dataKeyRequest.setKeyId(keyId); dataKeyRequest.setKeySpec("AES_128"); GenerateDataKeyResult dataKeyResult = kmsClient.generateDataKey(dataKeyRequest); ByteBuffer plaintextKey = dataKeyResult.getPlaintext(); ByteBuffer encryptedKey = dataKeyResult.getCiphertextBlob();
C#

For details, see the GenerateDataKey method in the AWS SDK for .NET.

// Generate a data key // String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; GenerateDataKeyRequest dataKeyRequest = new GenerateDataKeyRequest() { KeyId = keyId, KeySpec = DataKeySpec.AES_128 }; GenerateDataKeyResponse dataKeyResponse = kmsClient.GenerateDataKey(dataKeyRequest); MemoryStream plaintextKey = dataKeyResponse.Plaintext; MemoryStream encryptedKey = dataKeyResponse.CiphertextBlob;

Viewing a Custom Master Key

To get detailed information about a customer master key (CMK), including the CMK ARN and key state, use the DescribeKey operation.

DescribeKey does not get aliases. To get aliases, use the ListAliases operation.

This example uses the kmsClient client object that you created in Creating a Client.

JavaC#
Java

For details, see the describeKey method in the AWS SDK for Java API Reference.

// Describe a CMK // // Replace the fictitious key ARN with a valid one. String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DescribeKeyRequest req = new DescribeKeyRequest().withKeyId(keyId); DescribeKeyResult result = kmsClient.describeKey(req);
C#

For details, see the DescribeKey method in the AWS SDK for .NET.

// Describe a CMK // // Replace the fictitious key ARN with a valid one. String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest() { KeyId = keyId }; DescribeKeyResponse describeKeyResponse = kmsClient.DescribeKey(describeKeyRequest);

Getting Key IDs and Key ARNs of Customer Master Keys

To get the IDs and ARNs of the customer master keys, use the ListKeys operation.

This example uses the kmsClient client object that you created in Creating a Client.

JavaC#
Java

For details, see the listKeys method in the AWS SDK for Java API Reference.

// List CMKs in this account // Integer limit = 10; String marker = null; ListKeysRequest req = new ListKeysRequest().withMarker(marker).withLimit(limit); ListKeysResult result = kmsClient.listKeys(req);
C#

For details, see the ListKeys method in the AWS SDK for .NET.

// List CMKs in this account // int limit = 10; String marker = null; ListKeysRequest listKeysRequest = new ListKeysRequest() { Marker = marker, Limit = limit }; ListKeysResponse listKeysResponse = kmsClient.ListKeys(listKeysRequest);

Enabling Customer Master Keys

To enable a disabled customer master key (CMK), use the EnableKey operation.

This example uses the kmsClient client object that you created in Creating a Client.

JavaC#
Java

For details about the Java implementation, see the enableKey method in the AWS SDK for Java API Reference.

// Enable a CMK // String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; EnableKeyRequest req = new EnableKeyRequest().withKeyId(keyId); kmsClient.enableKey(req);
C#

For details, see the EnableKey method in the AWS SDK for .NET.

// Enable a CMK // String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; EnableKeyRequest enableKeyRequest = new EnableKeyRequest() { KeyId = keyId }; kmsClient.EnableKey(enableKeyRequest);

Disabling Customer Master Keys

To disable a CMK, use the DisableKey operation. Disabling a CMK prevents it from being used.

This example uses the kmsClient client object that you created in Creating a Client.

JavaC#
Java

For details, see the disableKey method in the AWS SDK for Java API Reference.

// Disable a CMK // String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DisableKeyRequest req = new DisableKeyRequest().withKeyId(keyId); kmsClient.disableKey(req);
C#

For details, see the EnableKey method in the AWS SDK for .NET.

// Disable a CMK // String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DisableKeyRequest disableKeyRequest = new DisableKeyRequest() { KeyId = keyId }; kmsClient.DisableKey(disableKeyRequest);