using ComPDFKit.PDFDocument; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EncryptTest { internal class EncryptTest { static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Encrypt"; static private string userPassword = string.Empty; static private string ownerPassword = string.Empty; static void Main(string[] args) { Console.WriteLine("Running Encrypt test sample…\r\n"); SDKLicenseHelper.LicenseVerify(); CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } if (EncryptByUserPassword(document)) { Console.WriteLine("Encrypt by user password done."); } else { Console.WriteLine("Encrypt by user password failed."); } document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); Console.WriteLine("--------------------"); if (EncryptByOwnerPassword(document)) { Console.WriteLine("Encrypt by owner password done."); } else { Console.WriteLine("Encrypt by owner password failed."); } Console.WriteLine("--------------------"); document.Release(); document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf"); if (EncryptByAllPasswords(document)) { Console.WriteLine("Encrypt by Both user and owner passwords done."); } else { Console.WriteLine("Encrypt by Both user and owner passwords failed."); } Console.WriteLine("--------------------"); document.Release(); document = CPDFDocument.InitWithFilePath("AllPasswords.pdf"); if (Unlock(document)) { Console.WriteLine("Unlock done."); } else { Console.WriteLine("Unlock failed."); } Console.WriteLine("--------------------"); document.Release(); document = CPDFDocument.InitWithFilePath("AllPasswords.pdf"); if (Decrypt(document)) { Console.WriteLine("Decrypt done."); } else { Console.WriteLine("Decrypt failed."); } Console.WriteLine("--------------------"); Console.WriteLine("Done!"); Console.WriteLine("--------------------"); Console.ReadLine(); } static private bool EncryptUseRC4Algo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo) { CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4; document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel); string encryptPath = outputPath + "\\EncryptUseRC4Test.pdf"; if (!document.WriteToFilePath(encryptPath)) { return false; } CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath); if (encryptedDoc.IsEncrypted) { Console.WriteLine("File is encrypted"); Console.WriteLine("Browse the changed file in: " + encryptPath); Console.WriteLine("User password is: {0}", userPassword); } else { Console.WriteLine("File encrypt failed"); return false; } return true; } static private bool EncryptUseAES128Algo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo) { CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelAES128; document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel); string encryptPath = outputPath + "\\EncryptUseAES128Test.pdf"; if (!document.WriteToFilePath(encryptPath)) { return false; } CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath); if (encryptedDoc.IsEncrypted) { Console.WriteLine("File is encrypted"); Console.WriteLine("Browse the changed file in: " + encryptPath); Console.WriteLine("User password is: {0}", userPassword); } else { Console.WriteLine("File encrypt failed"); return false; } return true; } static private bool EncryptUseAES256Algo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo) { CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelAES256; document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel); string encryptPath = outputPath + "\\EncryptUseAES256Test.pdf"; if (!document.WriteToFilePath(encryptPath)) { return false; } CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath); if (encryptedDoc.IsEncrypted) { Console.WriteLine("File is encrypted"); Console.WriteLine("Browse the changed file in " + encryptPath); Console.WriteLine("User password is: {0}", userPassword); } else { Console.WriteLine("File encrypt failed"); return false; } return true; } static private bool EncryptUseNoEncryptAlgo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo) { CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelNoEncryptAlgo; document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel); string encryptPath = outputPath + "\\EncryptUseNoEncryptAlgoTest.pdf"; if (!document.WriteToFilePath(encryptPath)) { return false; } CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath); if (encryptedDoc.IsEncrypted) { Console.WriteLine("File is encrypted."); Console.WriteLine("Browse the changed file in " + encryptPath); Console.WriteLine("User password is: {0}", userPassword); } else { Console.WriteLine("File encrypt failed"); return false; } return true; } static private bool EncryptByUserPassword(CPDFDocument document) { bool result = true; userPassword = "User"; ownerPassword = string.Empty; CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo(); if (EncryptUseRC4Algo(document, permissionsInfo)) { Console.WriteLine("RC4 encrypt done.\n"); } else { Console.WriteLine("RC4 encrypt failed.\n"); result = false; } if (EncryptUseAES128Algo(document, permissionsInfo)) { Console.WriteLine("AES128 encrypt done.\n"); } else { Console.WriteLine("AES128 encrypt failed.\n"); result = false; } if (EncryptUseAES256Algo(document, permissionsInfo)) { Console.WriteLine("AES256 encrypt done.\n"); } else { Console.WriteLine("AES256 encrypt failed.\n"); result = false; } if (EncryptUseNoEncryptAlgo(document, permissionsInfo)) { Console.WriteLine("NoEncryptAlgo encrypt done.\n"); } else { Console.WriteLine("NoEncryptAlgo encrypt failed.\n"); result = false; } return result; } static private bool EncryptByOwnerPassword(CPDFDocument document) { userPassword = null; ownerPassword = "Owner"; CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo(); permissionsInfo.AllowsPrinting = false; permissionsInfo.AllowsCopying = false; CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4; document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel); string encryptPath = outputPath + "\\EncryptByOwnerPasswordTest.pdf"; if (!document.WriteToFilePath(encryptPath)) { return false; } CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath); if (encryptedDoc.IsEncrypted) { Console.WriteLine("File is encrypted."); Console.WriteLine("Browse the changed file in " + encryptPath); Console.WriteLine("Owner password is: {0}", ownerPassword); } else { Console.WriteLine("File encrypt failed"); return false; } return true; } static private bool EncryptByAllPasswords(CPDFDocument document) { userPassword = "User"; ownerPassword = "Owner"; CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo(); permissionsInfo.AllowsPrinting = false; permissionsInfo.AllowsCopying = false; CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4; document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel); string encryptPath = outputPath + "\\EncryptByAllPasswordsTest.pdf"; if (!document.WriteToFilePath(encryptPath)) { return false; } CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath); if (encryptedDoc.IsEncrypted) { Console.WriteLine("File is encrypted."); Console.WriteLine("Browse the changed file in " + encryptPath); Console.WriteLine("User password is: {0}", userPassword); Console.WriteLine("Owner password is: {0}", ownerPassword); } else { Console.WriteLine("File encrypt failed"); return false; } return true; } static private void PrintPermissionsInfo(CPDFPermissionsInfo permissionsInfo) { Console.Write("AllowsPrinting: "); Console.Write(permissionsInfo.AllowsPrinting == true ? "Yes\n" : "No\n"); Console.Write("AllowsCopying: "); Console.Write(permissionsInfo.AllowsCopying == true ? "Yes\n" : "No\n"); } static private bool Unlock(CPDFDocument document) { userPassword = "User"; ownerPassword = "Owner"; if (document.IsLocked) { Console.WriteLine("Document is locked"); } Console.WriteLine("Unlock with user password"); document.UnlockWithPassword(userPassword); if (!document.IsLocked) { Console.WriteLine("Document is unlocked"); } else { return false; } PrintPermissionsInfo(document.GetPermissionsInfo()); Console.WriteLine("Unlock with owner password"); document.CheckOwnerPassword(ownerPassword); PrintPermissionsInfo(document.GetPermissionsInfo()); return true; } static private bool Decrypt(CPDFDocument document) { userPassword = "User"; ownerPassword = "Owner"; string decryptPath = outputPath + "\\DecryptTest.pdf"; document.UnlockWithPassword(userPassword); if (!document.Decrypt(decryptPath)) { return false; } CPDFDocument decryptDocument = CPDFDocument.InitWithFilePath(decryptPath); if (decryptDocument.IsEncrypted) { return false; } else { Console.WriteLine("Document decrypt done."); } return true; } } }