ProtectedData.Protect 方法

定义

加密指定缓冲区中的数据,并将加密数据写入目标缓冲区。

重载

名称 说明
Protect(Byte[], Byte[], DataProtectionScope)

加密指定字节数组中的数据,并返回包含加密数据的字节数组。

Protect(ReadOnlySpan<Byte>, DataProtectionScope, ReadOnlySpan<Byte>)

加密指定字节范围中的数据,并返回包含加密数据的字节数组。

Protect(ReadOnlySpan<Byte>, DataProtectionScope, Span<Byte>, ReadOnlySpan<Byte>)

加密指定缓冲区中的数据,并将加密数据写入目标缓冲区。

Protect(Byte[], Byte[], DataProtectionScope)

Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs

加密指定字节数组中的数据,并返回包含加密数据的字节数组。

public:
 static cli::array <System::Byte> ^ Protect(cli::array <System::Byte> ^ userData, cli::array <System::Byte> ^ optionalEntropy, System::Security::Cryptography::DataProtectionScope scope);
public static byte[] Protect(byte[] userData, byte[]? optionalEntropy, System.Security.Cryptography.DataProtectionScope scope);
public static byte[] Protect(byte[] userData, byte[] optionalEntropy, System.Security.Cryptography.DataProtectionScope scope);
static member Protect : byte[] * byte[] * System.Security.Cryptography.DataProtectionScope -> byte[]
Public Shared Function Protect (userData As Byte(), optionalEntropy As Byte(), scope As DataProtectionScope) As Byte()

参数

userData
Byte[]

包含要加密的数据的字节数组。

optionalEntropy
Byte[]

一个可选的附加字节数组,用于增加加密的复杂性,或者 null 没有额外的复杂性。

scope
DataProtectionScope

指定加密范围的枚举值之一。

返回

Byte[]

表示加密数据的字节数组。

例外

参数 userDatanull.

加密失败。

操作系统不支持此方法。

加密数据时,系统内存不足。

.NET Core 和 .NET 5+ 5 以上:仅Windows操作系统支持对 Protect 方法的调用。

示例

以下示例演示如何使用数据保护。

using System;
using System.Security.Cryptography;

public class DataProtectionSample
{
    // Create byte array for additional entropy when using Protect method.
    static byte [] s_additionalEntropy = { 9, 8, 7, 6, 5 };

    public static void Main()
    {
        // Create a simple byte array containing data to be encrypted.
        byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };

        //Encrypt the data.
        byte [] encryptedSecret = Protect( secret );
        Console.WriteLine("The encrypted byte array is:");
        PrintValues(encryptedSecret);

