ProtectedMemory Класс

Определение

Предоставляет методы для защиты данных, хранящихся в оперативной памяти, и снятия защиты с них. Этот класс не наследуется.

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

Примеры

В следующем примере показано, как использовать защиту данных.

#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

Комментарии

Этот класс предоставляет доступ к API защиты данных (DPAPI), доступному в операционных системах Windows XP и более поздних версий. Это служба, предоставляемая операционной системой и не требующая дополнительных библиотек. Он обеспечивает шифрование конфиденциальных данных в памяти.

Класс состоит из двух оболочек для неуправляемого DPAPI Protect и Unprotect. Эти два метода можно использовать для шифрования и расшифровки данных в памяти.

Методы

Protect(Byte[], MemoryProtectionScope)

Защищает заданные данные.

Unprotect(Byte[], MemoryProtectionScope)

Снимает защиту с данных, хранящихся в оперативной памяти и защищенных с помощью метода Protect(Byte[], MemoryProtectionScope).

Применяется к