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

メモリ保護のスコープを指定する列挙値の 1 つ。

例外

userData の長さは 16 バイトか、16 バイトの倍数である必要があります。

このメソッドがオペレーティング システムでサポートされていません。 このメソッドは、Windows 2000 以降のオペレーティング システムでのみ使用できます。

userDatanullです。

次のコード例は、データ保護の使用方法を示しています。

#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 以降のオペレーティング システムで使用できます。

適用対象