//Load PFX in X509Certificate2 as exportable
FileStream pfxStream = File.OpenRead("PDF.pfx");
X509Certificate2 x509Certificate = new X509Certificate2(pfxStream, "syncfusion", X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) x509Certificate.PrivateKey; |
static void Main(string[] args)
{
//Load existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument(@"Sample.pdf");
//Initialize the Windows store.
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
//Find the certificate using thumb print.
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, "0F59645E853309589EAE9965D2E603D70B46D707", true);
X509Certificate2 digitalID = fcollection[0];
//Load X509Certificate2.
PdfCertificate certificate = new PdfCertificate(digitalID);
//Create a revision 2 signature with loaded digital ID.
PdfSignature signature = new PdfSignature(document, document.Pages[0], certificate, "DigitalSignature");
//Changing the digital signature standard and hashing algorithm.
signature.Settings.CryptographicStandard = CryptographicStandard.CADES;
if (IsExportable(digitalID))
{
signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA512;
}
else
{
signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA1;
}
//Save the PDF document.
document.Save("WindowsStore.pdf");
//Close the document.
document.Close(true);
}
private static bool IsExportable(X509Certificate2 certificate)
{
try
{
return (certificate.PrivateKey as RSACryptoServiceProvider).CspKeyContainerInfo.Exportable;
}
catch
{
return false;
}
} |
static void Main(string[] args)
{
//Load existing PDF document.
PdfDocument document = new PdfDocument();
PdfPage page = document.Pages.Add();
PdfSignature signature = new PdfSignature(document, page, null, "DigitalSignature");
//Set the signature bounds.
signature.Bounds = new RectangleF(0, 0, 200, 100);
//Call the compute hash event.
signature.ComputeHash += Signature_ComputeHash;
//Save the PDF document.
document.Save("WindowsStore.pdf");
//Close the document.
document.Close(true);
System.Diagnostics.Process.Start("WindowsStore.pdf");
}
private static void Signature_ComputeHash(object sender, PdfSignatureEventArgs ars)
{
//Get the document bytes.
byte[] documentBytes = ars.Data;
//Initialize the Windows store.
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
//Find the certificate using thumb print.
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, "0F59645E853309589EAE9965D2E603D70B46D707", true);
X509Certificate2 certificate = fcollection[0];
//Include the signed data to PDF.
ars.SignedData = Sign(documentBytes, certificate);
}
public static byte[] Sign(byte[] data, X509Certificate2 certificate)
{
if (data == null)
throw new ArgumentNullException("data");
if (certificate == null)
throw new ArgumentNullException("certificate");
// setup the data to sign
ContentInfo content = new ContentInfo(data);
SignedCms signedCms = new SignedCms(content, true);
CmsSigner signer = new CmsSigner(certificate);
signer.DigestAlgorithm = new Oid("SHA256");//you can use SHA1,SHA256,SHA512
signedCms.ComputeSignature(signer);
return signedCms.Encode();
} |