ProtectedConfigurationProvider クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ProtectedConfigurationProvider クラスの意味・解説 

ProtectedConfigurationProvider クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

保護され構成データ暗号化と復号化を行うプロバイダ作成する基本クラスです。

名前空間: System.Configuration
アセンブリ: System.Configuration (system.configuration.dll 内)
構文構文

Public MustInherit Class
 ProtectedConfigurationProvider
    Inherits ProviderBase
Dim instance As ProtectedConfigurationProvider
public abstract class ProtectedConfigurationProvider
 : ProviderBase
public ref class ProtectedConfigurationProvider
 abstract : public ProviderBase
public abstract class ProtectedConfigurationProvider
 extends ProviderBase
public abstract class ProtectedConfigurationProvider
 extends ProviderBase
解説解説

構成ファイルセクション暗号化して、アプリケーション使用する機密情報保護できます。これにより、たとえ攻撃者構成ファイルアクセスできたとしても、未承認アクセスを行うのが難しくなるため、セキュリティ向上します

.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.Object
   System.Configuration.Provider.ProviderBase
    System.Configuration.ProtectedConfigurationProvider
       System.Configuration.DpapiProtectedConfigurationProvider
       System.Configuration.RsaProtectedConfigurationProvider
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ProtectedConfigurationProvider メンバ
System.Configuration 名前空間
ProtectedConfigurationProviderCollection
ProtectedConfiguration クラス
ProtectedConfigurationSection
DpapiProtectedConfigurationProvider クラス
RSAProtectedConfigurationProvider
その他の技術情報
保護され構成使用した構成情報の暗号化



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ProtectedConfigurationProvider クラス」の関連用語

ProtectedConfigurationProvider クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ProtectedConfigurationProvider クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS