Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I tell a string is an encrypted secure string or plain text in powershell?

Suppose I have a string:

"password"

And it may be in the form of an encrypted secure string converted to text. This secure string text may be decrypted via ConvertFrom-SecureString to recover the "password":

"1213132131....1232131" | ConvertFrom-SecureString |%{echo $_}
password

Is there any way to have powershell tell me whether the input string is already decrypted?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Or do I need to devise some other way to tell whether the input string is in plain text or encrypted as a secure string?

>Solution :

Caveat:


You can infer whether the string is encrypted or not based on whether applying ConvertTo-SecureString to it succeeds or not:

# Sample input string, already decrypted.
$str = 'hello'

$isEncrypted = 
  $null -ne (
    $secureString = try { $str | ConvertTo-SecureString -ErrorAction Stop } 
                    catch { }
  )

If the string is encrypted – i.e. if it is the serialized form of a [securestring] instance obtained with ConvertFrom-SecureString$secureString is assigned the deserialized form (i.e. a [securestring] instance).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading