Credentials
Exploring Credentials
$c = get-credential
$c.UserName
$c.Password
$c | gm
Savings Credentials
$c.Password | ConvertFrom-SecureString
$c.Password | ConvertFrom-SecureString | Set-Content C:\dev\mypass.txt
Rereading Credentials
Get-Content C:\dev\mypass.txt
$securestring = Get-Content C:\dev\mypass.txt | ConvertTo-SecureString
$securestring
Good to remember In Memory : SecureString -> ConvertFrom-SecureString -> Encrypted String Encrypted String -> ConvertTo-SecureString -> Securestring (in Memory)
get-help ConvertFrom-SecureString
Securing it with a key
$key = new-object byte[] 24
$keystring = (Read-Host -Prompt "Your encryption key").ToCharArray()
for($i=0;$i -lt 24;$i++) {
$key[$i] = $keystring[$i%$keystring.length]
}
$securestring | ConvertFrom-SecureString -Key $key
$securestring | ConvertFrom-SecureString -Key $key | ConvertTo-SecureString -key $key
Reassembeling Credentials
[System.Management.Automation.PSCredential]::new #check constructor
[System.Management.Automation.PSCredential]::new("administrator",$securestring)
Unsafe password creation
ConvertTo-SecureString -AsPlainText "helloworld" # powershell doesnt like
$convsec = ConvertTo-SecureString -AsPlainText -force "helloworld"
$convsec
[System.Management.Automation.PSCredential]::new("administrator",$convsec)
Anyway it is not that secure
$convsec = ConvertTo-SecureString -AsPlainText -force "helloworld"
$cred = [System.Management.Automation.PSCredential]::new("administrator",$convsec)
$cred.GetNetworkCredential().password