1

I have the following powershell script from original source. It is used for generating a machine key for validation of the viewstate MAC. However, when I run it in Powershell:

PS C:\Users\Documents> .\Generate-MachineKey.ps1 -decryptionAlgorithm AES -validationAlgorithm SHA1

Nothing is being output, the following is the script. What is the reason that nothing is output.

 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)
  }
}
1

2 Answers 2

2

Because by calling script file in your command you actually do not call the function Generate-MachineKey inside the script.

Correct way in your case would be "dot source" the script file and then call the function contained within the script:

. .\Generate-MachineKey.ps1
Generate-MachineKey -decryptionAlgorithm AES -validationAlgorithm SHA1

Please read dot sourcing Powershell scripts for more details and understand the difference between scripts, functions, calling and dot sourcing concepts

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

Comments

1

You are calling the script, but no the function inside. Add a call to your function at the end of the script:

Generate-MachineKey -decryptionAlgorithm AES  -validationAlgorithm SHA1

Alternatively, you can take the code out of the function and just have the 'param' section as the parameters definition for the script instead.

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.