닷넷에서 machine key를 변경

2024. 8. 26. 16:12젬스it

반응형


닷넷에서 machine key를 변경하려면 다음 단계를 따라야 합니다:
Machine Key 생성:
PowerShell을 사용하여 새로운 machine key를 생성할 수 있습니다. 아래 코드를 PowerShell에 입력하여 실행하세요:

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 }
            "HMACSHA384" { $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)
    }
}


Web.config 파일에 추가:
생성된 machine key를 Web.config 파일의 <system.web> 섹션에 추가합니다:
XML

<configuration>
    <system.web>
        <machineKey decryption="AES" decryptionKey="..." validation="HMACSHA256" validationKey="..." />
    </system.web>
</configuration>





반응형