【发布时间】: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。这是我做错的地方吗?生成的公钥长度?
【问题讨论】: