powershellcsvsecurestring

Array from a SecureString list


So I'm trying to decrypt a Csv that was securestring using a key and read it as an array, but when I decrypt it into plaintext using:

$unsec = $secure | convertfrom-securestring -asplaintext

it looks like several lines of this:

@{Num=123; RDM=abc}
@{Num=456; RDM=def}

What's the best way of decrypting the data into an array?

Thank you


Solution

  • You need to serialize the data before converting it to a SecureString otherwise you would end up with string representations of your objects when you convert it back to a plain string. You should use ConvertTo-Csv before ConvertTo-SecureString.

    Using Get-Process as an example:

    $csv = (Get-Process | Select-Object Name, Id | ConvertTo-Csv) -join [System.Environment]::NewLine
    $encrypted = $csv | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
    

    Now if you want to decrypt the $encrypted string you can:

    $encrypted | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText | ConvertFrom-Csv
    

    You should also be aware of the following limitation:

    The maximum length of a SecureString instance is 65,536 characters.