对哈希签名有问题

本文关键字:有问题 哈希签 | 更新日期: 2023-09-27 18:19:28

我从MSDN复制了以下代码。我必须用公钥在散列上签名。当我添加时

RSA.FromXmlString(PublicKey);

为了遵循nig代码,它显示异常,称

Object contains only the public half of a key pair. A private key must also be provided.

我做错了什么?这是用给定的公钥对哈希进行签名的正确方式吗?

我使用的是Windows 7 Pro 64 bis操作系统上的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);

做什么?我在这个散列签名时需要它吗?

对哈希签名有问题

问题在于使用公钥进行签名。你可以用私钥对哈希进行签名。公钥正在用于验证。

填写RSAP参数RSAKeyInfo

你需要RSAP参数看看这个。如何在c#中填写RSAP参数值

RSA.FromXmlString(RSAKeyInfo);