MemoryProtectionScope 列舉

定義

指定 Protect(Byte[], MemoryProtectionScope) 方法要套用的記憶體保護範圍。

public enum class MemoryProtectionScope
public enum MemoryProtectionScope
type MemoryProtectionScope = 
Public Enum MemoryProtectionScope
繼承
MemoryProtectionScope

欄位

CrossProcess 1

任何處理序中的所有程式碼都可以解除原本以 Protect(Byte[], MemoryProtectionScope) 方法保護的記憶體。

SameLogon 2

程式碼只有在與呼叫 Protect(Byte[], MemoryProtectionScope) 方法的程式碼位於同一個使用者內容中執行時,才能夠解除記憶體受到的保護。

SameProcess 0

程式碼只有在與呼叫 Protect(Byte[], MemoryProtectionScope) 方法的程式碼位於同一個處理序內時,才能夠解除記憶體受到的保護。

範例

下列程式碼範例示範如何使用資料保護。

#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

備註

這個列舉會搭配 ProtectUnprotect 方法來保護記憶體中的資料。

適用於