【问题标题】:aws cli: aws kms encrypt/decrypt using powershellaws cli:aws kms 使用 powershell 加密/解密
【发布时间】:2017-08-13 02:20:59
【问题描述】:

我正在尝试使用 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"
}

如你所见:

  • 我定义了一个输入“foo”,最后变成“Zm9v”
  • “Zm9v”不是base64
  • 我获取 encrypt 命令结果,从 JSON 转换为 Powershell 对象,然后获取 CiphertextBlob
  • 我将其从 base64 解码为纯文本,并使用 WriteAllBytes 将其写入二进制文件
  • 最后我用fileb用decrypt命令读取二进制文件

看来我的问题是:

我必须在某处缺少编码......如果有人可以帮助我进步:-D

问候, 蒂博

【问题讨论】:

    标签: powershell aws-cli aws-kms


    【解决方案1】:

    我的错误是认为 $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
    

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      相关资源
      最近更新 更多