GetNetworkCredential difficulties

Ask your 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.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GetNetworkCredential difficulties

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GetNetworkCredential difficulties

Post by jvierra »

jsira2003@yahoo.com wrote: Sun Jan 27, 2019 6: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: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com »

[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: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com »

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: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GetNetworkCredential difficulties

Post by jvierra »

jsira2003@yahoo.com wrote: Sun Jan 27, 2019 6: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: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: GetNetworkCredential difficulties

Post by jsira2003@yahoo.com »

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

Crazy stuff!

John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GetNetworkCredential difficulties

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GetNetworkCredential difficulties

Post by jvierra »

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
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked