ProtectedConfigurationProvider クラス
アセンブリ: System.Configuration (system.configuration.dll 内)


構成ファイルのセクションを暗号化して、アプリケーションで使用する機密情報を保護できます。これにより、たとえ攻撃者が構成ファイルにアクセスできたとしても、未承認のアクセスを行うのが難しくなるため、セキュリティが向上します。
.NET Framework には、構成ファイルのセクションの暗号化に使用できる保護された構成プロバイダが 2 つ用意されています。1 つは構成セクションの暗号化に RSACryptoServiceProvider を使用する RSAProtectedConfigurationProvider で、もう 1 つは構成セクションの暗号化に Windows データ保護 API (DPAPI: Data Protection API) を使用する DPAPIProtectedConfigurationProvider です。
RSA プロバイダまたは DPAPI プロバイダ以外のアルゴリズムを使用して機密情報を暗号する必要がある場合もあります。この場合、カスタムの保護された構成プロバイダを作成します。ProtectedConfigurationProvider は、独自の保護された構成プロバイダを作成するために継承する必要がある抽象基本クラスです。
標準プロバイダとカスタム プロバイダのどちらを使用する場合も、そのプロバイダが configProtectedData 構成セクションの providers セクション内にある add 要素を使用して構成されていることを確認する必要があります (次の例を参照してください)。
詳細については、「保護された構成プロバイダの実装」を参照してください。
![]() |
---|
ASP.NET は、暗号化された構成データを検出すると、構成済みのプロバイダを使用して透過的に復号化を実行します。必要なプロバイダを構成していることを確認する以外には、何もする必要はありません。 |

