Mengenkripsi dan mendekripsi kunci data - AWS Key Management Service

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Mengenkripsi dan mendekripsi kunci data

Contoh dalam topik ini menggunakan Enkripsi, Dekripsi, dan ReEncryptoperasi di API. AWS KMS

Operasi ini dirancang untuk mengenkripsi dan mendekripsi kunci data. Mereka menggunakan a AWS KMS keysdalam operasi enkripsi dan mereka tidak dapat menerima lebih dari 4 KB (4096 byte) data. Meskipun Anda mungkin menggunakannya untuk mengenkripsi sejumlah kecil data, seperti kata sandi atau kunci RSA, operasi tersebut tidak dirancang untuk mengenkripsi data aplikasi.

Untuk mengenkripsi data aplikasi, gunakan fitur enkripsi sisi server dari layanan AWS, atau pustaka enkripsi sisi klien, seperti AWS Encryption SDK atau klien enkripsi Amazon S3.

Mengenkripsi kunci data

Operasi Encrypt dirancang untuk mengenkripsi kunci data, tetapi tidak sering digunakan. GenerateDataKeyWithoutPlaintextOperasi GenerateDataKeydan mengembalikan kunci data terenkripsi. Anda dapat menggunakan metode ini ketika Anda memindahkan data terenkripsi ke Wilayah yang berbeda dan ingin mengenkripsi kunci datanya dengan kunci KMS di Wilayah baru.

Dalam bahasa yang memerlukan objek klien, contoh-contoh ini menggunakan objek klien AWS KMS yang Anda buat di Membuat klien.

Java

Untuk detailnya, lihat metode encrypt di Referensi API AWS SDK for Java.

// Encrypt a data key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; ByteBuffer plaintext = ByteBuffer.wrap(new byte[]{1,2,3,4,5,6,7,8,9,0}); EncryptRequest req = new EncryptRequest().withKeyId(keyId).withPlaintext(plaintext); ByteBuffer ciphertext = kmsClient.encrypt(req).getCiphertextBlob();
C#

Untuk detailnya, lihat metode Encrypt di AWS SDK for .NET.

// Encrypt a data key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; MemoryStream plaintext = new MemoryStream(); plaintext.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, 0, 10); EncryptRequest encryptRequest = new EncryptRequest() { KeyId = keyId, Plaintext = plaintext }; MemoryStream ciphertext = kmsClient.Encrypt(encryptRequest).CiphertextBlob;
Python

Untuk detailnya, lihat metode enkripsi di AWS SDK for Python (Boto3).

# Encrypt a data key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' plaintext = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00' response = kms_client.encrypt( KeyId=key_id, Plaintext=plaintext ) ciphertext = response['CiphertextBlob']
Ruby

Untuk detailnya, lihat metode instans enkripsi di AWS SDK for Ruby.

# Encrypt a data key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' plaintext = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00" response = kmsClient.encrypt({ key_id: key_id, plaintext: plaintext }) ciphertext = response.ciphertext_blob
PHP

Untuk detailnya, lihat metode Encrypt di AWS SDK for PHP.

// Encrypt a data key // // Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $message = pack('c*',1,2,3,4,5,6,7,8,9,0); $result = $KmsClient->encrypt([ 'KeyId' => $keyId, 'Plaintext' => $message, ]); $ciphertext = $result['CiphertextBlob'];
Node.js

Untuk detailnya, lihat properti enkripsi di AWS SDK untuk JavaScript di Node.js.

// Encrypt a data key // // Replace the following example key ARN with any valid key identfier const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const Plaintext = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); kmsClient.encrypt({ KeyId, Plaintext }, (err, data) => { if (err) console.log(err, err.stack); // an error occurred else { const { CiphertextBlob } = data; ... } });
PowerShell

Untuk mengenkripsi kunci data di bawah kunci KMS, gunakan cmdlet Invoke-KMSENcrypt. Ia mengembalikan ciphertext sebagai MemoryStream (System.io. MemoryStream) objek. Anda dapat menggunakan objek MemoryStream sebagai input ke cmdlet Invoke-KMSDecrypt.

AWS KMS juga mengembalikan kunci data sebagai objek MemoryStream. Dalam contoh ini, untuk mensimulasikan kunci data teks biasa, kami membuat array byte dan menulisnya ke objek MemoryStream.

