ProtectedMemory Classe

Définition

Fournit des méthodes pour protéger et déprotéger la mémoire. Cette classe ne peut pas être héritée.

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
Héritage
ProtectedMemory

Exemples

L’exemple suivant montre comment utiliser la protection des données.

#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

Remarques

Cette classe fournit l’accès à l’API de protection des données (DPAPI) disponible dans les systèmes d’exploitation Windows XP et ultérieurs. Il s’agit d’un service fourni par le système d’exploitation et qui ne nécessite pas de bibliothèques supplémentaires. Il fournit un chiffrement pour les données sensibles en mémoire.

La classe se compose de deux wrappers pour le DPAPI non managé et Protect Unprotect. Ces deux méthodes peuvent être utilisées pour chiffrer et déchiffrer des données en mémoire.

Méthodes

Protect(Byte[], MemoryProtectionScope)

Protège les données spécifiées.

Unprotect(Byte[], MemoryProtectionScope)

Déprotège les données en mémoire qui étaient protégées à l'aide de la méthode Protect(Byte[], MemoryProtectionScope).

S’applique à