Foxit PDF SDK for Windows

How to Encrypt PDF files using Foxit PDF SDK (.NET)

Foxit PDF SDK provides a range of encryption and decryption functions to meet different levels of document security protection. Users can use regular password encryption and certificate-driven encryption, or use their own security handler for custom security implementation. It also provides APIs to integrate with the third-party security mechanism (Microsoft RMS). These APIs allow developers to work with the Microsoft RMS SDK to both encrypt (protect) and decrypt (unprotect) PDF documents.

Note: For more detailed information about the RMS encryption and decryption, please refer to the simple demo “security” in the “examples\simple_demo” folder of the download package.

Example:

How to encrypt a PDF file with user password “123” and owner password “456”

using foxit.common;
using foxit.pdf;
...
using (var handler = new StdSecurityHandler())
{
 using (var encrypt_data = new StdEncryptData(true, -4, SecurityHandler.CipherType.e_CipherAES, 16))
 {
 handler.Initialize(encrypt_data, “123”, “456”);
 doc.SetSecurityHandler(handler);
 doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNormal);
 }
}
...

How to encrypt a PDF file with a Certificate

using foxit.common;
using foxit.pdf;
...
PDFDoc doc = new PDFDoc(input_file);
ErrorCode error_code = doc.Load(null);
if (error_code != ErrorCode.e_ErrSuccess){
 return;
}
// Do encryption.
string cert_file_path = input_path + "foxit.cer";
var cms = new EnvelopedCms(new ContentInfo(seed));
cms.ContentEncryptionAlgorithm.Oid.Value = "1.2.840.113549.3.4";
cms.Encrypt(new CmsRecipient(new X509Certificate2(cert_file_path)));
byte[] bytes = cms.Encode();
 byte[] data = new byte[20 + bytes.Length];
Array.Copy(seed, 0, data, 0, 20);
Array.Copy(bytes, 0, data, 20, bytes.Length);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] initial_key = new byte[16];
Array.Copy(sha1.ComputeHash(data), initial_key, initial_key.Length);
byte[][] bytes_array = new byte[1][];
bytes_array[0] = bytes;
var handler = new CertificateSecurityHandler()
var encrypt_data = new CertificateEncryptData()
encrypt_data.Set(true, SecurityHandler.CipherType.e_CipherAES, bytes_array);
handler.Initialize(encrypt_data, initial_key);
doc.SetSecurityHandler(handler);
doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNormal);
...

How to encrypt a PDF file with Foxit DRM

using foxit.common;
using foxit.pdf;
...
PDFDoc doc = new PDFDoc(input_file);
ErrorCode error_code = doc.Load(null);
if (error_code != ErrorCode.e_ErrSuccess){
 return;
}
// Do encryption.
var handler = new DRMSecurityHandler();
var encrypt_data = new DRMEncryptData(true,"Simple-DRM-filter", SecurityHandler.CipherType.e_CipherAES, 16, true, -4);
string file_id = "Simple-DRM-file-ID";
string initialize_key = "Simple-DRM-initialize-key";
handler.Initialize(encrypt_data, file_id, initialize_key);
doc.SetSecurityHandler(handler);
doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNormal);

Updated on May 8, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: