【问题标题】:Add TimeStamp to digital signature将时间戳添加到数字签名
【发布时间】:2017-12-21 09:49:11
【问题描述】:

我有一个代码可以在 C# 中成功使用 A1 证书对字符串进行签名。 我需要在此签名中添加时间戳。这个时间戳需要来自像http://www.cryptopro.ru/tsp/tsp.srf这样的时间戳服务器 我不知道如何将时间戳放到签名中。 它不是 PDF 符号,而是字符串符号。 谁能帮帮我?

用于签名的代码:

private byte[] Sign(string text)
    {
        // Find the certificate we’ll use to sign
        X509Certificate2 cert = new X509Certificate2(@"C:\Users\1000084016.pfx", "Password");
        RSACryptoServiceProvider csp = null;

        // Get its associated CSP and private key
        csp = (RSACryptoServiceProvider)cert.PrivateKey;

        // Hash the data
        SHA1Managed sha1 = new SHA1Managed();
        UnicodeEncoding encoding = new UnicodeEncoding();

        byte[] data;
        data = encoding.GetBytes(text);

        byte[] hash = sha1.ComputeHash(data);

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

【问题讨论】:

  • 这取决于您的数字签名的外观。如果是 XML 文档,签名哈希、时间戳和混合哈希应该有单独的字段

标签: c# timestamp digital-signature x509certificate rsacryptoserviceprovider


【解决方案1】:

时间戳由外部时间戳提供程序使用 RFC3161 在包含哈希的 application/timestamp-query 上发出。您可以使用 BouncyCastle 构建请求(参见示例 here

要将时间戳结果嵌入到签名中,您不能使用基本的 RSA 结果。您需要使用高级签名容器,例如 CMS(二进制)或 XMLDsig(XML),并将时间戳添加为未签名属性。请参阅此处使用 CMS 的 implementation

【讨论】:

  • 我更改了代码以使用 CMS。 CmsSigner 签名者 = 新 CmsSigner(certificateFromFile); Signer.UnsignedAttributes.Add(new Pkcs9SigningTime());但我无法读取 Pkcs9SigningTime 中的时间戳提供程序。你知道不用充气城堡怎么办吗?
  • Pkcs9SigningTime 将包含本地时间。您需要使用在线 http 请求向 TSP 请求 RFC3161 时间戳(我在答案中发布了客户端的链接),并使用 TSTInfo 结构将结果包含到 CMS 签名中(请参阅最后一个链接)。恐怕一点都不简单。
猜你喜欢
  • 2020-05-06
  • 2017-12-01
  • 2020-11-11
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2010-10-26
相关资源
最近更新 更多