Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 help and support Forum Windows 8 Forum Vista Tutorials

Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Vista Newsgroup Archive > Misc Newsgroups > PowerShell

Vista - HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString


 
 
09-07-2006   #1 (permalink)
Brandon Shell


 

HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString

I know im just doing something stupid, but I cant seem to get this to work.
I assume it has something to do with type'ing. Im not a developer in any
stretch so please be patient, just and ol'school command/vbs scripter.



Example:
---------
$securestring = ConvertTo-SecureString "Hello"

I get Error:
-----------
ConvertTo-SecureString : Cannot process argument because the value of
argument "input" is invalid.
At line:1 char:39


My System SpecsSystem Spec
09-07-2006   #2 (permalink)
Lee Holmes [MSFT]


 

Re: HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString

For some background -- a SecureString is a type of string that PowerShell
(and .Net) keeps encrypted in memory. Even if an attacker can explore the
memory on your computer (like the contents of a swap file, for example,)
they cannot gain access to the secret protected by the SecureString.

Although you can pass around SecureStrings with impunity, applications must
be extremely careful at the boundaries -- when creating SecureStrings and
retrieving the encrypted data from them. This means doing things like
reading your password input character by character, then removing each
character from memory as soon as possible. If the data is ever stored as a
regular string, it stays in memory until the process exits.

By typing a regular string onto the command line (like you did below,) the
string can no longer be made secure. That specific string stays in memory
until PowerShell exits. This is why ConvertTo-SecureString only accepts the
encrypted output of ConvertFrom-SecureString. Only in that way can we
retain the security guarantee of SecureStrings.

That said, most people aren't that concerned about an attacker spying on
their machine's memory, or digging through their Windows pagefile. In many
situations, the benefit of being able to automate these situations vastly
outweights the potential security risk.

For the upcoming release candidate, we've added some new functionality to
allow this:

$secureString = ConvertTo-SecureString "Hello" -AsPlainText -Force

(The force flag lets you bypass the warning I just gave you )

Until then, you can create SecureStrings from plain text this way:

$text = "Hello World"
$secureString = new-object Security.SecureString
$text.ToCharArray() | % { $secureString.AppendChar($_) }

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.




"Brandon Shell" <tshell.mask@gmail.com> wrote in message
news:e8Wwg9q0GHA.1040@TK2MSFTNGP06.phx.gbl...
>I know im just doing something stupid, but I cant seem to get this to work.
>I assume it has something to do with type'ing. Im not a developer in any
>stretch so please be patient, just and ol'school command/vbs scripter.
>
> Example:
> ---------
> $securestring = ConvertTo-SecureString "Hello"
>
> I get Error:
> -----------
> ConvertTo-SecureString : Cannot process argument because the value of
> argument "input" is invalid.
> At line:1 char:39
>



My System SpecsSystem Spec
09-07-2006   #3 (permalink)
Brandon Shell


 

Re: HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString

Thanks... make sense... I think I just misunderstood the purpose.

I was looking to encrypt a text (Password of sorts) and store on disk or db,
so that I was the only one that could decrypt.

"Lee Holmes [MSFT]" <lee.holmes@online.microsoft.com> wrote in message
news:%23gcaYqr0GHA.4476@TK2MSFTNGP05.phx.gbl...
> For some background -- a SecureString is a type of string that PowerShell
> (and .Net) keeps encrypted in memory. Even if an attacker can explore the
> memory on your computer (like the contents of a swap file, for example,)
> they cannot gain access to the secret protected by the SecureString.
>
> Although you can pass around SecureStrings with impunity, applications
> must be extremely careful at the boundaries -- when creating SecureStrings
> and retrieving the encrypted data from them. This means doing things like
> reading your password input character by character, then removing each
> character from memory as soon as possible. If the data is ever stored as
> a regular string, it stays in memory until the process exits.
>
> By typing a regular string onto the command line (like you did below,) the
> string can no longer be made secure. That specific string stays in memory
> until PowerShell exits. This is why ConvertTo-SecureString only accepts
> the encrypted output of ConvertFrom-SecureString. Only in that way can we
> retain the security guarantee of SecureStrings.
>
> That said, most people aren't that concerned about an attacker spying on
> their machine's memory, or digging through their Windows pagefile. In
> many situations, the benefit of being able to automate these situations
> vastly outweights the potential security risk.
>
> For the upcoming release candidate, we've added some new functionality to
> allow this:
>
> $secureString = ConvertTo-SecureString "Hello" -AsPlainText -Force
>
> (The force flag lets you bypass the warning I just gave you )
>
> Until then, you can create SecureStrings from plain text this way:
>
> $text = "Hello World"
> $secureString = new-object Security.SecureString
> $text.ToCharArray() | % { $secureString.AppendChar($_) }
>
> --
> Lee Holmes [MSFT]
> Windows PowerShell Development
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
>
>
> "Brandon Shell" <tshell.mask@gmail.com> wrote in message
> news:e8Wwg9q0GHA.1040@TK2MSFTNGP06.phx.gbl...
>>I know im just doing something stupid, but I cant seem to get this to
>>work. I assume it has something to do with type'ing. Im not a developer in
>>any stretch so please be patient, just and ol'school command/vbs scripter.
>>
>> Example:
>> ---------
>> $securestring = ConvertTo-SecureString "Hello"
>>
>> I get Error:
>> -----------
>> ConvertTo-SecureString : Cannot process argument because the value of
>> argument "input" is invalid.
>> At line:1 char:39
>>

