ProtectedMemory 类

定义

提供保护内存和取消内存保护的方法。Provides methods for protecting and unprotecting memory. 此类不能被继承。This class cannot be inherited.

public ref class ProtectedMemory sealed
public sealed class ProtectedMemory
type ProtectedMemory = class
Public NotInheritable Class ProtectedMemory
继承
ProtectedMemory

示例

下面的示例演示如何使用数据保护。The following example shows how to use data protection.

#using <System.Security.dll>

using namespace System;
using namespace System::Security::Cryptography;

int main()
{
   
   // Create the original data to be encrypted (The data length should be a multiple of 16).
   array<Byte>^secret = {1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4};
   
   // Encrypt the data in memory. The result is stored in the same array as the original data.
   ProtectedMemory::Protect( secret, MemoryProtectionScope::SameLogon );
   
   // Decrypt the data in memory and store in the original array.
   ProtectedMemory::Unprotect( secret, MemoryProtectionScope::SameLogon );
}
using System;
using System.Security.Cryptography;

public class MemoryProtectionSample
{

    public static void Main()
    {
        // Create the original data to be encrypted (The data length should be a multiple of 16).
        byte [] secret = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

        // Encrypt the data in memory. The result is stored in the same array as the original data.
        ProtectedMemory.Protect( secret, MemoryProtectionScope.SameLogon );

        // Decrypt the data in memory and store in the original array.
        ProtectedMemory.Unprotect( secret, MemoryProtectionScope.SameLogon );
    }
}
Imports System.Security.Cryptography

Public Class MemoryProtectionSample

    Public Shared Sub Main()
        ' Create the original data to be encrypted (The data length should be a multiple of 16).
        Dim secret As Byte() = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}

        ' Encrypt the data in memory. The result is stored in the same array as the original data.
        ProtectedMemory.Protect(secret, MemoryProtectionScope.SameLogon)

        ' Decrypt the data in memory and store in the original array.
        ProtectedMemory.Unprotect(secret, MemoryProtectionScope.SameLogon)

    End Sub
End Class

注解

此类提供对 Windows XP 和更高版本的操作系统中可用的数据保护 API (DPAPI) 的访问权限。This class provides access to the Data Protection API (DPAPI) available in the Windows XP and later operating systems. 这是由操作系统提供的服务, 不需要其他库。This is a service that is provided by the operating system and does not require additional libraries. 它为内存中的敏感数据提供加密。It provides encryption for sensitive data in memory.

此类包含两个非托管 DPAPI ProtectUnprotect的包装器。The class consists of two wrappers for the unmanaged DPAPI, Protect and Unprotect. 这两种方法可用于对内存中的数据进行加密和解密。These two methods can be used to encrypt and decrypt data in memory.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Protect(Byte[], MemoryProtectionScope)

保护指定数据。Protects the specified data.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
Unprotect(Byte[], MemoryProtectionScope)

取消对使用 Protect(Byte[], MemoryProtectionScope) 方法保护的内存中的数据的保护。Unprotects data in memory that was protected using the Protect(Byte[], MemoryProtectionScope) method.

适用于