Mã hóa trực tiếp với AWS KMS

Mã hóa trực tiếp với AWS KMS

Như đã biết, chúng ta có thể sử dung CMK để mã hóa dữ liệu một cách trực tiếp mà không cần tới Data key, tuy nhiên lượng dữ liệu có thể mã hóa mỗi lần chỉ khoảng 4Kb.
Trong một số trường hợp, mã hóa trực tiếp vẫn mang lại lợi ích ở một khía cạnh nào đó. Do vậy sẽ rất cần thiết nếu chúng ta biết cách thực hiện mã hóa dữ liệu một cách trực tiếp, sử dụng CMK mà không dùng Data key.

  1. Chạy lệnh dưới đây để thực hiện tạo file text NewSecretFile.txt.
echo "New secret text" > NewSecretFile.txt

Encryption using AWS KMS with no Data Key 2. Chạy lệnh dưới đây để thực hiện Mã hóa file text NewSecretFile.txt.

aws kms encrypt --key-id alias/ImportedCMK --plaintext fileb://NewSecretFile.txt --encryption-context project=kmsworkshop --output text  --query CiphertextBlob | base64 --decode > NewSecretsEncryptedFile.txt

Encryption using AWS KMS with no Data Key 3. Chạy lệnh dưới đây để kiểm tra file text sau khi đã đc Mã hóa.

cat NewSecretsEncryptedFile.txt

Encryption using AWS KMS with no Data Key 4. Chạy lệnh dưới đây để thực hiện decrypt file text, sử dụng CMK trước đó đã dùng để mã hóa.

aws kms decrypt --ciphertext-blob fileb://NewSecretsEncryptedFile.txt --encryption-context project=kmsworkshop --output text --query Plaintext | base64 --decode > NewSecretsDecryptedFile.txt

Encryption using AWS KMS with no Data Key 5. Chạy lệnh dưới đây để kiểm tra kết quả. Ta có thể đọc được nội dung file.

cat NewSecretsDecryptedFile.txt

Encryption using AWS KMS with no Data Key