語言

ProtectedData.Unprotect 方法

定義

解密指定緩衝區中的資料,並將解密後的資料寫入目的緩衝區。

多載

名稱 Description
Unprotect(Byte[], Byte[], DataProtectionScope)

解密指定位元組陣列中的資料,並回傳包含解密資料的位元組陣列。

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

解密指定位元組陣列中的資料,並回傳包含解密資料的位元組陣列。

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

解密指定緩衝區中的資料,並將解密後的資料寫入目的緩衝區。

Unprotect(Byte[], Byte[], DataProtectionScope)

來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
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+:僅支援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 該參數在加密時被使用,必須提供它來解密資料。

Note

若您在模擬過程中使用此方法,可能會收到以下錯誤訊息:「金鑰無法在指定狀態下使用。」為避免此錯誤,請先載入你想冒充的使用者設定檔,再呼叫該方法。

適用於

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

來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
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>)

來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
ProtectedData.cs
來源:
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。

適用於