0

I am trying to get the key this key for an assignment I was provided with the key and hash. In PowerShell how do I convert this to plain text. Any guidance is appreciated.

I have tried these

PS C:\test> $PlainPassword = Get-Content C:\test\secure-hash.txt

PS C:\test> $SecurePassword = ConvertTo-SecureString $PlainPassword -AsPlainText -Force

PS C:\test> 

I've tried ConvertFrom-SecureString input hash with no success

4
  • If you're using a Key then it's missing an argument Dec 28, 2022 at 2:55
  • What I am missing? I was provided with the key and the hash but I am new to powershell So I am not sure what I am missing. Can you please provide guidance?
    – AliONE
    Dec 28, 2022 at 2:58
  • I can show you the process of exporting and importing a password using a Key if that helps not sure if that's exactly what you have right now Dec 28, 2022 at 3:03
  • Please if you don't mind that would be great. Would that generate the plain text password if I have they key and the hash? What do you need from me? Would it be ok to post key and hash here? My apologies what is the actual process?
    – AliONE
    Dec 28, 2022 at 3:05

1 Answer 1

0

The process of exporting and importing a Password using the -Key parameter goes like this:

  1. You need to generate a Key, for that you can use the RandomNumberGenerator Class. Note: Valid key lengths are 16, 24 and 32 bytes.
$key = [byte[]]::new(32)
$ran = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$ran.GetBytes($key)

# Export the new key to a file:
Export-Clixml -InputObject $key -Path path\to\myKey.key
  1. Export your password using this key.
# Import the key (it should be obtained from a Vault preferably)
$key = Import-CliXml path\to\myKey.key

$myPassWord = 'myPassword'
ConvertTo-SecureString $myPassWord -AsPlainText -Force |
    ConvertFrom-SecureString -Key $key |
    Set-Content path\to\myPassword.txt
  1. Lastly you can import it whenever you want using the same key.

If you're using PowerShell Core:

# Get the encrypted password
$encryptedPassword = Get-Content path\to\myPassword.txt -Raw
# Import the key (it should be obtained from a Vault preferably)
$key = Import-CliXml path\to\myKey.key

ConvertTo-SecureString $encryptedPassword -Key $key |
    ConvertFrom-SecureString -AsPlainText
# -> Outputs: `myPassword`

If you're using Windows PowerShell:

# Get the encrypted password
$encryptedPassword = Get-Content path\to\myPassword.txt -Raw
# Import the key (it should be obtained from a Vault preferably)
$key = Import-CliXml path\to\myKey.key

$secureString = ConvertTo-SecureString $encryptedPassword -Key $key
[System.Net.NetworkCredential]::new('', $secureString).Password
# -> Outputs: `myPassword`
5
  • Step 1 $secureString = ConvertTo-SecureString $encryptedPassword -Key $key Step 2 [System.Net.NetworkCredential]::new('', $secureString).Password Resulets # -> Outputs: myPassword
    – AliONE
    Dec 28, 2022 at 3:37
  • I get the following ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null. At line:1 char:40 + $secureString = ConvertTo-SecureString $encryptedPassword -Key $key
    – AliONE
    Dec 28, 2022 at 3:39
  • @AliONE you're missing the part where you read the content of the files using Get-Content. Please, re-read the answer. Dec 28, 2022 at 3:40
  • 1
    You really need to learn the basics of powershell to even understand the help you are provided. Gotta crawl before you walk. Dec 28, 2022 at 3:45
  • 1
    @SantiagoSquarzon that worked! I was missing a statement. Thank you. Santiago I appreciate the patience and the help. Doug Maurer. For sure. I am learning.
    – AliONE
    Dec 28, 2022 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.