Firma e creazione di un ASN1 SignedMessage - BounCycastle
-
27-10-2019 - |
Domanda
Ci sono buoni tutorial su come firmare un file e avvolgerlo all'interno di un pacchetto ASN1 PKCS7 usando BounCycastle?
Soluzione
Dopo qualche tempo, ho scoperto come è stato fatto, negli esempi all'interno dell'app BounCycastle.
In realtà è abbastanza semplice e dritto di quanto non avessi trovato in Itext (non eliminare la genialità del framework stesso).
Il codice è qualcosa di simile:
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());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow