.NET Core 2.0 Crypto 示例不工作 - 错误 Cng v4.3 使用高于引用算法 v4.2 的算法 v4.3

.NET Core 2.0 Crypto sample not working - Error Cng v4.3 uses Algorithms v4.3 higher than referenced Algorithms v4.2

所以我正在尝试从 .NET Core Api 文档 ECDsaCng Class 获取一个编译示例。我收到有关是否缺少程序集的编译错误。我下载了 .NET Core 2.0,然后添加了对新安装的 C:\Program Files\dotnet\shared\Microsoft.NETCore.App.0.0\System.Security.Cryptography.Cng.dll 的引用,一些术语从黑色变为浅蓝色以显示令牌识别,但我遇到了编译错误,我猜这是一个版本控制问题。

我安装了 .NET Core 2.0 后又从头开始,以防有人疑惑。所以这是一个新项目向导新创建的项目,全新安装。所以我现在被困住了(我是那种会关闭所有 windows 然后再打开它们的人)。

代码如下。典型的错误消息在下面以块引用的形式给出。

// https://docs.microsoft.com/en-gb/dotnet/api/system.security.cryptography.ecdsacng?view=netcore-2.0
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

//Added Dependencies/Reference to C:\Program Files\dotnet\shared\Microsoft.NETCore.App.0.0\System.Security.Cryptography.Cng.dll;

Namespace DotNetCoreEllipticCurveDigitalSignerConsl
{
    class Alice
    {
        public static void Main(string[] args)
        {
            Bob bob = new Bob();
            using (ECDsaCng dsa = new ECDsaCng())
            {
                dsa.HashAlgorithm = CngAlgorithm.Sha256;
                bob.key = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob);

                byte[] data = new byte[] { 21, 5, 8, 12, 207 };

                byte[] signature = dsa.SignData(data);

                bob.Receive(data, signature);
            }
        }


    }
    public class Bob
    {
        public byte[] key;

        public void Receive(byte[] data, byte[] signature)
        {
            using (ECDsaCng ecsdKey = new ECDsaCng(CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob)))
            {
                if (ecsdKey.VerifyData(data, signature))
                    Console.WriteLine("Data is good");
                Else
                    Console.WriteLine("Data is bad");
            }
        }
    }
}

Severity Code Description Project File Line Suppression State Error CS1705 Assembly 'System.Security.Cryptography.Cng' with identity 'System.Security.Cryptography.Cng, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' uses 'System.Security.Cryptography.Algorithms, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Security.Cryptography.Algorithms' with identity 'System.Security.Cryptography.Algorithms, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' DotNetCoreEllipticCurveDigitalSignerConsl c:\users\simon\documents\visual studio 2017\Projects\DotNetCoreEllipticCurveDigitalSignerConsl\DotNetCoreEllipticCurveDigitalSignerConsl\CSC 1 Active

编辑:根据评论中的要求添加 csproj 文件

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="System.Security.Cryptography.Cng">
      <HintPath>..\..\..\..\..\..\..\Program Files\dotnet\shared\Microsoft.NETCore.App.0.0\System.Security.Cryptography.Cng.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

编辑:我已按照此博客将 csproj 文件手动编辑为 TargetFramework netcoreapp2.0 Rick Strahl (MVP) Upgrading to .NET Core 2.0 Preview 重新运行项目后,包管理器的输出 window 输出出现延迟 window =13=] 并且状态栏显示 Installing Microswoft.NET.Core.App 2.0.0. 然后编译错误红色波浪线消失了。

现在可以编译了,好消息。坏消息是现在出现运行时错误

您不应在 SDK 中添加对文件的直接引用。您需要做的是添加对包的引用 System.Security.Cryptography.Cng,然后它会检测到您正在使用本地的包并使用它而不是下载新副本。

我在 Windows 10 机器上创建了一个新的 .NET Core 2 控制台应用程序,引用 System.Security.Cryptography.Cng 作为 NuGet 包并复制了您的代码。

我得到了 NullReferenceException。问题是,ECDSA 算法似乎不知道哈希算法,因为它不是从 public 密钥中获取的...手动设置它对我有效:

class Alice
{
    public static void Main(string[] args)
    {
        Bob bob = new Bob();
        using (ECDsaCng dsa = new ECDsaCng())
        {
            dsa.HashAlgorithm = CngAlgorithm.Sha256;
            bob.key = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob);

            byte[] data = new byte[] { 21, 5, 8, 12, 207 };

            byte[] signature = dsa.SignData(data);

            bob.Receive(data, signature);
        }
    }


}
public class Bob
{
    public byte[] key;

    public void Receive(byte[] data, byte[] signature)
    {
        using (ECDsaCng ecsdKey = new ECDsaCng(CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob)))
        {
            // set hash algorithm manually here
            ecsdKey.HashAlgorithm = CngAlgorithm.Sha256;
            if (ecsdKey.VerifyData(data, signature))
                Console.WriteLine("Data is good");
            else
                Console.WriteLine("Data is bad");
        }
    }
}