Войдите pdf с BouncyCastle в ПАДЫ - PullRequest
0 голосов
/ 10 апреля 2020

У меня есть этот код, написанный другом, который ушел, который правильно подписывает PDF-файл в формате P7M:

        string cFile = "test.pdf";
        string cPincode = "123456";
        X509Certificate2 oCertificato = myCertificate (my X509 certificate);

            System.Security.SecureString SecurePIN = new System.Security.SecureString();
            foreach (char ch in cPincode)
            { SecurePIN.AppendChar(ch); }
            var rsa = (RSACryptoServiceProvider)oCertificato.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);
            RSACryptoServiceProvider CSP = new RSACryptoServiceProvider(csp);
            SHA256Managed hashSha256 = new SHA256Managed();
            byte[] certHash = hashSha256.ComputeHash(oCertificato.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.X509.X509Certificate certCopy = DotNetUtilities.FromX509Certificate(oCertificato);
            cms.MyAddSigner(rsa, certCopy, "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 oFileinfo = new FileInfo(cFile);
            CmsProcessableFile file = new CmsProcessableFile(oFileinfo);
            CmsSignedData Signed = cms.Generate(file, true);
            byte[] Encoded = Signed.GetEncoded();
            File.WriteAllBytes(cFile + ".p7m", Encoded);

Все отлично работает, но теперь я хотел бы подписать не в P7M, а в ПАДЫ. Как мне изменить код?

...