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

S M*_*den 4 c# public-key-encryption .net-core

所以我试图从.NET Core Api Docs ECDsaCng类获得一个示例编译。我遇到有关您是否缺少程序集的编译错误。我下载了.NET Core 2.0,然后添加了对新安装的引用,C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0\System.Security.Cryptography.Cng.dll并且一些术语从黑色变为浅蓝色,显示令牌已重新注册,但是出现编译错误,我猜这是版本问题。

安装.NET Core 2.0之后,如果有人怀疑,我又从头开始。因此,这是“新建项目向导”通过全新安装新创建的项目。所以我现在被困住了(我是那种会关闭所有窗户然后再次打开它们的人)。

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

// 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\2.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");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

严重性代码说明项目文件行抑制状态错误CS1705身份为“ System.Security.Cryptography.Cng”的程序集“ System.Security.Cryptography.Cng”,版本= 4.3.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a使用“ System.Security” .Cryptography.Algorithms,版本= 4.3.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a,其版本高于引用程序集“ System.Security.Cryptography.Algorithms”,标识为“ System.Security.Cryptography.Algorithms”,版本= 4.2 0.0。

编辑:根据注释中的请求添加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\2.0.0\System.Security.Cryptography.Cng.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

编辑:我已经按照此博客Rick Strahl(MVP)手动将csproj文件编辑为TargetFramework netcoreapp2.0 升级到.NET Core 2.0预览 重新启动项目后,“包管理器”的“输出”窗口中输出了一个延迟,Restoring NuGet packages...并且状态吧说,Installing Microswoft.NET.Core.App 2.0.0. 然后编译错误红色的花花公子消失了。

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

在此处输入图片说明

Sco*_*ain 5

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

  • 实际上,即使在修复了程序包引用之后,在我的Mac上,我仍然得到“未处理的异常:System.PlatformNotSupportedException:该平台不支持Windows加密下一代(CNG)”。在/Users/viktorendokimov/experiments/netcore/hwapp/Program.cs:第11行的hwapp.Main(String [] args)的System.Security.Cryptography.ECDsaCng..ctor()处 (6认同)

Com*_*Guy 5

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

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

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您让这个工作。[向 Scott 致敬] 我会将修正案转发给 Microsoft,这是他们文档站点上的代码。 (2认同)