首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SecureString与SecureKey问题

SecureString与SecureKey问题
EN

Stack Overflow用户
提问于 2016-03-30 08:44:29
回答 1查看 3.1K关注 0票数 0

我有下面的代码,工作没有任何错误。

  1. SaveKeyPass.ps1正在存储安全密钥(加密)和密码(使用securekey加密)
  2. GetKeyPass.ps1从文件中获取安全密钥和密码,然后解密安全密钥,最后使用解密的securekey解密密码。

SaveKeyPass.ps1

代码语言:javascript
复制
$key = "1234567891234567" 
$textPassword = "securekey-textpassword"
$securePassword = ConvertTo-SecureString $textPassword -AsPlainText -Force
$secureKey = ConvertTo-SecureString $Key -AsPlainText -Force
$encryptedKey = ConvertFrom-SecureString $SecureKey -Key (1..16)
$encryptedPassword = ConvertFrom-SecureString  $SecurePassword -SecureKey $decryptedSecureKeyFromFile
$encryptedKey | Out-File "C:\temp\securekey-enckey.txt"
$encryptedPassword | Out-File "C:\temp\securekey-encpass.txt"


Write-Host "Key: $Key"
Write-Host "Text Password: $textPassword"
Write-Host "Encrypted Password: $encryptedPassword"
Write-Host "Encrypted Key: $encryptedKey"

GetKeyPass.ps1

代码语言:javascript
复制
$key = ""
$textPassword = ""
$encryptedPasswordFromFile = ""
$encryptedKeyFromFile = ""
$secureDecryptedPassword = ""
$BSTR1= ""
$BSTR2= ""
$encryptedKeyFromFile = Get-Content "C:\temp\securekey-enckey.txt"
$encryptedPasswordFromFile = Get-Content "C:\temp\securekey-encpass.txt"
$secureDecryptedKey = ConvertTo-SecureString $encryptedKeyFromFile -Key (1..16)
$secureDecryptedPassword = ConvertTo-SecureString  $encryptedPasswordFromFile -SecureKey $secureDecryptedKey


$BSTR1 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedPassword)
$textPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR1)

$BSTR2 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureDecryptedKey)
$key = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR2)


Write-Host "Key: $key"
Write-Host "Text Password: $textPassword"
Write-Host "Encrypted Password: $encryptedPasswordFromFile"
Write-Host "Encrypted Key: $encryptedKeyFromFile"

第1期:

如果我将SaveKeyPass.ps1中的第一行(仅最后一个数字从7更改为8)改为并执行以下脚本 $key = "1234567891234568“并随后执行GetKeyPass.ps1 --我得到此错误 转换到-SecureString:填充无效,不能删除。地址:**:11 char:28

第2期:

如果我将SaveKeyPass.ps1中的第一行(键长从16字节更改为32字节)改为并执行此脚本 $key =“1234567891234567123453458912345667”并随后执行GetKeyPass.ps1 I得到此错误 指定的键无效。有效的密钥长度设置为128位、192位或256位。地址:**:11 char:28

我真的不知道这是怎么回事?在问题1中,只更改了一位数,因此不确定从何处抛出填充异常。在问题2中,我有32字节(256位)密钥,但异常是抱怨密钥长度不正确。

任何帮助都将不胜感激。感谢您的阅读!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 09:28:59

谢谢马丁和贾里德的现场报道,我已经更正了SaveKeyPass.ps1中的第11行

代码语言:javascript
复制
$encryptedPassword = ConvertFrom-SecureString  $SecurePassword -SecureKey $secureKey

它解决了问题1、完全问题2部分。关于第2期:

我注意到,1字符/数字的键是16位(可能在我的64位机器上),这意味着“123456712345345345678912345345345345345345345345667”是512位,而不是256位,我认为1字符/位是8字节。因此,这违反了键的最大长度要求,并失败了。

这意味着如果我提供8,12,16个字符,它们分别是128位,192位和256位。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36303829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档