我试图使用AWS KMS加密文本并创建PowerShell脚本。所以我用New-KMSDataKey
来加密我的KMS主密钥,它在输出中返回plaintextDataKey
和ciphertextblob
。如何使用AWS Powershell脚本中的KMS密钥加密数据
我现在用的plaintextDataKey
使用Invoke-KMSEncrypt
加密我的明文,但我得到无效的操作错误,如下图所示:
下面是我的脚本:
param([string]$zonesecret, [string]$KMSKey, [string]$Keyspec, [string]$region= 'us-east-1', [string]$AccessKey, [string]$SecretKey)
# splat
$splat = @{KeyId=$KMSKey; KeySpec=$Keyspec; Region=$region}
# generate a data key
$datakey = New-KMSDataKey @splat
$plaintextDataKey = [Convert]::ToBase64String($datakey.Plaintext.ToArray())
$encryptedDataKey = [Convert]::ToBase64String($datakey.CiphertextBlob.ToArray())
Write-Host $plaintextDataKey
Write-Host $encryptedDataKey
#encrypt using aes-256; pass zonesecret and plaintextDataKey
# memory stream
[byte[]]$byteArray = [System.Text.Encoding]::UTF8.GetBytes($zonesecret)
$memoryStream = New-Object System.IO.MemoryStream($byteArray,0,$byteArray.Length)
$splat = @{Plaintext=$memoryStream; KeyId=$plaintextDataKey; Region=$region;}
if(![string]::IsNullOrEmpty($AccessKey)){$splat += @{AccessKey=$AccessKey;}}
if(![string]::IsNullOrEmpty($SecretKey)){$splat += @{SecretKey=$SecretKey;}}
# encrypt
**$encryptedMemoryStream = Invoke-KMSEncrypt @splat** # ERROR:
Write-Host $encryptedMemoryStream
$base64encrypted = [System.Convert]::ToBase64String($encryptedMemoryStream.CiphertextBlob.ToArray())
Write-Host $base64encrypted
能有什么我确实做对了吗?我在这里做错了什么?这里没有其他cmdlet来加密数据:http://docs.aws.amazon.com/powershell/latest/reference/Index.html
任何人都可以请帮忙吗?我如何使用上面的明文数据密钥来加密我的内容?