Skip to content

Instantly share code, notes, and snippets.

@soaresdiogo
Created January 29, 2019 19:02
Show Gist options
  • Save soaresdiogo/5ff743c69207fdba42e43bc603a3c998 to your computer and use it in GitHub Desktop.
Save soaresdiogo/5ff743c69207fdba42e43bc603a3c998 to your computer and use it in GitHub Desktop.
Sign File and Generate the PKCS7 "RSA"
string mapPath = HttpContext.Current.Server.MapPath("/");
string pfx = @mapPath + "/keystore.p12";
X509Certificate2 cert = new X509Certificate2(pfx, "password");
string xml = @mapPath + "/file.SF";
byte[] fileContent = System.IO.File.ReadAllBytes(xml);
ContentInfo contentInfo = new ContentInfo(fileContent);
SignedCms cms = new SignedCms(contentInfo, true);
CmsSigner signer = new CmsSigner(cert);
cms.ComputeSignature(signer);
byte[] pkcs7 = cms.Encode();
File.WriteAllBytes(@mapPath + "/file.RSA", pkcs7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment