ProtectedMemory.Protect(Byte[], MemoryProtectionScope) Método

Definición

Protege los datos especificados.

public:
 static void Protect(cli::array <System::Byte> ^ userData, System::Security::Cryptography::MemoryProtectionScope scope);
public static void Protect (byte[] userData, System.Security.Cryptography.MemoryProtectionScope scope);
static member Protect : byte[] * System.Security.Cryptography.MemoryProtectionScope -> unit
Public Shared Sub Protect (userData As Byte(), scope As MemoryProtectionScope)

Parámetros

userData
Byte[]

La matriz de bytes que contiene los datos de la memoria que se van a proteger. La matriz debe ser un múltiplo de 16 bytes.

scope
MemoryProtectionScope

Uno de los valores de enumeración que especifica el ámbito de la protección de memoria.

Excepciones

userData debe tener una longitud de 16 bytes o múltiplo de 16.

El sistema operativo no admite este método. Este método sólo se puede utilizar con Windows 2000 o sistemas operativos posteriores.

userData es null.

Ejemplos

En el ejemplo de código 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

Este método se puede usar para proteger los datos en la memoria. Tenga en cuenta que el método no realiza una copia de los datos, pero cifra la matriz de bytes en su lugar. El userData parámetro debe tener una longitud de 16 bytes o un múltiplo de 16 bytes.

La compatibilidad con este método está disponible en los sistemas operativos Windows XP y versiones posteriores.

Se aplica a