ProtectedMemory.Protect(Byte[], MemoryProtectionScope) 方法
定义
保护指定数据。Protects the specified data.
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)
参数
- userData
- Byte[]
一个字节数组,包含要保护的内存中的数据。The byte array containing data in memory to protect. 该数组必须是 16 字节的倍数。The array must be a multiple of 16 bytes.
- scope
- MemoryProtectionScope
指定内存保护范围的枚举值之一。One of the enumeration values that specifies the scope of memory protection.
例外
userData 的长度必须是 16 字节或 16 字节的倍数。userData must be 16 bytes in length or in multiples of 16 bytes.
该操作系统不支持此方法。The operating system does not support this method. 此方法仅可用于 Windows 2000 或更高版本的操作系统。This method can be used only with the Windows 2000 or later operating systems.
userData 为 null。userData is null.
示例
下面的代码示例演示如何使用数据保护。The following code example shows how to use data protection.
#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
注解
此方法可用于保护内存中的数据。This method can be used to protect data in memory. 请注意,方法不会创建数据的副本,但会就地加密字节数组。Note that the method does not make a copy of the data, but encrypts the byte array in place. userData参数的长度必须为16字节或16字节的倍数。The userData parameter must be 16 bytes in length or a multiple of 16 bytes.
Windows XP 和更高版本的操作系统中提供对此方法的支持。Support for this method is available in the Windows XP and later operating systems.