0% found this document useful (0 votes)
4 views1 page

Tao Machine Key Cho Webconfig

The document provides a PowerShell function named Generate-MachineKey that generates a machine key for web applications. It allows users to specify decryption and validation algorithms, and outputs the machine key in a format suitable for inclusion in a Web.config file. The function includes internal methods for converting binary data to hexadecimal format and handles different cryptographic algorithms.

Uploaded by

binhccchinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Tao Machine Key Cho Webconfig

The document provides a PowerShell function named Generate-MachineKey that generates a machine key for web applications. It allows users to specify decryption and validation algorithms, and outputs the machine key in a format suitable for inclusion in a Web.config file. The function includes internal methods for converting binary data to hexadecimal format and handles different cryptographic algorithms.

Uploaded by

binhccchinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# Powershell >> cmd bellow >> then type "Generate-MachineKey -validation sha1" and

paste result into Webconfig.


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

You might also like