【发布时间】:2014-08-27 03:53:14
【问题描述】:
我从 MSDN 复制了以下代码。我必须用公钥签署一个哈希。当我添加
RSA.FromXmlString(PublicKey);
跟随代码它显示异常说
Object contains only the public half of a key pair. A private key must also be provided.
我做错了什么?这是使用给定公钥签署哈希的正确方法吗?
我在 Windows 7 Pro 64 bis OS 上使用 Microsoft Visual C# Express 2010。
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(PublicKey);
//The hash to sign.
byte[] Hash = { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 };
//Create an RSAOPKCS1SignatureFormatter object and pass it the
//RSACryptoServiceProvider to transfer the key information.
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter((AsymmetricAlgorithm)RSA);
//Set the hash algorithm to SHA1.
RSAFormatter.SetHashAlgorithm("SHA1");
//Create a signature for HashValue and return it.
byte[] SignedHash = RSAFormatter.CreateSignature(Hash);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
编辑:
RSA.FromXmlString(PublicKey);
做吗?签署此哈希时是否需要它?
【问题讨论】:
-
你不能用公钥签署任何东西,这与签署的想法相矛盾。
-
“我从 MSDN 复制了以下代码”:请提供链接,因为 Eugene 是对的。我想知道你怎么能在 MSDN 中找到这样的示例。
标签: c# hash cryptography sign