【问题标题】:powershell: importing csv as an array of bytespowershell:将csv作为字节数组导入
【发布时间】:2014-01-24 16:55:45
【问题描述】:

我正在尝试为在多台机器上运行的脚本创建存储的凭据。我可以让它在我的机器上正常工作,但我知道如果我想在其他计算机上运行它,我需要一个用于 convertto-securestring 的自定义密钥。我将密钥保存为 csv 和 .txt。 问题是,当我尝试将密钥从文件读取到变量并将其与 ConvertTo-SecureString 一起使用时,它会出错。是否可以获取 csv 并将其作为字节数组保存到变量中?

$encrypted = get-content .\Documents\powershell\encrypted_pass1.txt | ConvertTo-SecureString -key $key

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这就是我所做的。为了创建我的密钥,我使用了 system.security.cryptography.rngcryptoserviceprovider::create() 方法。 以及set-content和get-content的-encoding字节参数。 代码示例如下。

    #Generate encryption key.
    $key = New-Object byte[](32)
    $rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
    $rng.GetBytes($key)
    set-content -path ./key.txt -value $key -encoding byte
    

    并解密并使用它..

    $decryptkey = get-content -Encoding byte -Path .\key.txt
    $encrypted = get-content .\securepassword.txt | ConvertTo-SecureString -key $decryptkey
    $decryptptr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($encrypted)
    $decrypted = [Runtime.InteropServices.Marshal]::PtrToStringAuto($decryptptr)
    

    希望对您有所帮助。

    -丰富

    【讨论】:

    • 哎呀,解密代码被抬高了。我再试一次。看看这看起来是否正确。
    【解决方案2】:

    试试这个:

    $Encrypted = Get-Content -Encoding Byte -Path .\Documents\PowerShell\encrypted_pass1.txt;
    

    【讨论】:

    • 嘿特雷弗,感谢您的意见 - 我认为我的问题措辞不佳,但我理解您的回答及其相关性。它提出了另一个问题,但这可能是问题的核心:如何将字节数组保存到文件中?例如,如果我想将 192 位密钥保存到文件中以供其他机器使用。我认为这是我的主要问题所在 - 我试图将其保存为 txt 文件
    • “关键”只是文本,对吧?使用Set-Content cmdlet 将信息保存到文件中。 Set-Content -Path c:\key.txt -Value 'mykeyvalue';
    • 这是我在 key.txt 中的示例 165,21,193,254,46,198,192,217,116,204,44,240,36,48,80,118,208,68,32,157,190,35,102,70 我可以将此手动保存为变量 $key = 165,21,...70,然后使用 convertto-SecureString -key $key 重用它
    猜你喜欢
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多