コード例 #1
0
        public byte[] SignHash(string hexhash, string password)
        {
            byte[]      hash  = StringToByteArray(hexhash);
            Pkcs12Store store = new Pkcs12Store(getCertificate(), password.ToCharArray());
            String      alias = "";

            foreach (string al in store.Aliases)
            {
                if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
                {
                    alias = al;
                    break;
                }
            }
            AsymmetricKeyEntry pk = store.GetKey(alias);

            X509CertificateEntry[] chain = store.GetCertificateChain(alias);
            List <Org.BouncyCastle.X509.X509Certificate> c = new List <Org.BouncyCastle.X509.X509Certificate>();

            foreach (X509CertificateEntry en in chain)
            {
                c.Add(en.Certificate);
            }
            PrivateKeySignature signature = new PrivateKeySignature(pk.Key, "SHA256");
            String   hashAlgorithm        = signature.GetHashAlgorithm();
            PdfPKCS7 sgn         = new PdfPKCS7(null, c, hashAlgorithm, false);
            DateTime signingTime = DateTime.Now;

            byte[] sh           = sgn.getAuthenticatedAttributeBytes(hash, null, null, CryptoStandard.CMS);
            byte[] extSignature = signature.Sign(sh);
            sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());
            return(sgn.GetEncodedPKCS7(hash, null, null, null, CryptoStandard.CMS));
        }
コード例 #2
0
            public byte[] Sign(Stream data)
            {
                PrivateKeySignature signature = new PrivateKeySignature(pk.Key, "SHA256");
                String   hashAlgorithm        = signature.GetHashAlgorithm();
                PdfPKCS7 sgn = new PdfPKCS7(null, chain, hashAlgorithm, false);

                byte[]   hash        = DigestAlgorithms.Digest(data, hashAlgorithm);
                DateTime signingTime = DateTime.Now;

                byte[] sh           = sgn.getAuthenticatedAttributeBytes(hash, signingTime, null, null, CryptoStandard.CMS);
                byte[] extSignature = signature.Sign(sh);
                sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());
                return(sgn.GetEncodedPKCS7(hash, signingTime, null, null, null, CryptoStandard.CMS));
            }
コード例 #3
0
            public byte[] Sign(Stream inputStream)
            {
                try
                {
                    PrivateKeySignature signature = new PrivateKeySignature(pk, "SHA256");
                    String hashAlgorithm          = signature.GetHashAlgorithm();

                    PdfPKCS7 sgn  = new PdfPKCS7(null, chain, hashAlgorithm, false);
                    byte[]   hash = DigestAlgorithms.Digest(inputStream, hashAlgorithm);
                    byte[]   sh   = sgn.GetAuthenticatedAttributeBytes(hash, PdfSigner.CryptoStandard.CMS,
                                                                       null, null);
                    byte[] extSignature = signature.Sign(sh);
                    sgn.SetExternalDigest(extSignature, null, signature.GetEncryptionAlgorithm());

                    return(sgn.GetEncodedPKCS7(hash, PdfSigner.CryptoStandard.CMS, null,
                                               null, null));
                }
                catch (IOException ioe)
                {
                    throw new Exception(ioe.Message);
                }
            }