Perhatikan bahwa parameter Plaintext dari Invoke-KMSEncrypt mengambil array byte (byte[]); parameter itu tidak memerlukan objek MemoryStream. Dimulai pada AWSPowerShell versi 4.0, parameter di semua AWSPowerShell modul yang mengambil array byte dan MemoryStream objek menerima array byte, MemoryStream objek, string, array string, dan (System.io. FileInfo FileInfo) objek. Anda dapat meneruskan salah satu dari jenis ini ke Invoke-KMSEncrypt.

# Encrypt a data key # Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' # Simulate a data key # Create a byte array [byte[]] $bytes = 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 # Create a MemoryStream $plaintext = [System.IO.MemoryStream]::new() # Add the byte array to the MemoryStream $plaintext.Write($bytes, 0, $bytes.length) # Encrypt the simulated data key $response = Invoke-KMSEncrypt -KeyId $keyId -Plaintext $plaintext # Get the ciphertext from the response $ciphertext = $response.CiphertextBlob

Untuk menggunakan AWS KMS PowerShell cmdlet, instal AWS.tools. KeyManagementServicemodul. Untuk informasi selengkapnya, lihat Panduan Pengguna AWS Tools for Windows PowerShell.

Mendekripsi kunci data

Untuk mendekripsi kunci data, gunakan operasi Decrypt.

ciphertextBlobYang Anda tentukan harus berupa nilai CiphertextBlob bidang dari GenerateDataKey, GenerateDataKeyWithoutPlaintext, atau Enkripsi respons, atau PrivateKeyCiphertextBlob bidang dari GenerateDataKeyPairWithoutPlaintextrespons GenerateDataKeyPairatau. Anda juga dapat menggunakan Decrypt operasi untuk mendekripsi data yang dienkripsi di luar AWS KMS oleh kunci publik dalam kunci KMS asimetris.

KeyIdParameter tidak diperlukan saat mendekripsi dengan kunci KMS enkripsi simetris. AWS KMSbisa mendapatkan kunci KMS yang digunakan untuk mengenkripsi data dari metadata di gumpalan ciphertext. Tetapi selalu merupakan praktik terbaik untuk menentukan kunci KMS yang Anda gunakan. Praktik ini memastikan bahwa Anda menggunakan kunci KMS yang dimaksud, dan mencegah Anda mendekripsi ciphertext secara tidak sengaja menggunakan kunci KMS yang tidak Anda percayai.

Dalam bahasa yang memerlukan objek klien, contoh-contoh ini menggunakan objek klien AWS KMS yang Anda buat di Membuat klien.

Java

Untuk detailnya, lihat metode dekripsi di Referensi API AWS SDK for Java.

// Decrypt a data key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; ByteBuffer ciphertextBlob = Place your ciphertext here; DecryptRequest req = new DecryptRequest().withCiphertextBlob(ciphertextBlob).withKeyId(keyId); ByteBuffer plainText = kmsClient.decrypt(req).getPlaintext();
C#

Untuk detailnya, lihat Metode Decrypt di AWS SDK for .NET.

// Decrypt a data key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; MemoryStream ciphertextBlob = new MemoryStream(); // Write ciphertext to memory stream DecryptRequest decryptRequest = new DecryptRequest() { CiphertextBlob = ciphertextBlob, KeyId = keyId }; MemoryStream plainText = kmsClient.Decrypt(decryptRequest).Plaintext;
Python

Untuk detailnya, lihat metode dekripsi di AWS SDK for Python (Boto3).

# Decrypt a data key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' ciphertext = 'Place your ciphertext here' response = kms_client.decrypt( CiphertextBlob=ciphertext, KeyId=key_id ) plaintext = response['Plaintext']
Ruby

Untuk detailnya, lihat metode instans dekripsi di AWS SDK for Ruby.

# Decrypt a data key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' ciphertext = 'Place your ciphertext here' ciphertext_packed = [ciphertext].pack("H*") response = kmsClient.decrypt({ ciphertext_blob: ciphertext_packed, key_id: key_id }) plaintext = response.plaintext
PHP

Untuk detailnya, lihat Metode Decrypt di AWS SDK for PHP.

// Decrypt a data key // // Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $ciphertext = 'Place your cipher text blob here'; $result = $KmsClient->decrypt([ 'CiphertextBlob' => $ciphertext, 'KeyId' => $keyId, ]); $plaintext = $result['Plaintext'];
Node.js

