internal override void Encode( DerOutputStream derOut) { if (!IsEmpty()) { byte[] bytes = obj.GetDerEncoded(); if (explicitly) { derOut.WriteEncoded(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo, bytes); } else { // // need to mark constructed types... (preserve Constructed tag) // int flags = (bytes[0] & Asn1Tags.Constructed) | Asn1Tags.Tagged; derOut.WriteTag(flags, tagNo); derOut.Write(bytes, 1, bytes.Length - 1); } } else { derOut.WriteEncoded(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo, new byte[0]); } }
static void Main(string[] args) { var certificateData = File.ReadAllBytes("YOUR_p7b_FILE"); var cert = new X509CertificateParser().ReadCertificate(certificateData); //I just wanted to know if I can see the publicKey somehow //var publicKey = cert.GetPublicKey(); var store = new Pkcs12Store(File.OpenRead("YOUR_p12_File"), "test".ToCharArray()); var privateKey = store.GetKey("THE_NAME_OF_KEY_YOU_WANT_TO_GET").Key; var signedDataGen = new CmsSignedDataGenerator(); signedDataGen.AddSigner(privateKey, cert, CmsSignedDataGenerator.EncryptionRsa, CmsSignedDataGenerator.DigestSha512); var zipContent = new CmsProcessableFile(new FileInfo("YOUR_DATA_FILE")); //For me a zip var signedData = signedDataGen.Generate(zipContent, true); var envDataGen = new CmsEnvelopedDataGenerator(); envDataGen.AddKeyTransRecipient(cert); var sData = new CmsProcessableByteArray(signedData.GetEncoded()); var enveloped = envDataGen.Generate(sData, CmsEnvelopedDataGenerator.DesEde3Cbc); var dos = new DerOutputStream(File.OpenWrite("YOUR_DATA_FILE.zip.encrypted.sig)")); var bytesToWrite = enveloped.GetEncoded(); dos.Write(bytesToWrite, 0, bytesToWrite.Length); dos.Flush(); dos.Close(); }
internal override void Encode(DerOutputStream derOut) { if (!IsEmpty()) { byte[] derEncoded = obj.GetDerEncoded(); if (explicitly) { derOut.WriteEncoded(160, tagNo, derEncoded); return; } int flags = (derEncoded[0] & 0x20) | 0x80; derOut.WriteTag(flags, tagNo); derOut.Write(derEncoded, 1, derEncoded.Length - 1); } else { derOut.WriteEncoded(160, tagNo, new byte[0]); } }