ProtectedMemory 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供保護和解除記憶體保護的方法。 此類別無法獲得繼承。
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
- 繼承
-
ProtectedMemory
範例
下列範例示範如何使用數據保護。
#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
備註
這個類別可讓您存取 Windows XP 和更新版本的作業系統中提供的數據保護 API (DPAPI) 。 這是操作系統所提供的服務,不需要額外的連結庫。 它會為記憶體中的敏感數據提供加密。
類別是由 Unmanaged DPAPI Protect 和 Unprotect的兩個包裝函式所組成。 這兩種方法可用來加密和解密記憶體中的數據。
方法
Protect(Byte[], MemoryProtectionScope) |
保護指定的資料。 |
Unprotect(Byte[], MemoryProtectionScope) |
解除保護記憶體中使用 Protect(Byte[], MemoryProtectionScope) 方法所保護的資料。 |