Question

Is there any good tutorials on how to sign a file, and wrap it inside a asn1 pkcs7 package using bouncycastle?

Was it helpful?

Solution

After sometime, i've found how its done, in the examples inside the bouncyCastle app.

It's actually quite simpler and straight foward than i had found in IText (, not taking out the geniality of the framework itself).

The code is something like:

        AsymmetricCipherKeyPair signaturePair;
        X509Certificate signatureCert;

        IList certList = new ArrayList();
        IList crlList = new ArrayList();
        CmsProcessable msg = new CmsProcessableByteArray(Encoding.ASCII.GetBytes("I hate hello world!"));

        certList.Add(signatureCert);
        certList.Add(OrigCert);

        crlList.Add(SignCrl);

        IX509Store x509Certs = X509StoreFactory.Create(
            "Certificate/Collection",
            new X509CollectionStoreParameters(certList));
        IX509Store x509Crls = X509StoreFactory.Create(
            "CRL/Collection",
            new X509CollectionStoreParameters(crlList));

        CmsSignedDataGenerator gen = new CmsSignedDataGenerator();

        gen.AddSigner(signaturePair.Private, signatureCert, CmsSignedDataGenerator.DigestSha1);

        gen.AddCertificates(x509Certs);
        gen.AddCrls(x509Crls);

        CmsSignedData signedData = gen.Generate(msg, true);

        //saving in BER encoding
        Stream stream = new MemoryStream(signedData.GetEncoded());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top