>
>



My System SpecsSystem Spec
09-08-2006   #4 (permalink)
Lee Holmes [MSFT]


 

Re: HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString

That is a perfect use of the cmdlets.

PS >$secureString = Read-Host -AsSecureString
************
PS >ConvertFrom-SecureString $secureString | out-file c:\temp\encrypted.txt
PS >$newString = gc C:\temp\encrypted.txt | ConvertTo-SecureString

Lee

"Brandon Shell" <tshell.mask@gmail.com> wrote in message
news:OunFzat0GHA.4116@TK2MSFTNGP02.phx.gbl...
> Thanks... make sense... I think I just misunderstood the purpose.
>
> I was looking to encrypt a text (Password of sorts) and store on disk or
> db, so that I was the only one that could decrypt.
>
> "Lee Holmes [MSFT]" <lee.holmes@online.microsoft.com> wrote in message
> news:%23gcaYqr0GHA.4476@TK2MSFTNGP05.phx.gbl...
>> For some background -- a SecureString is a type of string that PowerShell
>> (and .Net) keeps encrypted in memory. Even if an attacker can explore
>> the memory on your computer (like the contents of a swap file, for
>> example,) they cannot gain access to the secret protected by the
>> SecureString.
>>
>> Although you can pass around SecureStrings with impunity, applications
>> must be extremely careful at the boundaries -- when creating
>> SecureStrings and retrieving the encrypted data from them. This means
>> doing things like reading your password input character by character,
>> then removing each character from memory as soon as possible. If the
>> data is ever stored as a regular string, it stays in memory until the
>> process exits.
>>
>> By typing a regular string onto the command line (like you did below,)
>> the string can no longer be made secure. That specific string stays in
>> memory until PowerShell exits. This is why ConvertTo-SecureString only
>> accepts the encrypted output of ConvertFrom-SecureString. Only in that
>> way can we retain the security guarantee of SecureStrings.
>>
>> That said, most people aren't that concerned about an attacker spying on
>> their machine's memory, or digging through their Windows pagefile. In
>> many situations, the benefit of being able to automate these situations
>> vastly outweights the potential security risk.
>>
>> For the upcoming release candidate, we've added some new functionality to
>> allow this:
>>
>> $secureString = ConvertTo-SecureString "Hello" -AsPlainText -Force
>>
>> (The force flag lets you bypass the warning I just gave you )
>>
>> Until then, you can create SecureStrings from plain text this way:
>>
>> $text = "Hello World"
>> $secureString = new-object Security.SecureString
>> $text.ToCharArray() | % { $secureString.AppendChar($_) }
>>
>> --
>> Lee Holmes [MSFT]
>> Windows PowerShell Development
>> Microsoft Corporation
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>>
>>
>> "Brandon Shell" <tshell.mask@gmail.com> wrote in message
>> news:e8Wwg9q0GHA.1040@TK2MSFTNGP06.phx.gbl...
>>>I know im just doing something stupid, but I cant seem to get this to
>>>work. I assume it has something to do with type'ing. Im not a developer
>>>in any stretch so please be patient, just and ol'school command/vbs
>>>scripter.
>>>
>>> Example:
>>> ---------
>>> $securestring = ConvertTo-SecureString "Hello"
>>>
>>> I get Error:
>>> -----------
>>> ConvertTo-SecureString : Cannot process argument because the value of
>>> argument "input" is invalid.
>>> At line:1 char:39
>>>

>>
>>

>
>



My System SpecsSystem Spec
 

HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString problems?



Thread Tools


Similar topics to: HowTo: Use ConvertTo-SecureString and ConvertFrom-SecureString
Thread Forum
SecureString in Batch PowerShell
SecureString : Using -key PowerShell
Using SecureString objects PowerShell
ConvertTo-Html PowerShell
Convert/ConvertTo/ConvertFrom -> To/From? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50