Decrypting Secure String

Product: PowerShell Universal
Version: 2.10.1

I am unable to decrypt a secure string as plain text. I get the following error:

[error] Key not valid for use in specified state.
[error] Exception calling "SecureStringToBSTR" with "1" argument(s): "Value cannot be null. Parameter name: s" 
[error] Cannot find an overload for "PtrToStringAuto" and the argument count: "1". 

I’ve tried both ConvertFrom-SecureString -AsPlainText and the following:

$decrypted = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$decryptedPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($decrypted)

Was the SecureString generated by the same user account? And where are you running this command? API\Script\Dashboard? Integrated vs PS Environment?

I just want to be able to accurately attempt to reproduce.

At first I tried in a dashboard, but when that didn’t work I tried it as a script. As a script I tried running both integrated and PS and it didn’t work. The logic works in PS outside of PSU.

It should be created with the same user, but let me do some double checking on my end.

It was using a different account. I updated my logic to use a key. Thanks for the point in the right direction.

1 Like