カスタム ProtectedConfigurationProvider を実装する方法を次の例に示します。
このプロバイダを構成できるようにするには、次の構成の抜粋で示すように、このプロバイダをグローバル アセンブリ キャッシュ (GAC: Global Assembly Cache) にインストールする必要があります。詳細については、「保護された構成プロバイダの実装」を参照してください。
Imports System Imports System.Xml Imports System.Security.Cryptography Imports System.IO Imports System.Text Imports System.Configuration.Provider Imports System.Collections.Specialized Imports System.Configuration ' Show how to use the ProtectedConfigurationProvider ' to create a custom protected configuration ' provider. Public Class TripleDESProtectedConfigurationProvider Inherits ProtectedConfigurationProvider Private des _ As New TripleDESCryptoServiceProvider() Private pKeyFilePath As String Private pName As String ' Gets the path of the file ' containing the key used to ' encrypt/decrypt. Public ReadOnly Property KeyFilePath() As String Get Return pKeyFilePath End Get End Property ' Gets the provider name. Public Overrides ReadOnly Property Name() As String Get Return pName End Get End Property ' Performs provider initialization. Public Overrides Sub Initialize( _ ByVal name As String, _ ByVal config As NameValueCollection) pName = name pKeyFilePath = config("keyContainerName") ReadKey(KeyFilePath) End Sub 'Initialize ' Performs encryption. Public Overrides Function Encrypt( _ ByVal node As XmlNode) As XmlNode Dim encryptedData As String = _ EncryptString(node.OuterXml) Dim xmlDoc As New XmlDocument() xmlDoc.PreserveWhitespace = True xmlDoc.LoadXml( _ ("<EncryptedData>" + encryptedData + _ "</EncryptedData>")) Return xmlDoc.DocumentElement End Function 'Encrypt ' Performs decryption. Public Overrides Function Decrypt( _ ByVal encryptedNode As XmlNode) As XmlNode Dim decryptedData As String = _ DecryptString(encryptedNode.InnerText) Dim xmlDoc As New XmlDocument() xmlDoc.PreserveWhitespace = True xmlDoc.LoadXml(decryptedData) Return xmlDoc.DocumentElement End Function 'Decrypt ' Encrypts a configuration section and returns ' the encrypted XML as a string. Private Function EncryptString( _ ByVal encryptValue As String) As String Dim valBytes As Byte() = _ Encoding.Unicode.GetBytes(encryptValue) Dim transform As ICryptoTransform = _ des.CreateEncryptor() Dim ms As New MemoryStream() Dim cs As New CryptoStream(ms, _ transform, CryptoStreamMode.Write) cs.Write(valBytes, 0, valBytes.Length) cs.FlushFinalBlock() Dim returnBytes As Byte() = ms.ToArray() cs.Close() Return Convert.ToBase64String(returnBytes) End Function 'EncryptString ' Decrypts an encrypted configuration section and ' returns the unencrypted XML as a string. Private Function DecryptString( _ ByVal encryptedValue As String) As String Dim valBytes As Byte() = _ Convert.FromBase64String(encryptedValue) Dim transform As ICryptoTransform = _ des.CreateDecryptor() Dim ms As New MemoryStream() Dim cs As New CryptoStream(ms, _ transform, CryptoStreamMode.Write) cs.Write(valBytes, 0, valBytes.Length) cs.FlushFinalBlock() Dim returnBytes As Byte() = ms.ToArray() cs.Close() Return Encoding.Unicode.GetString(returnBytes) End Function 'DecryptString ' Generates a new TripleDES key and vector and ' writes them to the supplied file path. Public Sub CreateKey(filePath As String) des.GenerateKey() des.GenerateIV() Dim sw As New StreamWriter(filePath, False) sw.WriteLine(ByteToHex(des.Key)) sw.WriteLine(ByteToHex(des.IV)) sw.Close() End Sub 'CreateKey ' Reads in the TripleDES key and vector from ' the supplied file path and sets the Key ' and IV properties of the ' TripleDESCryptoServiceProvider. Private Sub ReadKey(filePath As String) Dim sr As New StreamReader(filePath) Dim keyValue As String = sr.ReadLine() Dim ivValue As String = sr.ReadLine() des.Key = HexToByte(keyValue) des.IV = HexToByte(ivValue) End Sub 'ReadKey ' Converts a byte array to a hexadecimal string. Private Function ByteToHex( _ ByVal byteArray() As Byte) As String Dim outString As String = "" Dim b As [Byte] For Each b In byteArray outString += b.ToString("X2") Next b Return outString End Function 'ByteToHex ' Converts a hexadecimal string to a byte array. Private Function HexToByte(hexString As String) As Byte() Dim returnBytes(hexString.Length / 2) As Byte Dim i As Integer For i = 0 To returnBytes.Length - 1 returnBytes(i) = _ Convert.ToByte(hexString.Substring(i * 2, 2), 16) Next i Return returnBytes End Function 'HexToByte End Class 'TripleDESProtectedConfigurationProvider
using System; using System.Xml; using System.Security.Cryptography; using System.IO; using System.Text; using System.Configuration.Provider; using System.Collections.Specialized; using System.Configuration; namespace Samples.AspNet.Configuration { // Show how to use the ProtectedConfigurationProvider // to create a custom protected configuration // provider. public class TripleDESProtectedConfigurationProvider : ProtectedConfigurationProvider { private TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); private string pKeyFilePath; private string pName; // Gets the path of the file // containing the key used to // encrypt/decrypt. public string KeyFilePath { get { return pKeyFilePath; } } // Gets the provider name. public override string Name { get { return pName; } } // Performs provider initialization. public override void Initialize(string name, NameValueCollection config) { pName = name; pKeyFilePath = config["keyContainerName"]; ReadKey(KeyFilePath); } // Performs encryption. public override XmlNode Encrypt(XmlNode node) { string encryptedData = EncryptString(node.OuterXml); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml("<EncryptedData>" + encryptedData + "</EncryptedData>"); return xmlDoc.DocumentElement; } // Performs decryption. public override XmlNode Decrypt(XmlNode encryptedNode) { string decryptedData = DecryptString(encryptedNode.InnerText); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml(decryptedData); return xmlDoc.DocumentElement; } // Encrypts a configuration section and returns // the encrypted XML as a string. private string EncryptString(string encryptValue) { byte[] valBytes = Encoding.Unicode.GetBytes(encryptValue); ICryptoTransform transform = des.CreateEncryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write); cs.Write(valBytes, 0, valBytes.Length); cs.FlushFinalBlock(); byte[] returnBytes = ms.ToArray(); cs.Close(); return Convert.ToBase64String(returnBytes); } // Decrypts an encrypted configuration section and // returns the unencrypted XML as a string. private string DecryptString(string encryptedValue) { byte[] valBytes = Convert.FromBase64String(encryptedValue); ICryptoTransform transform = des.CreateDecryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write); cs.Write(valBytes, 0, valBytes.Length); cs.FlushFinalBlock(); byte[] returnBytes = ms.ToArray(); cs.Close(); return Encoding.Unicode.GetString(returnBytes); } // Generates a new TripleDES key and vector and // writes them to the supplied file path. public void CreateKey(string filePath) { des.GenerateKey(); des.GenerateIV(); StreamWriter sw = new StreamWriter(filePath, false); sw.WriteLine(ByteToHex(des.Key)); sw.WriteLine(ByteToHex(des.IV)); sw.Close(); } // Reads in the TripleDES key and vector from // the supplied file path and sets the Key // and IV properties of the // TripleDESCryptoServiceProvider. private void ReadKey(string filePath) { StreamReader sr = new StreamReader(filePath); string keyValue = sr.ReadLine(); string ivValue = sr.ReadLine(); des.Key = HexToByte(keyValue); des.IV = HexToByte(ivValue); } // Converts a byte array to a hexadecimal string. private string ByteToHex(byte[] byteArray) { string outString = ""; foreach (Byte b in byteArray) outString += b.ToString("X2"); return outString; } // Converts a hexadecimal string to a byte array. private byte[] HexToByte(string hexString) { byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } } }
前のカスタム ProtectedConfigurationProvider を使用する方法を次の例に示します。
Imports System Imports System.Xml Imports System.Security.Cryptography Imports System.IO Imports System.Text Imports System.Configuration.Provider Imports System.Collections.Specialized Imports System.Configuration ' Show how to use the ProtectedConfigurationProvider ' to create a custom protected configuration ' provider. Public Class TripleDESProtectedConfigurationProvider Inherits ProtectedConfigurationProvider Private des _ As New TripleDESCryptoServiceProvider() Private pKeyFilePath As String Private pName As String ' Gets the path of the file ' containing the key used to ' encrypt/decrypt. Public ReadOnly Property KeyFilePath() As String Get Return pKeyFilePath End Get End Property ' Gets the provider name. Public Overrides ReadOnly Property Name() As String Get Return pName End Get End Property ' Performs provider initialization. Public Overrides Sub Initialize( _ ByVal name As String, _ ByVal config As NameValueCollection) pName = name pKeyFilePath = config("keyContainerName") ReadKey(KeyFilePath) End Sub 'Initialize ' Performs encryption. Public Overrides Function Encrypt( _ ByVal node As XmlNode) As XmlNode Dim encryptedData As String = _ EncryptString(node.OuterXml) Dim xmlDoc As New XmlDocument() xmlDoc.PreserveWhitespace = True xmlDoc.LoadXml( _ ("<EncryptedData>" + encryptedData + _ "</EncryptedData>")) Return xmlDoc.DocumentElement End Function 'Encrypt ' Performs decryption. Public Overrides Function Decrypt( _ ByVal encryptedNode As XmlNode) As XmlNode Dim decryptedData As String = _ DecryptString(encryptedNode.InnerText) Dim xmlDoc As New XmlDocument() xmlDoc.PreserveWhitespace = True xmlDoc.LoadXml(decryptedData) Return xmlDoc.DocumentElement End Function 'Decrypt ' Encrypts a configuration section and returns ' the encrypted XML as a string. Private Function EncryptString( _ ByVal encryptValue As String) As String Dim valBytes As Byte() = _ Encoding.Unicode.GetBytes(encryptValue) Dim transform As ICryptoTransform = _ des.CreateEncryptor() Dim ms As New MemoryStream() Dim cs As New CryptoStream(ms, _ transform, CryptoStreamMode.Write) cs.Write(valBytes, 0, valBytes.Length) cs.FlushFinalBlock() Dim returnBytes As Byte() = ms.ToArray() cs.Close() Return Convert.ToBase64String(returnBytes) End Function 'EncryptString ' Decrypts an encrypted configuration section and ' returns the unencrypted XML as a string. Private Function DecryptString( _ ByVal encryptedValue As String) As String Dim valBytes As Byte() = _ Convert.FromBase64String(encryptedValue) Dim transform As ICryptoTransform = _ des.CreateDecryptor() Dim ms As New MemoryStream() Dim cs As New CryptoStream(ms, _ transform, CryptoStreamMode.Write) cs.Write(valBytes, 0, valBytes.Length) cs.FlushFinalBlock() Dim returnBytes As Byte() = ms.ToArray() cs.Close() Return Encoding.Unicode.GetString(returnBytes) End Function 'DecryptString ' Generates a new TripleDES key and vector and ' writes them to the supplied file path. Public Sub CreateKey(filePath As String) des.GenerateKey() des.GenerateIV() Dim sw As New StreamWriter(filePath, False) sw.WriteLine(ByteToHex(des.Key)) sw.WriteLine(ByteToHex(des.IV)) sw.Close() End Sub 'CreateKey ' Reads in the TripleDES key and vector from ' the supplied file path and sets the Key ' and IV properties of the ' TripleDESCryptoServiceProvider. Private Sub ReadKey(filePath As String) Dim sr As New StreamReader(filePath) Dim keyValue As String = sr.ReadLine() Dim ivValue As String = sr.ReadLine() des.Key = HexToByte(keyValue) des.IV = HexToByte(ivValue) End Sub 'ReadKey ' Converts a byte array to a hexadecimal string. Private Function ByteToHex( _ ByVal byteArray() As Byte) As String Dim outString As String = "" Dim b As [Byte] For Each b In byteArray outString += b.ToString("X2") Next b Return outString End Function 'ByteToHex ' Converts a hexadecimal string to a byte array. Private Function HexToByte(hexString As String) As Byte() Dim returnBytes(hexString.Length / 2) As Byte Dim i As Integer For i = 0 To returnBytes.Length - 1 returnBytes(i) = _ Convert.ToByte(hexString.Substring(i * 2, 2), 16) Next i Return returnBytes End Function 'HexToByte End Class 'TripleDESProtectedConfigurationProvider
using System; using System.Xml; using System.Security.Cryptography; using System.IO; using System.Text; using System.Configuration.Provider; using System.Collections.Specialized; using System.Configuration; namespace Samples.AspNet.Configuration { // Show how to use the ProtectedConfigurationProvider // to create a custom protected configuration // provider. public class TripleDESProtectedConfigurationProvider : ProtectedConfigurationProvider { private TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); private string pKeyFilePath; private string pName; // Gets the path of the file // containing the key used to // encrypt/decrypt. public string KeyFilePath { get { return pKeyFilePath; } } // Gets the provider name. public override string Name { get { return pName; } } // Performs provider initialization. public override void Initialize(string name, NameValueCollection config) { pName = name; pKeyFilePath = config["keyContainerName"]; ReadKey(KeyFilePath); } // Performs encryption. public override XmlNode Encrypt(XmlNode node) { string encryptedData = EncryptString(node.OuterXml); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml("<EncryptedData>" + encryptedData + "</EncryptedData>"); return xmlDoc.DocumentElement; } // Performs decryption. public override XmlNode Decrypt(XmlNode encryptedNode) { string decryptedData = DecryptString(encryptedNode.InnerText); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml(decryptedData); return xmlDoc.DocumentElement; } // Encrypts a configuration section and returns // the encrypted XML as a string. private string EncryptString(string encryptValue) { byte[] valBytes = Encoding.Unicode.GetBytes(encryptValue); ICryptoTransform transform = des.CreateEncryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write); cs.Write(valBytes, 0, valBytes.Length); cs.FlushFinalBlock(); byte[] returnBytes = ms.ToArray(); cs.Close(); return Convert.ToBase64String(returnBytes); } // Decrypts an encrypted configuration section and // returns the unencrypted XML as a string. private string DecryptString(string encryptedValue) { byte[] valBytes = Convert.FromBase64String(encryptedValue); ICryptoTransform transform = des.CreateDecryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write); cs.Write(valBytes, 0, valBytes.Length); cs.FlushFinalBlock(); byte[] returnBytes = ms.ToArray(); cs.Close(); return Encoding.Unicode.GetString(returnBytes); } // Generates a new TripleDES key and vector and // writes them to the supplied file path. public void CreateKey(string filePath) { des.GenerateKey(); des.GenerateIV(); StreamWriter sw = new StreamWriter(filePath, false); sw.WriteLine(ByteToHex(des.Key)); sw.WriteLine(ByteToHex(des.IV)); sw.Close(); } // Reads in the TripleDES key and vector from // the supplied file path and sets the Key // and IV properties of the // TripleDESCryptoServiceProvider. private void ReadKey(string filePath) { StreamReader sr = new StreamReader(filePath); string keyValue = sr.ReadLine(); string ivValue = sr.ReadLine(); des.Key = HexToByte(keyValue); des.IV = HexToByte(ivValue); } // Converts a byte array to a hexadecimal string. private string ByteToHex(byte[] byteArray) { string outString = ""; foreach (Byte b in byteArray) outString += b.ToString("X2"); return outString; } // Converts a hexadecimal string to a byte array. private byte[] HexToByte(string hexString) { byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configProtectedData > <providers> <clear /> <add keyContainerName="pcKey.txt" name="TripleDESProtectedConfigurationProvider" type="Samples.Aspnet.Configuration.TripleDESProtectedConfigurationProvider, protectedconfigurationproviderlib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=79e01ae0f5cfc66f, processorArchitecture=MSIL" /> </providers> </configProtectedData > <connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=webnetue2;Initial Catalog=Northwind;User ID=aspnet_test;Password=test" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>

System.Configuration.Provider.ProviderBase
System.Configuration.ProtectedConfigurationProvider
System.Configuration.DpapiProtectedConfigurationProvider
System.Configuration.RsaProtectedConfigurationProvider


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ProtectedConfigurationProvider コンストラクタ
アセンブリ: System.Configuration (system.configuration.dll 内)



Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ProtectedConfigurationProvider プロパティ

名前 | 説明 | |
---|---|---|
![]() | Description | 管理ツールまたは他のユーザー インターフェイス (UI) での表示に適した、簡単でわかりやすい説明を取得します。 ( ProviderBase から継承されます。) |
![]() | Name | 構成時にプロバイダを参照するために使用される表示名を取得します。 ( ProviderBase から継承されます。) |

ProtectedConfigurationProvider メソッド

名前 | 説明 | |
---|---|---|
![]() | Decrypt | 構成ファイルから渡される XmlNode オブジェクトを復号化します。 |
![]() | Encrypt | 構成ファイルから渡される XmlNode オブジェクトを暗号化します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | Initialize | プロバイダを初期化します。 ( ProviderBase から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

ProtectedConfigurationProvider メンバ
保護された構成データの暗号化と復号化を行うプロバイダを作成する基本クラスです。
ProtectedConfigurationProvider データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | ProtectedConfigurationProvider | 既定の設定を使用して ProtectedConfigurationProvider クラスの新しいインスタンスを初期化します。 |

名前 | 説明 | |
---|---|---|
![]() | Description | 管理ツールまたは他のユーザー インターフェイス (UI) での表示に適した、簡単でわかりやすい説明を取得します。(ProviderBase から継承されます。) |
![]() | Name | 構成時にプロバイダを参照するために使用される表示名を取得します。(ProviderBase から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Decrypt | 構成ファイルから渡される XmlNode オブジェクトを復号化します。 |
![]() | Encrypt | 構成ファイルから渡される XmlNode オブジェクトを暗号化します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | Initialize | プロバイダを初期化します。 (ProviderBase から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からProtectedConfigurationProviderを検索する場合は、下記のリンクをクリックしてください。

- ProtectedConfigurationProviderのページへのリンク