sudo echo "Sample Secret Text to Encrypt" > samplesecret.txt
2. Cấp thêm quyền cho Instance để thực hiện tạo key
kms
vào ô tìm kiếm.GenerateDataKey
vào ô tìm kiếmKMSWorkshop-AdditionalPermissions
KMSWorkshop-AdditionalPermissions
vào ô tìm kiếm, nhấn Enteraws kms generate-data-key --key-id alias/ImportedCMK --key-spec AES_256 --encryption-context project=workshop
echo '<Plantext value of the JSON file in step 11>' | base64 --decode > datakeyPlainText.txt
echo '<CipherTextBlob value of the JSON file in step 11>' | base64 --decode > datakeyEncrypted.txt
13. Chạy lệnh dưới đây để mã hóa file Text samplesecret.txt chúng ta đã tạo trước đó. Đầu ra thu được là file encryptedSecret.txt với nội dung đã được mã hóa AES256.
openssl enc -e -aes256 -in samplesecret.txt -out encryptedSecret.txt -k fileb://datakeyPlainText.txt
14. Chạy lệnh dưới đây để kiểm tra nội dung file Text sau khi mã hóa
openssl enc -e -aes256 -in samplesecret.txt -out encryptedSecret.txt -k fileb://datakeyPlainText.txt
15. Chạy lệnh dưới đây để thực hiện xóa file Data key dạng Plain text và chỉ giữ lại file Data key dạng Encrypted, đảm bảo best pratice.
rm datakeyPlainText.txt
Theo best practice về bảo mật dữ liệu, sau khi mã hóa dữ liệu ta sẽ phải xóa Data Key dạng Plain Text. Do vậy khi cần Giải mã file Text đã được Encrypted, thì phải Giải mã file Data key dạng Encrypted nằm trong file datakeyEncrypted.txt để có đc Data key dạng Plain text.
aws kms decrypt --encryption-context project=workshop --ciphertext-blob fileb://datakeyEncrypted.txt
17. Chạy lệnh dưới đây để thực hiện decode thông tin Data key mã b64 để có được file Data Key dạng Plain text hoàn chỉnh
echo '<Plantext value of the JSON file in step 16>' | base64 --decode > datakeyPlainText.txt
18. Chạy lệnh dưới đây để thực hiện decrypt file Text đã được mã hóa sử dụng thư viện OpenSSL với file Data Key dạng Plain text
openssl enc -d -aes256 -in encryptedSecret.txt -k fileb://datakeyPlainText.txt
19. Chạy lệnh dưới đây để thực hiện xóa file Data key dạng Plain text và chỉ giữ lại file Data key dạng Encrypted, đảm bảo best pratice.
rm datakeyPlainText.txt