ProtectedMemory 类

定义

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

public ref class ProtectedMemory sealed
public ref class ProtectedMemory abstract sealed
public sealed class ProtectedMemory
public static class ProtectedMemory
type ProtectedMemory = class
Public NotInheritable Class ProtectedMemory
Public 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 和的包装 ProtectUnprotectThe 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.

方法

Protect(Byte[], MemoryProtectionScope)

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

Unprotect(Byte[], MemoryProtectionScope)

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

适用于