英語で読む

次の方法で共有


RsaProtectedConfigurationProvider クラス

定義

構成データの暗号化と復号化を行うために RSA 暗号方式を使用する ProtectedConfigurationProvider インスタンスを提供します。

C#
public sealed class RsaProtectedConfigurationProvider : System.Configuration.ProtectedConfigurationProvider
継承
RsaProtectedConfigurationProvider

次の例は、標準 RsaProtectedConfigurationProvider を使用して構成セクションを保護または保護解除する方法を示しています。

C#
using System;
using System.Configuration;

public class UsingRsaProtectedConfigurationProvider
{

    // Protect the connectionStrings section.
    private static void ProtectConfiguration()
    {

        // Get the application configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

        // Define the Rsa provider name.
        string provider =
            "RsaProtectedConfigurationProvider";

        // Get the section to protect.
        ConfigurationSection connStrings =
            config.ConnectionStrings;

        if (connStrings != null)
        {
            if (!connStrings.SectionInformation.IsProtected)
            {
                if (!connStrings.ElementInformation.IsLocked)
                {
                    // Protect the section.
                    connStrings.SectionInformation.ProtectSection(provider);

                    connStrings.SectionInformation.ForceSave = true;
                    config.Save(ConfigurationSaveMode.Full);

                    Console.WriteLine("Section {0} is now protected by {1}",
                        connStrings.SectionInformation.Name,
                        connStrings.SectionInformation.ProtectionProvider.Name);
                }
                else
                    Console.WriteLine(
                         "Can't protect, section {0} is locked",
                         connStrings.SectionInformation.Name);
            }
            else
                Console.WriteLine(
                    "Section {0} is already protected by {1}",
                    connStrings.SectionInformation.Name,
                    connStrings.SectionInformation.ProtectionProvider.Name);
        }
        else
            Console.WriteLine("Can't get the section {0}",
                connStrings.SectionInformation.Name);
    }

    // Unprotect the connectionStrings section.
    private static void UnProtectConfiguration()
    {

        // Get the application configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

        // Get the section to unprotect.
        ConfigurationSection connStrings =
            config.ConnectionStrings;

        if (connStrings != null)
        {
            if (connStrings.SectionInformation.IsProtected)
            {
                if (!connStrings.ElementInformation.IsLocked)
                {
                    // Unprotect the section.
                    connStrings.SectionInformation.UnprotectSection();

                    connStrings.SectionInformation.ForceSave = true;
                    config.Save(ConfigurationSaveMode.Full);

                    Console.WriteLine("Section {0} is now unprotected.",
                        connStrings.SectionInformation.Name);
                }
                else
                    Console.WriteLine(
                         "Can't unprotect, section {0} is locked",
                         connStrings.SectionInformation.Name);
            }
            else
                Console.WriteLine(
                    "Section {0} is already unprotected.",
                    connStrings.SectionInformation.Name);
        }
        else
            Console.WriteLine("Can't get the section {0}",
                connStrings.SectionInformation.Name);
    }

    public static void Main(string[] args)
    {

        string selection = string.Empty;

        if (args.Length == 0)
        {
            Console.WriteLine(
                "Select protect or unprotect");
            return;
        }

        selection = args[0].ToLower();

        switch (selection)
        {
            case "protect":
                ProtectConfiguration();
                break;

            case "unprotect":
                UnProtectConfiguration();
                break;
 
            default:
                Console.WriteLine("Unknown selection");
                break;
        }

        Console.Read();
    }
}

