Share via


ProtectedMemory.Protect(Byte[], MemoryProtectionScope) Metode

Definisi

Melindungi data yang ditentukan.

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)

Parameter

userData
Byte[]

Array byte yang berisi data dalam memori untuk dilindungi. Array harus kelipatan 16 byte.

scope
MemoryProtectionScope

Salah satu nilai enumerasi yang menentukan cakupan perlindungan memori.

Pengecualian

userData panjangnya harus 16 byte atau dalam kelipatan 16 byte.

Sistem operasi tidak mendukung metode ini. Metode ini hanya dapat digunakan dengan sistem operasi Windows 2000 atau yang lebih baru.

userDataadalah null.

Contoh

Contoh kode berikut menunjukkan cara menggunakan perlindungan data.

#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

Keterangan

Metode ini dapat digunakan untuk melindungi data dalam memori. Perhatikan bahwa metode ini tidak membuat salinan data, tetapi mengenkripsi array byte di tempatnya. Panjang userData parameter harus 16 byte atau kelipatan 16 byte.

Dukungan untuk metode ini tersedia di Windows XP dan sistem operasi yang lebih baru.

Berlaku untuk