        // Decrypt the data and store in a byte array.
        byte [] originalData = Unprotect( encryptedSecret );
        Console.WriteLine("{0}The original data is:", Environment.NewLine);
        PrintValues(originalData);
    }

    public static byte [] Protect( byte [] data )
    {
        try
        {
            // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
            // only by the same current user.
            return ProtectedData.Protect( data, s_additionalEntropy, DataProtectionScope.CurrentUser );
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("Data was not encrypted. An error occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
    }

    public static byte [] Unprotect( byte [] data )
    {
        try
        {
            //Decrypt the data using DataProtectionScope.CurrentUser.
            return ProtectedData.Unprotect( data, s_additionalEntropy, DataProtectionScope.CurrentUser );
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("Data was not decrypted. An error occurred.");
            Console.WriteLine(e.ToString());
            return null;
        }
    }

    public static void PrintValues( Byte[] myArr )
    {
        foreach ( Byte i in myArr )
        {
            Console.Write( "\t{0}", i );
        }
        Console.WriteLine();
    }
}
Imports System.Security.Cryptography



Public Class DataProtectionSample
    ' Create byte array for additional entropy when using Protect method.
    Private Shared s_additionalEntropy As Byte() = {9, 8, 7, 6, 5}


    Public Shared Sub Main()
        ' Create a simple byte array containing data to be encrypted.
        Dim secret As Byte() = {0, 1, 2, 3, 4, 1, 2, 3, 4}

        'Encrypt the data.
        Dim encryptedSecret As Byte() = Protect(secret)
        Console.WriteLine("The encrypted byte array is:")
        PrintValues(encryptedSecret)

        ' Decrypt the data and store in a byte array.
        Dim originalData As Byte() = Unprotect(encryptedSecret)
        Console.WriteLine("{0}The original data is:", Environment.NewLine)
        PrintValues(originalData)

    End Sub


    Public Shared Function Protect(ByVal data() As Byte) As Byte()
        Try
            ' Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
            '  only by the same current user.
            Return ProtectedData.Protect(data, s_additionalEntropy, DataProtectionScope.CurrentUser)
        Catch e As CryptographicException
            Console.WriteLine("Data was not encrypted. An error occurred.")
            Console.WriteLine(e.ToString())
            Return Nothing
        End Try

    End Function


    Public Shared Function Unprotect(ByVal data() As Byte) As Byte()
        Try
            'Decrypt the data using DataProtectionScope.CurrentUser.
            Return ProtectedData.Unprotect(data, s_additionalEntropy, DataProtectionScope.CurrentUser)
        Catch e As CryptographicException
            Console.WriteLine("Data was not decrypted. An error occurred.")
            Console.WriteLine(e.ToString())
            Return Nothing
        End Try

    End Function


    Public Shared Sub PrintValues(ByVal myArr() As [Byte])
        Dim i As [Byte]
        For Each i In myArr
            Console.Write(vbTab + "{0}", i)
        Next i
        Console.WriteLine()

    End Sub
End Class

注解

此方法可用于加密密码、密钥或连接字符串等数据。 该 optionalEntropy 参数使你可以添加数据以提高加密的复杂性;指定 null 无其他复杂性。 如果提供了此信息,则还必须在使用该方法解密数据时使用此信息 Unprotect

注意

如果在模拟期间使用此方法,可能会收到以下错误:“密钥在指定状态下无效。若要防止此错误,在调用该方法之前加载要模拟的用户的配置文件。

适用于

Protect(ReadOnlySpan<Byte>, DataProtectionScope, ReadOnlySpan<Byte>)

Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs

加密指定字节范围中的数据,并返回包含加密数据的字节数组。

public static byte[] Protect(ReadOnlySpan<byte> userData, System.Security.Cryptography.DataProtectionScope scope, ReadOnlySpan<byte> optionalEntropy = default);
static member Protect : ReadOnlySpan<byte> * System.Security.Cryptography.DataProtectionScope * ReadOnlySpan<byte> -> byte[]
Public Shared Function Protect (userData As ReadOnlySpan(Of Byte), scope As DataProtectionScope, Optional optionalEntropy As ReadOnlySpan(Of Byte) = Nothing) As Byte()

参数

userData
ReadOnlySpan<Byte>

包含要加密的数据的缓冲区。

scope
DataProtectionScope

指定加密范围的枚举值之一。

optionalEntropy
ReadOnlySpan<Byte>

一个可选的附加字节范围,用于增加加密的复杂性,或为空,没有额外的复杂性。

返回

Byte[]

表示加密数据的字节数组。

例外

加密失败。

操作系统不支持此方法。

加密数据时,系统内存不足。

操作系统未Windows。

适用于

Protect(ReadOnlySpan<Byte>, DataProtectionScope, Span<Byte>, ReadOnlySpan<Byte>)

Source:
ProtectedData.cs
Source:
ProtectedData.cs
Source:
ProtectedData.cs

加密指定缓冲区中的数据,并将加密数据写入目标缓冲区。

public static int Protect(ReadOnlySpan<byte> userData, System.Security.Cryptography.DataProtectionScope scope, Span<byte> destination, ReadOnlySpan<byte> optionalEntropy = default);
static member Protect : ReadOnlySpan<byte> * System.Security.Cryptography.DataProtectionScope * Span<byte> * ReadOnlySpan<byte> -> int
Public Shared Function Protect (userData As ReadOnlySpan(Of Byte), scope As DataProtectionScope, destination As Span(Of Byte), Optional optionalEntropy As ReadOnlySpan(Of Byte) = Nothing) As Integer

参数

userData
ReadOnlySpan<Byte>

包含要加密的数据的缓冲区。

scope
DataProtectionScope

指定加密范围的枚举值之一。

destination
Span<Byte>

用于接收加密数据的缓冲区。

optionalEntropy
ReadOnlySpan<Byte>

用于增加加密复杂性的可选附加缓冲区,或者为空,无需额外的复杂性。

返回

写入到的字节总数 destination

例外

缓冲区 destination 太小,无法保存加密的数据。

加密失败。

操作系统不支持此方法。

加密数据时,系统内存不足。

操作系统未Windows。

适用于