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 байтам.

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

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 и более поздних версий.

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