0

I'm trying to hit a web service using an SHA1 64 bit encoded password, but I'm not sure how to code this in powershell.

I tried using: PS C:\Program Files (x86)\PowerGUI> $password = "password not shown here"

PS C:\Program Files (x86)\PowerGUI> $bytes =  System.Text.Encoding]::Unicode.GetBytes($password)

PS C:\Program Files (x86)\PowerGUI> $encodedString = [Convert]::ToBase64CharArray($bytes)

But I got this back:

Cannot find an overload for "ToBase64CharArray" and the argument count: "1". At line:1 char:46 + $encodedString = [Convert]::ToBase64CharArray <<<< ($bytes) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

I'm new with powershell, so this may not even be the right code to use. I tried to modify an example I found online.

Any ideas how to do this only in powershell using SHA1 encryption and base64 encoding?


Okay, this works for the encoding, thanks to Jonathan for this part:

$str = "pwd"
$bytes = [System.Text.Encoding]::Unicode.GetBytes($str)
$encodedStr = [Convert]::ToBase64String($bytes)

# and the result:
Write-Host $encodedStr
cAB3AGQA

I noticed the function call :: is sensitive to whitespace. This works for the encryption piece.

$Sha1provider = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$hashBytes = $Sha1provider.ComputeHash($bytes)
Write-Host $hashBytes
114 168 243 129 97 21 246 249 22 4 38 215 241 185 174 86 116 201 7 7

1 Answer 1

1

The Convert.ToBase64CharArray method doesn't have an overload with just one argument.

http://msdn.microsoft.com/en-us/library/3d0e5t57.aspx

You probably want to do:

$encodedString = [Convert]::ToBase64String($bytes);
write-host $encodedString

Note that base64 encoding is not SHA1 encoding. Look here: http://blog.logiclabz.com/c/function-to-encrypt-string-in-c-net-using-sha1-algorithm.aspx

Also, you don't necessarily have to convert C# code to Powershell.

You can use Add-Type to include C# code in your scripts.

PS: And I think you know that encoding is not encryption? http://www.blesta.com/2009/07/26/encoding-vs-encryption/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.