简体   繁体   English

验证.NET中的电子签名

[英]Verify electronic signature in .NET

I have a set of questions: 我有一系列问题:

  1. What does a .p7s file contain? .p7s文件包含什么? I read that it usually is an encrypted message sent to an e-mail, but in my case I have a file and a .p7s file on my hdd. 我读到它通常是发送到电子邮件的加密消息,但就我而言,我的硬盘上有一个文件和一个.p7s文件。 The file is much larger than the .p7s, so I am wondering what is in it (and of course how to later use it). 该文件比.p7s大得多,因此我想知道其中包含什么(当然还有以后如何使用它)。

2.this question occurs mostly because I have no naswer for 1. - If I have my public key in the form of a byte array, how can I verify the signature in C#? 2.之所以会出现此问题,主要是因为我对1没有任何反应。-如果我的公用密钥为字节数组形式,如何在C#中验证签名? I found this on the internet, but again, it uses some text gotten from an e-mail and I honestly don't know what is happening: 我在互联网上找到了它,但是同样,它使用了从电子邮件中获得的一些文本,老实说,我不知道发生了什么:

public static bool Verify(byte[] signature, string text)
        {
            X509Certificate2 certificate = new X509Certificate2(@"D:\My-CV.docx.p7s");

            if (signature == null)
                throw new ArgumentNullException("signature");
            if (certificate == null)
                throw new ArgumentNullException("certificate");

            //hash the text 
            // Methode 3 for Hashing
            System.Security.Cryptography.SHA1 hash3 = System.Security.Cryptography.SHA1.Create();
            System.Text.UnicodeEncoding encoder = new System.Text.UnicodeEncoding();
            byte[] combined = encoder.GetBytes(text);
            byte[] hash3byte = hash3.ComputeHash(combined);

            //Adding the text from the email, to a contentInfo 
            ContentInfo content = new ContentInfo(hash3byte);

            // decode the signature
            SignedCms verifyCms = new SignedCms(content, true);
            verifyCms.Decode(signature);

            // verify it
            try
            {
                verifyCms.CheckSignature(new X509Certificate2Collection(certificate), false);
                return true;
            }
            catch (CryptographicException)
            {
                return false;
            }
        } 

Can someone help me with this? 有人可以帮我弄这个吗?

The .p7s file is containing the signature of your document. .p7s文件包含文档的签名。 The content is not encrypted, but it's look like. 内容未加密,但是看起来像。

The document file is much larger, because it contains the date, the .p7s the signature. 文档文件要大得多,因为它包含日期,.p7s签名。

To check the signature, you need the public part of the certificate with which the document was signed. 要检查签名,您需要用于签署文档的证书的公共部分。

You can check this two posts to check the signature : 您可以检查这两个帖子以检查签名:

The last thing is to load the signature data, from the p7s file. 最后一件事是从p7s文件加载签名数据。 I make some search, and come back to you. 我进行了搜索,然后再找您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM