[英]Generate Machine Key using Power shell
我正在嘗試生成機器密鑰以在幾台機器之間共享,經過快速谷歌搜索后,我找到了這篇文章 KB 2915218,附錄 A。
PS E: ./Generate-MachineKey -validation sha1
它運行良好,但不輸出密鑰。 有什么理由嗎? 我在 Powershell 中做錯了什么嗎?
謝謝,
該腳本包含一個函數,因此您必須首先加載該腳本,然后運行該函數才能使其工作。
首先,我們必須加載文件,我將我的 ps1 稱為“MachineKey”,這就是我加載它的方式
PS E:\> . .\MachineKey.ps1
一旦我加載了文件,如果我想運行名為“Generate-MachineKey”的函數,我必須在之后輸入它
PS E:\> Generate-MachineKey -validationAlgorithm SHA1
它的方式比我想象的要容易:不需要保存 .ps1 文件,只需粘貼腳本並運行它。 我在本地 PC 上,以管理員身份使用 Win 8.1。
以管理員身份打開 PowerShell。
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)
>> }
>> }
>>
在下一行輸入此命令
PS C:\Users\JM> Generate-MachineKey
<machineKey decryption="AES" decryptionKey="xxxxxxxxxxxxxxxxxxxx" validation="HMACSHA256" validationKey="xxxxxxxxxxxxxxxxx" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.