DataProtector 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為資料保護裝置提供基底類別。
public ref class DataProtector abstract
public abstract class DataProtector
type DataProtector = class
Public MustInherit Class DataProtector
- 繼承
-
DataProtector
- 衍生
範例
下列範例示範如何建立使用保護類別的數據保護裝置,以及額外的 Entropy 選項。 根據預設,類別 DataProtector 會將目的屬性的哈希加上要加密的數據。 呼叫具有額外 entropy 選項的數據保護裝置時,您可以將該功能關閉並使用哈希用途作為額外的 Entropy。
using System;
using System.Security.Permissions;
namespace System.Security.Cryptography
{
public sealed class MyDataProtector : DataProtector
{
public DataProtectionScope Scope { get; set; }
// This implementation gets the HashedPurpose from the base class and passes it as OptionalEntropy to ProtectedData.
// The default for DataProtector is to prepend the hash to the plain text, but because we are using the hash
// as OptionalEntropy there is no need to prepend it.
protected override bool PrependHashedPurposeToPlaintext
{
get
{
return false;
}
}
// To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
// in the constructor, but Assert the permission when ProviderProtect is called. This is similar to FileStream
// where access is checked at time of creation, not time of use.
[SecuritySafeCritical]
[DataProtectionPermission(SecurityAction.Assert, ProtectData = true)]
protected override byte[] ProviderProtect(byte[] userData)
{
// Delegate to ProtectedData
return ProtectedData.Protect(userData, GetHashedPurpose(), Scope);
}
// To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
// in the constructor, but Assert the permission when ProviderUnProtect is called. This is similar to FileStream
// where access is checked at time of creation, not time of use.
[SecuritySafeCritical]
[DataProtectionPermission(SecurityAction.Assert, UnprotectData = true)]
protected override byte[] ProviderUnprotect(byte[] encryptedData)
{
// Delegate to ProtectedData
return ProtectedData.Unprotect(encryptedData, GetHashedPurpose(), Scope);
}
public override bool IsReprotectRequired(byte[] encryptedData)
{
// For now, this cannot be determined, so always return true;
return true;
}
// Public constructor
// The Demand for DataProtectionPermission is in the constructor because we Assert this permission
// in the ProviderProtect/ProviderUnprotect methods.
[DataProtectionPermission(SecurityAction.Demand, Unrestricted = true)]
[SecuritySafeCritical]
public MyDataProtector(string appName, string primaryPurpose, params string[] specificPurpose)
: base(appName, primaryPurpose, specificPurpose)
{
}
}
}
Imports System.Security
Imports System.Security.Cryptography
Imports System.Security.Permissions
Public NotInheritable Class MyDataProtector
Inherits DataProtector
Public Property Scope() As DataProtectionScope
Get
Return Scope
End Get
Set(value As DataProtectionScope)
End Set
End Property ' This implementation gets the HashedPurpose from the base class and passes it as OptionalEntropy to ProtectedData.
' The default for DataProtector is to prepend the hash to the plain text, but because we are using the hash
' as OptionalEntropy there is no need to prepend it.
Protected Overrides ReadOnly Property PrependHashedPurposeToPlaintext() As Boolean
Get
Return False
End Get
End Property
' To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
' in the constructor, but Assert the permission when ProviderProtect is called. This is similar to FileStream
' where access is checked at time of creation, not time of use.
<SecuritySafeCritical(), DataProtectionPermission(SecurityAction.Assert, ProtectData:=True)> _
Protected Overrides Function ProviderProtect(ByVal userData() As Byte) As Byte()
' Delegate to ProtectedData
Return ProtectedData.Protect(userData, GetHashedPurpose(), Scope)
End Function 'ProviderProtect
' To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
' in the constructor, but Assert the permission when ProviderUnProtect is called. This is similar to FileStream
' where access is checked at time of creation, not time of use.
<SecuritySafeCritical(), DataProtectionPermission(SecurityAction.Assert, UnprotectData:=True)> _
Protected Overrides Function ProviderUnprotect(ByVal encryptedData() As Byte) As Byte()
' Delegate to ProtectedData
Return ProtectedData.Unprotect(encryptedData, GetHashedPurpose(), Scope)
End Function 'ProviderUnprotect
Public Overrides Function IsReprotectRequired(ByVal encryptedData() As Byte) As Boolean
' For now, this cannot be determined, so always return true;
Return True
End Function 'IsReprotectRequired
' Public constructor
' The Demand for DataProtectionPermission is in the constructor because we Assert this permission
' in the ProviderProtect/ProviderUnprotect methods.
<DataProtectionPermission(SecurityAction.Demand, Unrestricted:=True), SecuritySafeCritical()> _
Public Sub New(ByVal appName As String, ByVal primaryPurpose As String, ParamArray specificPurpose() As String)
MyBase.New(appName, primaryPurpose, specificPurpose)
End Sub
End Class
下列範例示範使用 PrependHashedPurposeToPlaintext 類別功能的 DataProtector 簡單數據保護裝置。
using System;
using System.Security.Permissions;
namespace System.Security.Cryptography
{
public sealed class MemoryProtector : DataProtector
{
public MemoryProtectionScope Scope { get; set; }
protected override bool PrependHashedPurposeToPlaintext
{
get
{
// Signal the DataProtector to prepend the hash of the purpose to the data.
return true;
}
}
// To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
// in the constructor, but Assert the permission when ProviderProtect is called. This is similar to FileStream
// where access is checked at time of creation, not time of use.
[SecuritySafeCritical]
[DataProtectionPermission(SecurityAction.Assert, ProtectData = true)]
protected override byte[] ProviderProtect(byte[] userData)
{
// Delegate to ProtectedData
ProtectedMemory.Protect(userData, Scope);
return userData;
}
// To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
// in the constructor, but Assert the permission when ProviderUnprotect is called.. This is similar to FileStream
// where access is checked at time of creation, not time of use.
[SecuritySafeCritical]
[DataProtectionPermission(SecurityAction.Assert, UnprotectData = true)]
protected override byte[] ProviderUnprotect(byte[] encryptedData)
{
ProtectedMemory.Unprotect(encryptedData,Scope);
return encryptedData;
}
public override bool IsReprotectRequired(byte[] encryptedData)
{
// For now, this cannot be determined so always return true.
return true;
}
// Public constructor
// The Demand for DataProtectionPermission is in the constructor because we Assert this permission
// in the ProviderProtect/ProviderUnprotect methods.
[DataProtectionPermission(SecurityAction.Demand, Unrestricted = true)]
[SecuritySafeCritical]
public MemoryProtector(string appName, string primaryPurpose, params string[] specificPurpose)
: base(appName, primaryPurpose, specificPurpose)
{
}
}
}
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Cryptography
Public NotInheritable Class MemoryProtector
Inherits DataProtector
Public Property Scope() As MemoryProtectionScope
Get
Return Scope
End Get
Set(value As MemoryProtectionScope)
End Set
End Property
Protected Overrides ReadOnly Property PrependHashedPurposeToPlaintext() As Boolean
Get
' Signal the DataProtector to prepend the hash of the purpose to the data.
Return True
End Get
End Property
' To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
' in the constructor, but Assert the permission when ProviderProtect is called. This is similar to FileStream
' where access is checked at time of creation, not time of use.
<SecuritySafeCritical(), DataProtectionPermission(SecurityAction.Assert, ProtectData:=True)> _
Protected Overrides Function ProviderProtect(ByVal userData() As Byte) As Byte()
' Delegate to ProtectedData
ProtectedMemory.Protect(userData, Scope)
Return userData
End Function 'ProviderProtect
' To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
' in the constructor, but Assert the permission when ProviderUnprotect is called.. This is similar to FileStream
' where access is checked at time of creation, not time of use.
<SecuritySafeCritical(), DataProtectionPermission(SecurityAction.Assert, UnprotectData:=True)> _
Protected Overrides Function ProviderUnprotect(ByVal encryptedData() As Byte) As Byte()
ProtectedMemory.Unprotect(encryptedData, Scope)
Return encryptedData
End Function 'ProviderUnprotect
Public Overrides Function IsReprotectRequired(ByVal encryptedData() As Byte) As Boolean
' For now, this cannot be determined so always return true.
Return True
End Function 'IsReprotectRequired
' Public constructor
' The Demand for DataProtectionPermission is in the constructor because we Assert this permission
' in the ProviderProtect/ProviderUnprotect methods.
<DataProtectionPermission(SecurityAction.Demand, Unrestricted:=True), SecuritySafeCritical()> _
Public Sub New(ByVal appName As String, ByVal primaryPurpose As String, ParamArray specificPurpose() As String)
MyBase.New(appName, primaryPurpose, specificPurpose)
End Sub
End Class
備註
此類別可保護儲存的數據,防止檢視和竄改。 建立這個類別的實例,並使用用來保護數據的確切用途字串,來取得受保護數據的存取權。 呼叫端不需要金鑰來保護或取消保護數據。 金鑰是由加密演算法所提供。
衍生類別必須覆寫 基類所呼叫的 ProviderProtect 和 Unprotect 方法 DataProtector 。 它們也必須覆寫 IsReprotectRequired 方法,在應用程式重新整理儲存加密文字的資料庫時,一律會傳回 true
效率可能很小的損失。 衍生類別應該提供可呼叫基類建構函式的建構函式,這個建構函式會設定 ApplicationName、 SpecificPurposes和 PrimaryPurpose 屬性。
建構函式
DataProtector(String, String, String[]) |
使用提供的應用程式名稱、主要用途和特定用途,建立的新執行個體 DataProtector 類別。 |
屬性
ApplicationName |
取得應用程式的名稱。 |
PrependHashedPurposeToPlaintext |
指定是否已在加密前將雜湊預先附加至文字陣列。 |
PrimaryPurpose |
取得受保護資料的主要目的。 |
SpecificPurposes |
取得受保護資料之特定用途。 |
方法
Create(String, String, String, String[]) |
使用指定的資料保護裝置名稱、應用程式名稱、主要用途和特定用途,建立資料保護裝置實作的執行個體。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetHashedPurpose() |
建立建構函式所指定之屬性值的雜湊。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsReprotectRequired(Byte[]) |
判斷指定的已加密資料是否需要重新加密。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Protect(Byte[]) |
保護指定的使用者資料。 |
ProviderProtect(Byte[]) |
指定衍生類別中的委派方法,基底類別中的 Protect(Byte[]) 方法會呼叫回此方法中。 |
ProviderUnprotect(Byte[]) |
指定衍生類別中的委派方法,基底類別中的 Unprotect(Byte[]) 方法會呼叫回此方法中。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
Unprotect(Byte[]) |
取消保護指定的受保護資料。 |