I was trying to convert some text to MD5 or base64 encoding. Here are a couple of powershell functions that can help with that task
Function to convert to Base64
[sourcecode]
# Function to Convert to Account name to Base64
function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}
[/sourcecode]
Function to convert to MD5
[sourcecode]
Function ConvertTo-MD5($string) {
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
$utf8 = new-object -TypeName System.Text.UTF8Encoding;
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($String))).Replace("-", "");
return $hash;
}
[/sourcecode]
Here is the rest of the script to put it all together
[sourcecode]
# Get Strings to convert
$base64string = Read-host "Type the base64 string here"
$md5string = Read-host "Type the MD5 string here" -AsSecureString
$BSTR = `
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecretKey)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$aktemp = ConvertTo-Base64($base64string)
$sktemp = ConvertTo-MD5($PlainPassword)
Write-host "The base64 string is: " $aktemp
Write-host "The MD5 stirng is: " $sktemp
[/sourcecode]