语言

ProtectedData.Unprotect 方法

定义

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

重载

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

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

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

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

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

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

Unprotect(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> ^ Unprotect(cli::array <System::Byte> ^ encryptedData, cli::array <System::Byte> ^ optionalEntropy, System::Security::Cryptography::DataProtectionScope scope);
public static byte[] Unprotect(byte[] encryptedData, byte[]? optionalEntropy, System.Security.Cryptography.DataProtectionScope scope);
public static byte[] Unprotect(byte[] encryptedData, byte[] optionalEntropy, System.Security.Cryptography.DataProtectionScope scope);
static member Unprotect : byte[] * byte[] * System.Security.Cryptography.DataProtectionScope -> byte[]
Public Shared Function Unprotect (encryptedData As Byte(), optionalEntropy As Byte(), scope As DataProtectionScope) As Byte()

参数

encryptedData
Byte[]

包含使用该方法加密的数据的 Protect(Byte[], Byte[], DataProtectionScope) 字节数组。

optionalEntropy
Byte[]

用于加密数据的可选附加字节数组,或者 null 未使用其他字节数组。

scope
DataProtectionScope

枚举值之一,指定用于加密数据的数据保护范围。

返回

Byte[]

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

例外

参数 encryptedData 为 null.

解密失败。

操作系统不支持此方法。

内存不足。

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

示例

下面的代码示例演示如何使用数据保护。

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

注解

此方法可用于取消保护使用 Protect 该方法加密的数据。 optionalEntropy如果在加密期间使用了该参数,则必须提供该参数以取消加密数据。

注释

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

适用于

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

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

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

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

参数

encryptedData
ReadOnlySpan<Byte>

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

scope
DataProtectionScope

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

optionalEntropy
ReadOnlySpan<Byte>

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

返回

Byte[]

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

例外

加密失败。

操作系统不支持此方法。

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

操作系统未Windows。

适用于

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

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

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

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

参数

encryptedData
ReadOnlySpan<Byte>

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

scope
DataProtectionScope

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

destination
Span<Byte>

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

optionalEntropy
ReadOnlySpan<Byte>

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

返回

写入到的字节总数 destination

例外

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

加密失败。

操作系统不支持此方法。

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

操作系统未Windows。

适用于