【问题标题】:Verifying Signature - What is causing the "Signature not valid" error?验证签名 - 是什么导致“签名无效”错误?
【发布时间】:2011-12-13 13:20:04
【问题描述】:

我需要使用我公司的数字证书对字符串进行签名和验证。 VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), 签名);部分返回 false 所以我得到“签名无效”错误。我究竟做错了什么?此错误最可能的原因是什么?

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApplication1
{
    class Program
    {
        static byte[] Sign(string text, string certSubject)
        {
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            X509Certificate2 myCert = null;
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            store.Open(OpenFlags.ReadOnly);
            foreach (X509Certificate2 cert in store.Certificates.Find(X509FindType.FindBySubjectName, certSubject, false))
            {
                myCert = cert;
                break;
            }
            store.Close();
            if (myCert == null)
            {
                throw new Exception("Certificate not found: " + certSubject, null);
            }
            // Hash the data
            SHA1Managed sha1 = new SHA1Managed();
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] data = encoding.GetBytes(text);
            byte[] hash = sha1.ComputeHash(data);

            // Sign the hash
            return provider.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
        }

        static bool Verify(string text, byte[] signature, string certPath)
        {
            // Load the certificate we'll use to verify the signature from a file 
            X509Certificate2 cert = new X509Certificate2(certPath);
            // Note: 
            // If we want to use the client cert in an ASP.NET app, we may use something like this instead:
            // X509Certificate2 cert = new X509Certificate2(Request.ClientCertificate.Certificate);

            // Get its associated CSP and public key
            RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key;

            // Hash the data
            SHA1Managed sha1 = new SHA1Managed();
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] data = encoding.GetBytes(text);
            byte[] hash = sha1.ComputeHash(data);

            // Verify the signature with the hash
            return csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature);
        }

        static void Main(string[] args)
        {
            // Usage sample
            try
            {
                // Sign text
                byte[] signature = Sign("Test 123", "Ross cert");

                // Verify signature. 
                if (Verify("Test 123", signature, @"C:\...RossTest.cer"))
                {
                    Console.WriteLine("SUCCESS! Signature verified");
                }
                else
                {
                    Console.WriteLine("ERROR: Signature not valid!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
            Console.ReadKey();
        }
    }
}

【问题讨论】:

    标签: c# asp.net encryption


    【解决方案1】:

    我想问题在于,在您的签名方法中,您付出了很多努力才能获得myCert,但从不使用它。您只需使用新创建的未初始化RSACryptoServiceProvider 签名即可。

    我想在store.Close() 行之后,您需要类似以下内容:

    RSACryptoServiceProvider provider = (RSACryptoServiceProvider)cert.PrivateKey;
    

    【讨论】:

    • 谢谢克里斯。这就是问题所在。顺便说一句,我做了 RSACryptoServiceProvider provider = (RSACryptoServiceProvider)myCert.PrivateKey; (而不是 ...PrivateKey.Key)。
    • 这很酷。我只是根据您在验证方法中使用 PublicKey 的东西来猜测语法——我认为它们会返回相同的东西。我会更新我的答案以反映正确的用法。
    【解决方案2】:
    RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
    

    这是您签署数据时唯一分配provider 的位置。该代码意味着您刚刚创建了一个全新的密钥对,即它与您查找的证书无关。

    当您验证签名时,您使用的是证书的公钥(不是您生成的密钥对中的公钥)。由于它们不匹配,您将永远无法获得有效的签名验证。

    您需要使用与证书关联的私钥进行签名,而不仅仅是任何密钥对。

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2015-10-13
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多