I’d like to build a small script to show me the username and password by using a small key(4 Numbers – typin by User[Read-Host]).
My script is:
function pwdBSTR($pwd){
return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd))
}
IF ((pwdBSTR -pwd (Read-Host "Passwort eingeben:"-AsSecureString))-eq(pwdBSTR -pwd ("01000000d08c9ddf0115d1118c7a00c04fc297eb010000003bbea3bea3c1324b852946716386ed580000000002000000000003660000c000000010000000ab70bd8ef511fae1ae2e4de23cdc41fe0000000004800000a000000010000000c0c339f26dfe6d3085a5a64a5b1366c510000000b951ca954bd695276da97e4ebf8a5cc114000000fd1c191fb835d4fa5e934e34a5b90b685ae4f7f4"| ConvertTo-SecureString))){
Write-Host "Usernaem for web.de is [email protected]"
write-host "Password is HelloWorld!"
}
Is there a better way to handle the PW-Check?
5
If you want to use a Key to decrypt your password then you’re going the wrong way. In addition, using a Key you would not have a need to actually decrypt the password if you don’t want to.
To create the encrypted password using a Key you can start from, for example:
# NOTE: Valid secure key lengths are 8, 12 and 16
$key = ConvertTo-SecureString 'my16byteKey12345' -AsPlainText -Force
$passw = 'hello world'
$passw | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString -SecureKey $key
# Outputs: `76492d1116743f0423413....=`
Then once you have the encrypted string, instead of doing a equality comparison, you can just try
/ catch
when decrypting it. If the key is incorrect then you will have an error:
try {
$key = Read-Host 'Passwort eingeben' -AsSecureString
# here you can put the encrypted password you got from the previous step
# you can also read it from a file
$encryptedPwd = '76492d1116743f0423413....='
# next step, if the key is wrong the you go directly to the `catch` block
$securestring = ConvertTo-SecureString $encryptedPwd -SecureKey $key -ErrorAction Stop
# once you're here then the key was correct
# if you do want to decrypt the password for some reason the you can do:
[System.Net.NetworkCredential]::new('', $securestring).Password
# which would output `hello world` in this case
}
catch {
Write-Error 'Incorrect Key'
}
2