我正在使用 AWS Powershell cmdlet New-KMSDataKey创建一个 System.IO.MemoryStream其中包含我需要用来加密某些文件的加密 key 。

这是该命令的文档:

http://docs.aws.amazon.com/powershell/latest/reference/items/New-KMSDataKey.html



这是该 cmdlet 返回的对象:

http://docs.aws.amazon.com/sdkfornet/latest/apidocs/items/TKeyManagementServiceGenerateDataKeyResult_NET3_5.html



我正在尝试获取 plaintext属性(property)。我如何访问 System.IO.MemoryStream拿到 key ?

这是我的脚本示例:

$KMSKeyS3 = New-KMSDataKey -KeyId $KMSKeySource -KeySpec AES_256 -Region "ap-southeast-2" 

这给了我:
CiphertextBlob           KeyId                                           Plaintext                                                    
--------------           -----                                           --------- 
System.IO.MemoryStream   arn:aws:kms:ap-southeast-2:<Customer>:key/<Key> System.IO.MemoryStream 

请您参考如下方法:

简而言之,

# generate a data key 
$KMSKeyS3 = New-KMSDataKey -KeyId $KMSKeySource -KeySpec AES_256 -Region "ap-southeast-2" 
 
[byte[]]$plaintextDataKey = $KMSKeyS3.Plaintext.ToArray() 
[byte[]]$encryptedDataKey = $KMSKeyS3.CiphertextBlob.ToArray() 
[string]$encryptedDatakeyBase64 = $([Convert]::ToBase64String($encryptedDataKey)) 

this answer to a question on PowerShell and KMS获得全面的答案,包括经过测试的加密和解密脚本以及 base64 转换。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!