Share via


ProtectedMemory Classe

Definizione

Fornisce metodi per applicare e rimuovere la protezione della memoria. La classe non può essere ereditata.

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
Ereditarietà
ProtectedMemory

Esempio

Nell'esempio seguente viene illustrato come usare la protezione dei dati.

#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

Commenti

Questa classe fornisce l'accesso all'API Protezione dati (DPAPI) disponibile in Windows XP e nei sistemi operativi successivi. Si tratta di un servizio fornito dal sistema operativo e non richiede librerie aggiuntive. Fornisce la crittografia per i dati sensibili in memoria.

La classe è costituita da due wrapper per DPAPI non gestito e ProtectUnprotect. Questi due metodi possono essere usati per crittografare e decrittografare i dati in memoria.

Metodi

Protect(Byte[], MemoryProtectionScope)

Protegge i dati specificati.

Unprotect(Byte[], MemoryProtectionScope)

Rimuove la protezione dei dati in memoria precedentemente protetti tramite il metodo Protect(Byte[], MemoryProtectionScope).

Si applica a