【问题标题】:cryptographicexception the parameter is incorrectcryptographicexception 参数不正确
【发布时间】:2016-03-31 20:10:14
【问题描述】:

我正在使用带有 SHA1 加密的 ECDSA,因为我正在尝试为桌面应用程序进行许可激活。为此,我使用 PHP 服务器向其提供 PC 信息,服务器向我提供公钥,然后我想验证 C# 中的数据。

我在 PHP 中生成了这个公钥:

"-----BEGIN PUBLIC KEY-----
MDIwEAYHKoZIzj0CAQYFK4EEAAYDHgAEKzL3PFVVo3IWftdEYmwiSO/4zULGM/wB
8BrLjQ==
-----END PUBLIC KEY-----";

我使用这里的代码 http://securitydriven.net/inferno/ 来达到这个目的

byte[] thePublicKeyToBytes = GetBytesFromPEM(thePublicKey2, "PUBLIC KEY");
CngKey dsaKeyPublic2 = thePublicKeyToBytes.ToPublicKeyFromBlob();

byte[] theRestToBytes = GetBytes(theRestInBinary);
byte[] meinData = GetBytes("Blabla");

using (var ecdsa = new ECDsaCng(dsaKeyPublic2) { HashAlgorithm = CngAlgorithm.Sha1 }) // verify DSA signature with public key
{
    if (ecdsa.VerifyData(meinData, theRestToBytes)) MessageBox.Show("Signature verified.");
    else MessageBox.Show("Signature verification failed.");
}

程序在哪里:

byte[] GetBytesFromPEM(string pemString, string section)
{
    var header = String.Format("-----BEGIN {0}-----", section);
    var footer = String.Format("-----END {0}-----", section);

    var start = pemString.IndexOf(header, StringComparison.Ordinal) + header.Length;
    var end = pemString.IndexOf(footer, start, StringComparison.Ordinal) - start;

    if (start < 0 || end < 0)
    {
        return null;
    }

    return Convert.FromBase64String(pemString.Substring(start, end));
}

问题是我在这一行得到了这个异常“cryptographicexception the parameter is wrong”:

CngKey dsaKeyPublic2 = thePublicKeyToBytes.ToPublicKeyFromBlob();

我无法显示 inferno 的公钥,但我看到他们的密钥长度是 384。这是我做错的地方吗?生成的公钥长度?

【问题讨论】:

    标签: c# php ecdsa


    【解决方案1】:

    您的公钥有 52 个字节长 - 太短了。你是如何生成它的?
    ToPublicKeyFromBlob() 方法是return CngKey.Import(byteArray, CngKeyBlobFormat.EccPublicBlob) 的快捷方式 - 它仅适用于基于 Ecc 的密钥以及由 .NET 生成的密钥。 Inferno 在 P384 曲线上使用 ECC 密钥,这意味着每个公钥将有 48*2=96 个字节,加上 8 个头字节(如 here 所述),总共 104 个字节。

    【讨论】:

    • 密钥是由带有 secp112r1 限制的 PHP 算法生成的。 Inferno 是否支持这种类型的键?
    【解决方案2】:

    Andrei,Inferno 仅使用 NIST P-384 曲线。更重要的是,.NET 框架(开箱即用)支持的唯一曲线是 P-256、P-384 和 P-521。

    【讨论】:

      猜你喜欢
      • 2011-06-23
      • 2019-05-23
      • 2015-05-27
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 2020-03-13
      相关资源
      最近更新 更多