Can we encrypt only username using basic authentication in powershell

Prabha 241 Reputation points
2021-11-08T20:24:16.413+00:00

Can we encrypt only username using basic authentication in PowerShell ? The below example shows for both username and password encrypted using basic authentication. or do we have anything that converts the username in base64 instead of passing as a plain text (hardcoded) likewise we use secure string for passwords.

$user = 'user'
$pass = 'pass'
$securePassword = (ConvertTo-SecureString $Password -AsPlainText -Forc
$pair = "$($user):$( $securePassword)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
 $Headers = @{
    Authorization = $basicAuthValue
}
Invoke-WebRequest -Uri 'https://whatever' -Headers $Headers
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-11-08T20:30:37.397+00:00

    Hi @Swabha ,

    something like this?

    $userName = "Luigi"
    $encryptedUserName = $userName | ConvertTo-SecureString -AsPlainText -Force
    $encryptedUserName
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 47,901 Reputation points
    2021-11-08T22:30:41.653+00:00

    Try this:

    $barray  = [System.Text.Encoding]::UTF8.GetBytes("MyUserName")
    $b64 = [System.Convert]::ToBase64String($barray)
    

  3. MotoX80 36,291 Reputation points
    2021-11-08T23:36:54.54+00:00

    You have to adhere to internet standards.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication

    Basic authentication scheme
    The "Basic" HTTP authentication scheme is defined in RFC 7617, which transmits credentials as user ID/password pairs, encoded using base64.

    Security of basic authentication
    As the user ID and password are passed over the network as clear text (it is base64 encoded, but base64 is a reversible encoding), the basic authentication scheme is not secure. HTTPS/TLS should be used with basic authentication. Without these additional security enhancements, basic authentication should not be used to protect sensitive or valuable information.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.