ProtectedMemory.Protect(Byte[], MemoryProtectionScope) Метод

Определение

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

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)

Параметры

userData
Byte[]

Массив байтов, содержащий данные в памяти для защиты. Массив должен иметь несколько 16 байт.

scope
MemoryProtectionScope

Одно из значений перечисления, указывающее область защиты памяти.

Исключения

userData должно быть 16 байтов длиной или несколькими 16 байтами.

Операционная система не поддерживает этот метод. Этот метод можно использовать только с операционными системами Windows 2000 или более поздних версий.

userData равно null.

Примеры

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

#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

Комментарии

Этот метод можно использовать для защиты данных в памяти. Обратите внимание, что метод не делает копию данных, а шифрует массив байтов. Параметр userData должен иметь длину 16 байтов или 16 байт.

Поддержка этого метода доступна в Windows XP и более поздних операционных системах.

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