Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Create / Verify Detached Digital Signature for PDF using Chilkat Crypt
Creates a digital signature for a PDF file and then verifies the signature. // Create/verify a detached signature for a PDF file.
private void SignPDF_Click(object sender, System.EventArgs e)
{
Chilkat.Crypt crypt = new Chilkat.Crypt();
// Get a 30-day trial code from http://www.chilkatsoft.com/register30.asp
crypt.UnlockComponent("UnlockCode");
// Read the PDF
System.IO.FileInfo fInfo = new System.IO.FileInfo("sample.pdf");
System.IO.BinaryReader br = new System.IO.BinaryReader(
System.IO.File.OpenRead("sample.pdf"));
byte [] pdfBytes = br.ReadBytes((int)fInfo.Length);
br.Close();
// Indicate which digital certificate is to be used.
// This certificate must have the private key installed on the system.
Chilkat.Cert cert = new Chilkat.Cert();
// Replace this line with your own .cer or .p7b file.
cert.LoadFromFile("matt.cer");
crypt.SetSigningCertificate(cert);
// Create the signature.
byte [] signature = crypt.CreateSignature(pdfBytes);
// Save the signature to a file.
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(
System.IO.File.Create("signature.p7m"));
bw.Write(signature);
bw.Close();
// Now load the signature back into another byte array.
fInfo = new System.IO.FileInfo("signature.p7m");
br = new System.IO.BinaryReader(
System.IO.File.OpenRead("signature.p7m"));
byte [] sigBytes = br.ReadBytes((int)fInfo.Length);
br.Close();
// This should be valid.
bool valid1 = crypt.VerifySignature(pdfBytes,sigBytes);
// Modify the data, and verify should return false.
pdfBytes[0] = 0xCC;
bool valid2 = crypt.VerifySignature(pdfBytes,sigBytes);
MessageBox.Show("valid1 = " + valid1 + ", valid2 = " + valid2);
}
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.