次の例は、暗号化後の構成ファイルからの抜粋を示しています。

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
        xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <KeyName>Rsa Key</KeyName>
          </KeyInfo>
          <CipherData>
            <CipherValue>B702tRDVHJjC3CYXt7I0ucCDjdht/Vyk/DdUhwQyt7vepSD85dwCP8ox9Y1BUdjajFeTFfFBsGypbli5HPGRYamQdrVkPo07bBBXNT5H02qxREguGUU4iDtV1Xp8BLVZjQMV4ZgP6Wbctw2xRvPC7GvKHLI4fUN/Je5LmutsijA=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>ME+XJA2TAj3QN3yT4pJq3sRArC0i7Cz3Da71BkaRe9QNfuVuUjcv0jeGUN4wDdOAZ7LPq6UpVrpirY3kQcALDvPJ5nKxk++Mw75rjtIO8eh2goTY9rCK6zanfzaDshFy7IqItpvs/y2kmij25nM3ury6uO0hCf0UbEL1mbT2jXDqvcrHZUobO1Ef6bygBZ/8HpU+VfF9CTCob/BBE9zUkK37EQhcduwsnzBvDblYbF/Rd+F4lxAkZnecGLfCZjOzJB4xH1a0vvWtPR7zNwL/7I0uHzQjyMdWrkBnotMjoR70R7NELBotCogWO0MBimncKigdR3dTTdrCd72a7UJ4LMlEQaZXGIJp4PIg6qVDHII=</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
</configuration>

注釈

クラスを RsaProtectedConfigurationProvider 使用すると、構成ファイルに格納されている機密情報を暗号化する方法が提供されます。これにより、未承認のアクセスから保護できます。 組み込み RsaProtectedConfigurationProvider インスタンスを使用するには、「例」セクションに示すように、このクラスのインスタンスを作成する代わりに、プロバイダーを宣言し、構成ファイルで適切な設定を行います。

オブジェクトは RsaProtectedConfigurationProvider 、クラスによって RSA 提供される暗号化関数を使用して、構成セクションの暗号化と暗号化解除を行います。

注意

ASP.NET 構成ファイル内の暗号化された情報を暗号化解除するには、ASP.NET アプリケーションの ID が、構成データの暗号化と暗号化解除に使用される暗号化キーへの読み取りアクセス権を持っている必要があります。 詳細については、「 チュートリアル: 保護された構成を使用した構成情報の暗号化」を参照してください

注意

.NET Core と .NET 5 以降では、 RsaProtectedConfigurationProvider 型はサポートされていません。 すべての API は、実行時に を PlatformNotSupportedException スローします。

コンストラクター

RsaProtectedConfigurationProvider()

RsaProtectedConfigurationProvider クラスの新しいインスタンスを初期化します。

プロパティ

CspProviderName

Windows Cryptography API (crypto API) 暗号化サービス プロバイダー (CSP: Cryptographic Service Provider) の名前を取得します。

Description

管理ツールまたは他のユーザー インターフェイス (UI) での表示に適した、簡単でわかりやすい説明を取得します。

(継承元 ProviderBase)
KeyContainerName

キー コンテナーの名前を取得します。

Name

構成時にプロバイダーを参照するために使用される表示名を取得します。

(継承元 ProviderBase)
RsaPublicKey

プロバイダーが使用するパブリック キーを取得します。

UseFIPS

プロバイダーが FIPS を使用するかどうかを示す値を取得します。

UseMachineContainer

RsaProtectedConfigurationProvider オブジェクトがコンピューターのキー コンテナーを使用しているかどうかを示す値を取得します。

UseOAEP

プロバイダーが OAEP (Optimal Asymmetric Encryption Padding) キー交換データを使用しているかどうかを示す値を取得します。

メソッド

AddKey(Int32, Boolean)

RSA キー コンテナーにキーを追加します。

Decrypt(XmlNode)

渡される XML ノードを復号化します。

DeleteKey()

RSA キー コンテナーからキーを削除します。

Encrypt(XmlNode)

渡される XML ノードを暗号化します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
ExportKey(String, Boolean)

RSA キーをキー コンテナーからエクスポートします。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
ImportKey(String, Boolean)

RSA キーをキー コンテナーにインポートします。

Initialize(String, NameValueCollection)

既定の設定でプロバイダーを初期化します。

Initialize(String, NameValueCollection)

構成ビルダーを初期化します。

(継承元 ProviderBase)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

製品 バージョン
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください