【问题标题】:Sign file with SHA-256 Algorithm使用 SHA-256 算法签署文件
【发布时间】:2016-03-11 16:48:43
【问题描述】:

我有一个用 X509Certificate2 证书签署 XML 文件的函数。我的问题是签名被插入为 SHA-1 和 SHA-256。我的代码做错了什么?

CmsSigner cms = new CmsSigner(oCert);
//if apply this row receive unknow error!
//cms.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"); 

ContentInfo ci = new ContentInfo(File.ReadAllBytes(cFileXml));
SignedCms cmssigned = new SignedCms(ci);
cmssigned.ComputeSignature(cms);
byte[] encodedmessage = cmssigned.Encode();

File.WriteAllBytes(cFile + ".p7m", encodedmessage);

【问题讨论】:

  • this 有帮助吗?
  • 所以我必须用充气城堡来做这个?谢谢
  • 什么是“充气城堡”,这与您的问题有什么关系?
  • 把你建议的帖子读到最后,你会发现:错误是一样的。微软在 SHA256 的库和操作系统上搞砸了。我用 Bouncy castle 解决了所有问题。谢谢。

标签: c# sha256


【解决方案1】:

我对自己说。这是简单的解决方案:

                string PIN = Leo.LeggiFirmacert();
            System.Security.SecureString SecurePIN = new System.Security.SecureString();
            foreach (char ch in PIN)
            { SecurePIN.AppendChar(ch); }
            var rsa = (RSACryptoServiceProvider)cert.PrivateKey;
            string ContinerName = rsa.CspKeyContainerInfo.KeyContainerName;
            string CspName = rsa.CspKeyContainerInfo.ProviderName;
            int CspType = rsa.CspKeyContainerInfo.ProviderType;
            CspParameters csp = new CspParameters(CspType, CspName, ContinerName, new System.Security.AccessControl.CryptoKeySecurity(), SecurePIN);
            SHA256Managed hashSha256 = new SHA256Managed();
            byte[] certHash = hashSha256.ComputeHash(cert.RawData);
            EssCertIDv2 essCert1 = new EssCertIDv2(new Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier("2.16.840.1.101.3.4.2.1"), certHash);
            SigningCertificateV2 scv2 = new SigningCertificateV2(new EssCertIDv2[] { essCert1 });

            Org.BouncyCastle.Asn1.Cms.Attribute CertHAttribute = new Org.BouncyCastle.Asn1.Cms.Attribute(Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAASigningCertificateV2, new DerSet(scv2));
            Asn1EncodableVector v = new Asn1EncodableVector();
            v.Add(CertHAttribute);
            Org.BouncyCastle.Asn1.Cms.AttributeTable AT = new Org.BouncyCastle.Asn1.Cms.AttributeTable(v);
            CmsSignedDataGenWithRsaCsp cms = new CmsSignedDataGenWithRsaCsp();
            Org.BouncyCastle.Crypto.AsymmetricKeyParameter keyParameter = null;
            Org.BouncyCastle.X509.X509Certificate certCopy = DotNetUtilities.FromX509Certificate(cert);
            cms.MyAddSigner(rsa, certCopy, keyParameter, "1.2.840.113549.1.1.1", "2.16.840.1.101.3.4.2.1", AT, null);
            ArrayList certList = new ArrayList();
            certList.Add(certCopy);
            Org.BouncyCastle.X509.Store.X509CollectionStoreParameters PP = new Org.BouncyCastle.X509.Store.X509CollectionStoreParameters(certList);

            Org.BouncyCastle.X509.Store.IX509Store st1 = Org.BouncyCastle.X509.Store.X509StoreFactory.Create("CERTIFICATE/COLLECTION", PP);

            cms.AddCertificates(st1);

            FileInfo File__1 = new FileInfo(NomeFile);
            CmsProcessableFile file__2 = new CmsProcessableFile(File__1);
            CmsSignedData Firmato = cms.Generate(file__2, true);
            byte[] Encoded = Firmato.GetEncoded();
            RisFirma = "";

            return Encoded;

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2021-10-29
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多