Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signing and creating a asn1 signedmessage - Bouncycastle

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

like image 489
marcelo-ferraz Avatar asked Jun 13 '11 20:06

marcelo-ferraz


1 Answers

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());
like image 105
marcelo-ferraz Avatar answered Sep 19 '22 16:09

marcelo-ferraz