我正在尝试使用powershell上的aws cli加密和解密内容(不是PowerShell特定的,而是the standard one)
$input = "foo"
$file_path = "$(pwd)\file"
$region = "eu-west-1"
# ENCRYPT
$ciphertextblob =
aws kms encrypt `
--region $region `
--key-id "a266be0d-304b-4gf2-8b75-021ba4b0d23a" `
--plaintext $input |
ConvertFrom-Json |
Foreach-Object { $_.CiphertextBlob }
$encrypted = [System.Convert]::FromBase64String($ciphertextblob)
[io.file]::WriteAllBytes($file_path, $encrypted)
# DECRYPT
$decrypt =
aws kms decrypt `
--region $region `
--ciphertext-blob "fileb://$file_path"
# SHOW
$decrypt
,结果是
{
"Plaintext": "Zm9v",
"KeyId": "arn:aws:kms:eu-west-1:639530368848:key/a266be0d-304b-4gf2-8b75-021ba4b0d23a"
}
我必须在某个地方缺少编码...如果有人可以帮助我进步:-D
此致 巴尔
答案 0 :(得分:0)
我的错误是认为$ decrypt不是base64。
以下是完整的工作示例:
$input = "foo"
$file_path = "$(pwd)\file"
$region = "eu-west-1"
# ENCRYPT
$ciphertextblob =
aws kms encrypt `
--region $region `
--key-id "a266be0d-304b-4gf2-8b75-021ba4b0d23a" `
--plaintext $input |
ConvertFrom-Json |
Foreach-Object { $_.CiphertextBlob }
$encrypted = [System.Convert]::FromBase64String($ciphertextblob)
[io.file]::WriteAllBytes($file_path, $encrypted)
# DECRYPT
$decrypt_base64 =
aws kms decrypt `
--region $region `
--ciphertext-blob "fileb://$file_path" |
ConvertFrom-Json |
%{ $_.Plaintext }
$decrypt_plaintext = [System.Text.Encoding]::UTF8.GetString(
[System.Convert]::FromBase64String($decrypt_base64)
)
# SHOW
$decrypt_plaintext
预期的结果是:
foo