Generate Machine Key using Power shell

Its way easier than I thought: no need to save a .ps1 file, just paste the script and run it. I am on my local PC, Win 8.1 as an Admin.

Open PowerShell as Administrator.

Paste in the script from https://support.microsoft.com/en-us/help/2915218/resolving-view-state-message-authentication-code-mac-errors#AppendixA as so:

Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.

PS C:\Users\JM> # Generates a <machineKey> element that can be copied + pasted into a Web.config file.
PS C:\Users\JM> function Generate-MachineKey {
>>   [CmdletBinding()]
>>   param (
>>     [ValidateSet("AES", "DES", "3DES")]
>>     [string]$decryptionAlgorithm = 'AES',
>>     [ValidateSet("MD5", "SHA1", "HMACSHA256", "HMACSHA384", "HMACSHA512")]
>>     [string]$validationAlgorithm = 'HMACSHA256'
>>   )
>>   process {
>>     function BinaryToHex {
>>         [CmdLetBinding()]
>>         param($bytes)
>>         process {
>>             $builder = new-object System.Text.StringBuilder
>>             foreach ($b in $bytes) {
>>               $builder = $builder.AppendFormat([System.Globalization.CultureInfo]::InvariantCulture, "{0:X2}", $b)
>>             }
>>             $builder
>>         }
>>     }
>>     switch ($decryptionAlgorithm) {
>>       "AES" { $decryptionObject = new-object System.Security.Cryptography.AesCryptoServiceProvider }
>>       "DES" { $decryptionObject = new-object System.Security.Cryptography.DESCryptoServiceProvider }
>>       "3DES" { $decryptionObject = new-object System.Security.Cryptography.TripleDESCryptoServiceProvider }
>>     }
>>     $decryptionObject.GenerateKey()
>>     $decryptionKey = BinaryToHex($decryptionObject.Key)
>>     $decryptionObject.Dispose()
>>     switch ($validationAlgorithm) {
>>       "MD5" { $validationObject = new-object System.Security.Cryptography.HMACMD5 }
>>       "SHA1" { $validationObject = new-object System.Security.Cryptography.HMACSHA1 }
>>       "HMACSHA256" { $validationObject = new-object System.Security.Cryptography.HMACSHA256 }
>>       "HMACSHA385" { $validationObject = new-object System.Security.Cryptography.HMACSHA384 }
>>       "HMACSHA512" { $validationObject = new-object System.Security.Cryptography.HMACSHA512 }
>>     }
>>     $validationKey = BinaryToHex($validationObject.Key)
>>     $validationObject.Dispose()
>>     [string]::Format([System.Globalization.CultureInfo]::InvariantCulture,
>>       "<machineKey decryption=`"{0}`" decryptionKey=`"{1}`" validation=`"{2}`" validationKey=`"{3}`" />",
>>       $decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
>>       $validationAlgorithm.ToUpperInvariant(), $validationKey)
>>   }
>> }
>>

Type in this command on the next line

PS C:\Users\JM> Generate-MachineKey
<machineKey decryption="AES" decryptionKey="xxxxxxxxxxxxxxxxxxxx" validation="HMACSHA256" validationKey="xxxxxxxxxxxxxxxxx" />

The script contains a function so you'll have to first load the script and then run the function for it to work.

First we'll have to load the file, i called my ps1 "MachineKey" so this is how i load it

PS E:\> . .\MachineKey.ps1

Once i've loaded the file if i want to run the function called "Generate-MachineKey" i have to type this in afterwards

PS E:\> Generate-MachineKey -validationAlgorithm SHA1

Tags:

Powershell