Untuk detailnya, lihat properti dekripsi di AWSSDK untuk JavaScript di Node.js.

// Decrypt a data key // // Replace the following example key ARN with any valid key identfier const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const CiphertextBlob = 'Place your cipher text blob here'; kmsClient.decrypt({ CiphertextBlob, KeyId }, (err, data) => { if (err) console.log(err, err.stack); // an error occurred else { const { Plaintext } = data; ... } });
PowerShell

Untuk mendekripsi kunci data, gunakan cmdlet Invoke-KMSEncrypt.

Cmdlet ini mengembalikan plaintext sebagai (System.io. MemoryStream MemoryStream) objek. Untuk mengonversinya menjadi array byte, gunakan cmdlet atau fungsi yang mengonversi objek MemoryStream ke array byte, seperti fungsi dalam modul Konversi.

Karena contoh ini menggunakan ciphertext yang dikembalikan oleh cmdlet enkripsi AWS KMS, ia menggunakan objek MemoryStream untuk nilai parameter CiphertextBlob. Namun, parameter CiphertextBlob dari Invoke-KMSDecrypt mengambil array byte (byte[]); parameter tidak memerlukan objek MemoryStream. Dimulai pada AWSPowerShell versi 4.0, parameter di semua AWSPowerShell modul yang mengambil array byte dan MemoryStream objek menerima array byte, MemoryStream objek, string, array string, dan (System.io. FileInfo FileInfo) objek. Anda dapat meneruskan salah satu dari jenis ini ke Invoke-KMSDecrypt.

# Decrypt a data key # Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' [System.IO.MemoryStream]$ciphertext = Read-Host 'Place your cipher text blob here' $response = Invoke-KMSDecrypt -CiphertextBlob $ciphertext -KeyId $keyId $plaintext = $response.Plaintext

Untuk menggunakan AWS KMS PowerShell cmdlet, instal AWS.tools. KeyManagementServicemodul. Untuk informasi selengkapnya, silakan lihat Panduan Pengguna AWS Tools for Windows PowerShell.

Mengenkripsi ulang kunci data di bawah yang berbeda AWS KMS key

Untuk mendekripsi kunci data terenkripsi, dan kemudian segera mengenkripsi ulang kunci data di bawah yang berbeda, gunakan operasi. AWS KMS key ReEncrypt Operasi dilakukan sepenuhnya di sisi server dalam AWS KMS, jadi mereka tidak pernah mengekspos plaintext Anda di luar AWS KMS.

ciphertextBlobYang Anda tentukan harus berupa nilai CiphertextBlob bidang dari GenerateDataKey, GenerateDataKeyWithoutPlaintext, atau Enkripsi respons, atau PrivateKeyCiphertextBlob bidang dari GenerateDataKeyPairWithoutPlaintextrespons GenerateDataKeyPairatau. Anda juga dapat menggunakan ReEncrypt operasi untuk mengenkripsi ulang data yang dienkripsi di luar AWS KMS oleh kunci publik dalam kunci KMS asimetris.

SourceKeyIdParameter tidak diperlukan saat mengenkripsi ulang dengan kunci KMS enkripsi simetris. AWS KMSbisa mendapatkan kunci KMS yang digunakan untuk mengenkripsi data dari metadata di gumpalan ciphertext. Tetapi selalu merupakan praktik terbaik untuk menentukan kunci KMS yang Anda gunakan. Praktik ini memastikan bahwa Anda menggunakan kunci KMS yang dimaksud, dan mencegah Anda mendekripsi ciphertext secara tidak sengaja menggunakan kunci KMS yang tidak Anda percayai.

Dalam bahasa yang memerlukan objek klien, contoh-contoh ini menggunakan objek klien AWS KMS yang Anda buat di Membuat klien.

Java

Untuk detailnya, lihat metode reEncrypt di Referensi API AWS SDK for Java.

// Re-encrypt a data key ByteBuffer sourceCiphertextBlob = Place your ciphertext here; // Replace the following example key ARNs with valid key identfiers String sourceKeyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String destinationKeyId = "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321"; ReEncryptRequest req = new ReEncryptRequest(); req.setCiphertextBlob(sourceCiphertextBlob); req.setSourceKeyId(sourceKeyId); req.setDestinationKeyId(destinationKeyId); ByteBuffer destinationCipherTextBlob = kmsClient.reEncrypt(req).getCiphertextBlob();
C#

