DataProtector 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供数据保护程序的基类。
public ref class DataProtector abstract
public abstract class DataProtector
type DataProtector = class
Public MustInherit Class DataProtector
- 继承
-
DataProtector
- 派生
示例
以下示例演示如何创建一个数据保护程序,该保护器使用具有额外熵选项的保护类。 默认情况下, DataProtector 类将用途属性的哈希附加到要加密的数据。 在调用具有额外熵选项的数据保护器时,可以关闭该功能并将哈希用途用作额外熵。
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 基类调用回的 DataProtector 和 Unprotect 方法。 它们还必须重写 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[]) |
取消对指定保护数据保护。 |