.net标准2.0和System.Security.Cryptography.ProtectedData.Protect

Noe*_*oel 4 .net-core .net-standard-2.0

查看System.Security.Cryptography.ProtectedData.Protect @ https://docs.microsoft.com/en-gb/dotnet/api/

因为我们希望将一个库从4.7移植到.net标准2.0库,其中.net core 2.0将使用它.所以我进行了搜索,它只能在完整的框架和.net核心中使用

我的问题是为什么在.Net Stanard 2.0中没有它?

我会教它是否可以用于例如4.7和.net core 2.0那么它将成为.Net Standard 2.0的一部分

Mar*_*ich 11

此API在.NET Standard 2.0中不可用,但它可用于".NET Standard 2.0"作为"平台扩展",这意味着您必须添加一个NuGet包才能获得对它的支持.

如果添加对System.Security.Cryptography.ProtectedDataNuGet包的引用,则可以开发使用这些API的.NET标准库.

但是,此支持仅在Windows上运行时才有效,因为这些API被描述为

提供对Windows Data Protection Api的访问.

所以它不适用于Windows以外的平台.根据您的需要,这可能会很好.

如果您希望跨平台实现类似的概念,我建议您查看ASP.NET核心数据保护API,它也可以在ASP.NET核心应用程序的上下文之外使用,因为它是由提供的NuGet包构成的加密逻辑和密钥存储解决方案(例如目录,Windows证书存储,Azure KeyVault).


cat*_*hat 6

ProtectedData 使用Windows 中的DPAPI我创建了CrossProtectedData库,它在 Windows 中使用 ProtectedData,在非 Windows 中运行时使用 AspNetCore.DataProtection。

要使用,只需添加 NuGet 包CrossProtect并将对的任何调用替换ProtectedDataCrossProtect. 例子:

using Integrative.Encryption;
using System;
using System.Security.Cryptography;
using System.Text;

namespace CrossProtectedExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // our text to protect
            var text = "Hello!";

            // get bytes from text
            var bytes = Encoding.UTF8.GetBytes(text);

            // optional entropy
            var entropy = new byte[] { 100, 25, 31, 213 };

            // protect (encrypt)
            var protectedBytes = CrossProtect.Protect(bytes, entropy,
                DataProtectionScope.CurrentUser);

            // unprotect (decrypt)
            var unprotected = CrossProtect.Unprotect(protectedBytes, entropy,
                DataProtectionScope.CurrentUser);

            // convert bytes back to text
            var result = Encoding.UTF8.GetString(unprotected);

            // print result
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)