Untuk detailnya, lihat ReEncrypt metode di AWS SDK for .NET.

// Re-encrypt a data key MemoryStream sourceCiphertextBlob = new MemoryStream(); // Write ciphertext to memory stream // Replace the following example key ARNs with valid key identfiers String sourceKeyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String destinationKeyId = "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321"; ReEncryptRequest reEncryptRequest = new ReEncryptRequest() { CiphertextBlob = sourceCiphertextBlob, SourceKeyId = sourceKeyId, DestinationKeyId = destinationKeyId }; MemoryStream destinationCipherTextBlob = kmsClient.ReEncrypt(reEncryptRequest).CiphertextBlob;
Python

Untuk detailnya, lihat re_encryptmetode diAWS SDK for Python (Boto3).

# Re-encrypt a data key ciphertext = 'Place your ciphertext here' # Replace the following example key ARNs with valid key identfiers source_key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' destination_key_id = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321' response = kms_client.re_encrypt( CiphertextBlob=ciphertext, SourceKeyId=source_key_id, DestinationKeyId=destination_key_id ) destination_ciphertext_blob = response['CiphertextBlob']
Ruby

Untuk detailnya, lihat metode re_encryptinstance di AWS SDK for Ruby.

# Re-encrypt a data key ciphertext = 'Place your ciphertext here' ciphertext_packed = [ciphertext].pack("H*") # Replace the following example key ARNs with valid key identfiers source_key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' destination_key_id = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321' response = kmsClient.re_encrypt({ ciphertext_blob: ciphertext_packed, source_key_id: source_key_id, destination_key_id: destination_key_id }) destination_ciphertext_blob = response.ciphertext_blob.unpack('H*')
PHP

Untuk detailnya, lihat ReEncryptmetode di AWS SDK for PHP.

// Re-encrypt a data key $ciphertextBlob = 'Place your ciphertext here'; // Replace the following example key ARNs with valid key identfiers $sourceKeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $destinationKeyId = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321'; $result = $KmsClient->reEncrypt([ 'CiphertextBlob' => $ciphertextBlob, 'SourceKeyId' => $sourceKeyId, 'DestinationKeyId' => $destinationKeyId, ]);
Node.js

Untuk detailnya, lihat properti ReEncrypt di SDK AWS JavaScript untuk di Node.js.

// Re-encrypt a data key const CiphertextBlob = 'Place your cipher text blob here'; // Replace the following example key ARNs with valid key identfiers const SourceKeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const DestinationKeyId = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321'; kmsClient.reEncrypt({ CiphertextBlob, SourceKeyId, DestinationKeyId }, (err, data) => { ... });
PowerShell

Untuk mengenkripsi ulang ciphertext di bawah kunci KMS yang sama atau berbeda, gunakan cmdlet Invoke-KMS. ReEncrypt

Karena contoh ini menggunakan ciphertext yang dikembalikan oleh cmdlet enkripsi AWS KMS, ia menggunakan objek MemoryStream untuk nilai parameter CiphertextBlob. Namun, parameter CiphertextBlob dari Invoke-KMSReEncrypt mengambil array byte (byte[]); parameter tidak memerlukan objek MemoryStream. Dimulai pada AWSPowerShell versi 4.0, parameter di semua AWSPowerShell modul yang mengambil array byte dan MemoryStream objek menerima array byte, MemoryStream objek, string, array string, dan (System.io. FileInfo FileInfo) objek. Anda dapat meneruskan salah satu dari jenis ini ke Invoke-KMSReEncrypt.

# Re-encrypt a data key [System.IO.MemoryStream]$ciphertextBlob = Read-Host 'Place your cipher text blob here' # Replace the following example key ARNs with valid key identfiers $sourceKeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $destinationKeyId = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321' $response = Invoke-KMSReEncrypt -Ciphertext $ciphertextBlob -SourceKeyId $sourceKeyId -DestinationKeyId $destinationKeyId $reEncryptedCiphertext = $response.CiphertextBlob

Untuk menggunakan AWS KMS PowerShell cmdlet, instal AWS.tools. KeyManagementServicemodul. Untuk informasi selengkapnya, lihat Panduan Pengguna AWS Tools for Windows PowerShell.