Convert a string to a byte array in PowerShell version 2

You most likely just need to convert your hash to base64 after the last line.

$enc = [system.Text.Encoding]::UTF8
$string1 = "This is a string to hash" 
$data1 = $enc.GetBytes($string1) 

# Create a New SHA1 Crypto Provider 
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 

# Now hash and display results 
$result1 = $sha.ComputeHash($data1)
[System.Convert]::ToBase64String($result1)

Text->Bytes->Encrypt/Hash->Base64

That's a very common pattern for sending cryptographic data in a text format.


It looks like you're on the right track. You have to pick a character encoding to convert between a string and a byte array. You picked UTF-8 above, but there are other options (e.g. ASCII, UTF-16, etc.).

Encrypting a string directly is not supported.