ProtectedMemory Clase

Definición

Proporciona métodos para proteger y desproteger la memoria. Esta clase no puede heredarse.

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

Ejemplos

En el ejemplo siguiente se muestra cómo usar la protección de datos.

#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

Comentarios

Esta clase proporciona acceso a la API de protección de datos (DPAPI) disponible en los sistemas operativos Windows XP y versiones posteriores. Se trata de un servicio proporcionado por el sistema operativo y no requiere bibliotecas adicionales. Proporciona cifrado para datos confidenciales en la memoria.

La clase consta de dos contenedores para la DPAPI no administrada y Protect Unprotect. Estos dos métodos se pueden usar para cifrar y descifrar datos en memoria.

Métodos

Protect(Byte[], MemoryProtectionScope)

Protege los datos especificados.

Unprotect(Byte[], MemoryProtectionScope)

Desprotege los datos de la memoria que se protegieron utilizando el método Protect(Byte[], MemoryProtectionScope).

Se aplica a