Share via


ProtectedMemory Kelas

Definisi

Menyediakan metode untuk melindungi dan membuka proteksi memori. Kelas ini tidak dapat diwariskan.

public ref class ProtectedMemory sealed
public ref class ProtectedMemory abstract sealed
public sealed class ProtectedMemory
public static class ProtectedMemory
type ProtectedMemory = class
Public NotInheritable Class ProtectedMemory
Public Class ProtectedMemory
Warisan
ProtectedMemory

Contoh

Contoh 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

Kelas ini menyediakan akses ke Api Perlindungan Data (DPAPI) yang tersedia di Windows XP dan sistem operasi yang lebih baru. Ini adalah layanan yang disediakan oleh sistem operasi dan tidak memerlukan pustaka tambahan. Ini menyediakan enkripsi untuk data sensitif dalam memori.

Kelas ini terdiri dari dua pembungkus untuk DPAPI yang tidak dikelola, Protect dan Unprotect. Kedua metode ini dapat digunakan untuk mengenkripsi dan mendekripsi data dalam memori.

Metode

Protect(Byte[], MemoryProtectionScope)

Melindungi data yang ditentukan.

Unprotect(Byte[], MemoryProtectionScope)

Membuka proteksi data dalam memori yang dilindungi menggunakan metode .Protect(Byte[], MemoryProtectionScope)

Berlaku untuk