DataProtector 类

定义

提供数据保护程序的基类。

C#
public abstract class DataProtector
继承
DataProtector
派生

示例

以下示例演示如何创建一个数据保护程序,该保护器使用具有额外熵选项的保护类。 默认情况下, DataProtector 类将用途属性的哈希附加到要加密的数据。 在调用具有额外熵选项的数据保护器时,可以关闭该功能并将哈希用途用作额外熵。

C#
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)
        {
        }
    }
}

以下示例演示了一个使用 PrependHashedPurposeToPlaintext 类功能的 DataProtector 简单数据保护程序。

C#
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)
        {
        }
    }
}

注解

此类可防止查看和篡改存储的数据。 可以通过创建此类的实例并使用用于保护数据的确切用途字符串来获取对受保护数据的访问权限。 调用方不需要密钥来保护或取消保护数据。 密钥由加密算法提供。

派生类必须重写 ProviderProtect 基类调用回的 DataProtectorUnprotect 方法。 它们还必须重写 IsReprotectRequired 方法,当应用程序刷新其存储的密码文本数据库时,该方法始终可能会返回 true 潜在的少量效率损失。 派生类应提供一个构造函数,用于调用基类构造函数,该构造函数设置 ApplicationNameSpecificPurposesPrimaryPurpose 属性。

构造函数

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[])

取消对指定保护数据保护。

适用于

产品 版本
.NET Framework 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