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

適用対象