ProtectedData 類別

定義

提供加密和解密資料的方法。 此類別無法獲得繼承。

C#
public static class ProtectedData
C#
public sealed class ProtectedData
繼承
ProtectedData

範例

下列範例示範如何使用數據保護。

C#
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();
    }
}

備註

這個類別可讓您存取 Windows 作業系統中提供的數據保護 API (DPAPI) 。 這是操作系統所提供的服務,不需要額外的連結庫。 它提供使用使用者或計算機認證來加密或解密數據的保護。

重要

因為其相依於 DPAPI,所以 ProtectedData 只有 Windows 平臺才支援 類別。 它在 Windows 以外的平臺上在 .NET Core 上使用會擲回 PlatformNotSupportedException

類別是由 Unmanaged DPAPI ProtectUnprotect的兩個包裝函式所組成。 這兩種方法可用來加密和解密數據,例如密碼、金鑰和連接字串。

如果您在模擬期間使用這些方法,可能會收到下列錯誤:「金鑰不適用於指定狀態。」這是因為 DPAPI 會將金鑰資料儲存在使用者設定檔中。 如果未載入配置檔,DPAPI 將無法執行解密。 若要避免此錯誤,請在呼叫任一方法之前載入您想要模擬之使用者的配置檔。 搭配模擬使用 DPAPI 可能會產生顯著的複雜度,而且需要仔細的設計選擇。

方法

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

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

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

提供加密和解密資料的方法。 此類別無法獲得繼承。

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

提供加密和解密資料的方法。 此類別無法獲得繼承。

TryProtect(ReadOnlySpan<Byte>, DataProtectionScope, Span<Byte>, Int32, ReadOnlySpan<Byte>)

提供加密和解密資料的方法。 此類別無法獲得繼承。

TryUnprotect(ReadOnlySpan<Byte>, DataProtectionScope, Span<Byte>, Int32, ReadOnlySpan<Byte>)

提供加密和解密資料的方法。 此類別無法獲得繼承。

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

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

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

提供加密和解密資料的方法。 此類別無法獲得繼承。

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

提供加密和解密資料的方法。 此類別無法獲得繼承。

適用於

產品 版本
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10