GetNetworkCredential difficulties

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jsira2003@yahoo.com
Posts: 112
Joined: Fri Apr 07, 2017 9:47 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com » Sun Jan 27, 2019 7:28 pm

I still can't get over works in ise and not packaged. That doesn't make sense. The logic is the same, however the environment is different.

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GetNetworkCredential difficulties

Post by jvierra » Sun Jan 27, 2019 7:42 pm

How did you create the byte array? It has to be done correctly.

My code works with no issues. Without posting what you have done and just saying it doesn't work is not helpful.

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GetNetworkCredential difficulties

Post by jvierra » Sun Jan 27, 2019 7:43 pm

jsira2003@yahoo.com wrote:
Sun Jan 27, 2019 7:28 pm
I still can't get over works in ise and not packaged. That doesn't make sense. The logic is the same, however the environment is different.
You are still not providing sufficient information. We cannot guess at what you are doing.

User avatar
jsira2003@yahoo.com
Posts: 112
Joined: Fri Apr 07, 2017 9:47 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com » Sun Jan 27, 2019 7:46 pm

[byte[]]$global:LilHash1 = (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)

This isn't my actual key but you get the idea.

I used values from 0 - 255. Not the 1-32 as I have shown. I have put 32 values in the parenthesis.

John

User avatar
jsira2003@yahoo.com
Posts: 112
Joined: Fri Apr 07, 2017 9:47 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com » Sun Jan 27, 2019 7:49 pm

I have changed all instances of the key to be the byte array. It appears that the array without the byte cast worked the same as with the byte cast.

John

User avatar
jsira2003@yahoo.com
Posts: 112
Joined: Fri Apr 07, 2017 9:47 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com » Sun Jan 27, 2019 7:56 pm

I tried this:

$Secure2 = $aline | ConvertTo-SecureString -Key ($key) -debug

This is what I got

Exception calling .ctor with 2 argument(s): 'cannot process argument basuse the value of the argument "password" is null. change the value of argument "password" to a non-null value.

John

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GetNetworkCredential difficulties

Post by jvierra » Sun Jan 27, 2019 8:20 pm

jsira2003@yahoo.com wrote:
Sun Jan 27, 2019 7:56 pm
I tried this:

$Secure2 = $aline | ConvertTo-SecureString -Key ($key) -debug

This is what I got

Exception calling .ctor with 2 argument(s): 'cannot process argument basuse the value of the argument "password" is null. change the value of argument "password" to a non-null value.

John
Why do you keep putting parens around $key?

I ran the code at a prompt and in a packaged EXE and it works with no issues.

User avatar
jsira2003@yahoo.com
Posts: 112
Joined: Fri Apr 07, 2017 9:47 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com » Sun Jan 27, 2019 8:22 pm

I'll pull the parens out. My code shows that error with the debug.

Crazy stuff!

John

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GetNetworkCredential difficulties

Post by jvierra » Sun Jan 27, 2019 8:25 pm

Here is a complete script that creates a file and decrypts it. It works under all circumstances that I have tried.

Code: Select all


Function DecryptUserKey {
    #requires -Version 3
    Param (
    [string]$File,
    [byte[]]$Key
    )
    
    $ErrorActionPreference = 'Stop'
    
    Try {
        Get-Content $File |
        ForEach-Object{
            $ss = $_ | ConvertTo-SecureString -Key $Key
            ([System.Management.Automation.PSCredential]::New('N/A', $ss)).GetNetworkCredential().Password
        }
    } Catch {
        [System.Windows.Forms.MessageBox]::Show($_)
    }
    
}

$Key = [system.Text.Encoding]::UTF8.GetBytes('1234561234567890')

$pwdfile = 'd:\scripts\passwords.txt'
# make encoded file:
$Key = [system.Text.Encoding]::UTF8.GetBytes('1234561234567890')
'password1', 'password2', 'password3' |
    ConvertTo-SecureString -AsPlainText -force |
    ConvertFrom-SecureString -Key $Key |
    Out-File $pwdfile

# decrypt password file
$decrypted = DecryptUserKey -File $pwdfile -Key $key
$decrypted
pause

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GetNetworkCredential difficulties

Post by jvierra » Sun Jan 27, 2019 8:28 pm

This shows the easiest way to create a key.

Code: Select all


Function DecryptUserKey {
    #requires -Version 3
    Param (
    [string]$File,
    [byte[]]$Key
    )
    
    $ErrorActionPreference = 'Stop'
    
    Try {
        Get-Content $File |
        ForEach-Object{
            $ss = $_ | ConvertTo-SecureString -Key $Key
            ([System.Management.Automation.PSCredential]::New('N/A', $ss)).GetNetworkCredential().Password
        }
    } Catch {
        [System.Windows.Forms.MessageBox]::Show($_)
    }
    
}

$keyText = 'aNsdknalkjdspsojdskljlskdjslkjdslkjjls'

# creae 32 character string
$keyString = $keyText.Substring(0,32)
$Key = [system.Text.Encoding]::UTF8.GetBytes($keyString)

$pwdfile = 'd:\scripts\passwords.txt'
# make encoded file:
$Key = [system.Text.Encoding]::UTF8.GetBytes('1234561234567890')
'password1', 'password2', 'password3' |
    ConvertTo-SecureString -AsPlainText -force |
    ConvertFrom-SecureString -Key $Key |
    Out-File $pwdfile

# decrypt password file
$decrypted = DecryptUserKey -File $pwdfile -Key $key
$decrypted
pause

Locked