ProtectedMemory Klasa

Definicja

Zapewnia metody ochrony i niechronienia pamięci. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
ProtectedMemory

Przykłady

W poniższym przykładzie pokazano, jak korzystać z ochrony danych.

#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

Uwagi

Ta klasa zapewnia dostęp do interfejsu API ochrony danych (DPAPI) dostępnego w systemach operacyjnych Windows XP i nowszych. Jest to usługa dostarczana przez system operacyjny i nie wymaga dodatkowych bibliotek. Zapewnia szyfrowanie poufnych danych w pamięci.

Klasa składa się z dwóch otoek niezarządzanych interfejsów DPAPI Protect i Unprotect. Te dwie metody mogą służyć do szyfrowania i odszyfrowywania danych w pamięci.

Metody

Protect(Byte[], MemoryProtectionScope)

Chroni określone dane.

Unprotect(Byte[], MemoryProtectionScope)

Usuwa ochronę danych w pamięci chronionej Protect(Byte[], MemoryProtectionScope) przy użyciu metody .

Dotyczy