EncryptTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace EncryptTest
  9. {
  10. internal class EncryptTest
  11. {
  12. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\CS";
  13. static private string userPassword = string.Empty;
  14. static private string ownerPassword = string.Empty;
  15. static void Main(string[] args)
  16. {
  17. #region Perparation work
  18. Console.WriteLine("Running Encrypt test sample…\r\n");
  19. SDKLicenseHelper.LicenseVerify();
  20. if (!Directory.Exists(outputPath))
  21. {
  22. Directory.CreateDirectory(outputPath);
  23. }
  24. #endregion
  25. #region Sample 1: Encrypt by user password
  26. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  27. if (EncryptByUserPassword(document))
  28. {
  29. Console.WriteLine("Encrypt by user password done.");
  30. }
  31. else
  32. {
  33. Console.WriteLine("Encrypt by user password failed.");
  34. }
  35. document.Release();
  36. Console.WriteLine("--------------------");
  37. #endregion
  38. #region Sample 2: Encrypt by owner password
  39. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  40. if (EncryptByOwnerPassword(document))
  41. {
  42. Console.WriteLine("Encrypt by owner password done.");
  43. }
  44. else
  45. {
  46. Console.WriteLine("Encrypt by owner password failed.");
  47. }
  48. document.Release();
  49. Console.WriteLine("--------------------");
  50. #endregion
  51. #region Sample 3: Encrypt by all passwords
  52. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  53. if (EncryptByAllPasswords(document))
  54. {
  55. Console.WriteLine("Encrypt by Both user and owner passwords done.");
  56. }
  57. else
  58. {
  59. Console.WriteLine("Encrypt by Both user and owner passwords failed.");
  60. }
  61. document.Release();
  62. Console.WriteLine("--------------------");
  63. #endregion
  64. #region Sample 4: Unlock
  65. document = CPDFDocument.InitWithFilePath("AllPasswords.pdf");
  66. if (Unlock(document))
  67. {
  68. Console.WriteLine("Unlock done.");
  69. }
  70. else
  71. {
  72. Console.WriteLine("Unlock failed.");
  73. }
  74. document.Release();
  75. Console.WriteLine("--------------------");
  76. #endregion
  77. #region Sample 5: Decrypt
  78. document = CPDFDocument.InitWithFilePath("AllPasswords.pdf");
  79. if (Decrypt(document))
  80. {
  81. Console.WriteLine("Decrypt done.");
  82. }
  83. else
  84. {
  85. Console.WriteLine("Decrypt failed.");
  86. }
  87. document.Release();
  88. Console.WriteLine("--------------------");
  89. #endregion
  90. Console.WriteLine("Done!");
  91. Console.WriteLine("--------------------");
  92. Console.ReadLine();
  93. }
  94. static private bool EncryptUseRC4Algo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo)
  95. {
  96. CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4;
  97. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel);
  98. string encryptPath = outputPath + "\\EncryptUseRC4Test.pdf";
  99. if (!document.WriteToFilePath(encryptPath))
  100. {
  101. return false;
  102. }
  103. CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath);
  104. if (encryptedDoc.IsEncrypted)
  105. {
  106. Console.WriteLine("File is encrypted");
  107. Console.WriteLine("Browse the changed file in: " + encryptPath);
  108. Console.WriteLine("User password is: {0}", userPassword);
  109. }
  110. else
  111. {
  112. Console.WriteLine("File encrypt failed");
  113. return false;
  114. }
  115. return true;
  116. }
  117. static private bool EncryptUseAES128Algo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo)
  118. {
  119. CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelAES128;
  120. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel);
  121. string encryptPath = outputPath + "\\EncryptUseAES128Test.pdf";
  122. if (!document.WriteToFilePath(encryptPath))
  123. {
  124. return false;
  125. }
  126. CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath);
  127. if (encryptedDoc.IsEncrypted)
  128. {
  129. Console.WriteLine("File is encrypted");
  130. Console.WriteLine("Browse the changed file in: " + encryptPath);
  131. Console.WriteLine("User password is: {0}", userPassword);
  132. }
  133. else
  134. {
  135. Console.WriteLine("File encrypt failed");
  136. return false;
  137. }
  138. return true;
  139. }
  140. static private bool EncryptUseAES256Algo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo)
  141. {
  142. CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelAES256;
  143. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel);
  144. string encryptPath = outputPath + "\\EncryptUseAES256Test.pdf";
  145. if (!document.WriteToFilePath(encryptPath))
  146. {
  147. return false;
  148. }
  149. CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath);
  150. if (encryptedDoc.IsEncrypted)
  151. {
  152. Console.WriteLine("File is encrypted");
  153. Console.WriteLine("Browse the changed file in " + encryptPath);
  154. Console.WriteLine("User password is: {0}", userPassword);
  155. }
  156. else
  157. {
  158. Console.WriteLine("File encrypt failed");
  159. return false;
  160. }
  161. return true;
  162. }
  163. static private bool EncryptUseNoEncryptAlgo(CPDFDocument document, CPDFPermissionsInfo permissionsInfo)
  164. {
  165. CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelNoEncryptAlgo;
  166. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel);
  167. string encryptPath = outputPath + "\\EncryptUseNoEncryptAlgoTest.pdf";
  168. if (!document.WriteToFilePath(encryptPath))
  169. {
  170. return false;
  171. }
  172. CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath);
  173. if (encryptedDoc.IsEncrypted)
  174. {
  175. Console.WriteLine("File is encrypted.");
  176. Console.WriteLine("Browse the changed file in " + encryptPath);
  177. Console.WriteLine("User password is: {0}", userPassword);
  178. }
  179. else
  180. {
  181. Console.WriteLine("File encrypt failed");
  182. return false;
  183. }
  184. return true;
  185. }
  186. /// <summary>
  187. /// Using RC4, AES128, AES256, NoEncrypt algorithm to encrypt document.
  188. /// User password: User
  189. /// No owner password
  190. /// </summary>
  191. /// <param name="document">Regular document</param>
  192. static private bool EncryptByUserPassword(CPDFDocument document)
  193. {
  194. bool result = true;
  195. userPassword = "User";
  196. ownerPassword = string.Empty;
  197. CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
  198. if (EncryptUseRC4Algo(document, permissionsInfo))
  199. {
  200. Console.WriteLine("RC4 encrypt done.\n");
  201. }
  202. else
  203. {
  204. Console.WriteLine("RC4 encrypt failed.\n");
  205. result = false;
  206. }
  207. if (EncryptUseAES128Algo(document, permissionsInfo))
  208. {
  209. Console.WriteLine("AES128 encrypt done.\n");
  210. }
  211. else
  212. {
  213. Console.WriteLine("AES128 encrypt failed.\n");
  214. result = false;
  215. }
  216. if (EncryptUseAES256Algo(document, permissionsInfo))
  217. {
  218. Console.WriteLine("AES256 encrypt done.\n");
  219. }
  220. else
  221. {
  222. Console.WriteLine("AES256 encrypt failed.\n");
  223. result = false;
  224. }
  225. if (EncryptUseNoEncryptAlgo(document, permissionsInfo))
  226. {
  227. Console.WriteLine("NoEncryptAlgo encrypt done.\n");
  228. }
  229. else
  230. {
  231. Console.WriteLine("NoEncryptAlgo encrypt failed.\n");
  232. result = false;
  233. }
  234. return result;
  235. }
  236. /// <summary>
  237. /// Encrypt by owner password
  238. /// No user password
  239. /// Owner password: Owner
  240. /// </summary>
  241. /// <param name="document">Regular document</param>
  242. static private bool EncryptByOwnerPassword(CPDFDocument document)
  243. {
  244. userPassword = null;
  245. ownerPassword = "Owner";
  246. CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
  247. permissionsInfo.AllowsPrinting = false;
  248. permissionsInfo.AllowsCopying = false;
  249. CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4;
  250. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel);
  251. string encryptPath = outputPath + "\\EncryptByOwnerPasswordTest.pdf";
  252. if (!document.WriteToFilePath(encryptPath))
  253. {
  254. return false;
  255. }
  256. CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath);
  257. if (encryptedDoc.IsEncrypted)
  258. {
  259. Console.WriteLine("File is encrypted.");
  260. Console.WriteLine("Browse the changed file in " + encryptPath);
  261. Console.WriteLine("Owner password is: {0}", ownerPassword);
  262. }
  263. else
  264. {
  265. Console.WriteLine("File encrypt failed");
  266. return false;
  267. }
  268. return true;
  269. }
  270. /// <summary>
  271. /// Encrypt by all passwords
  272. /// User password: User
  273. /// Owner password: Owner
  274. /// </summary>
  275. /// <param name="document">Regular document</param>
  276. static private bool EncryptByAllPasswords(CPDFDocument document)
  277. {
  278. userPassword = "User";
  279. ownerPassword = "Owner";
  280. CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo();
  281. permissionsInfo.AllowsPrinting = false;
  282. permissionsInfo.AllowsCopying = false;
  283. CPDFDocumentEncryptionLevel encryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4;
  284. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel);
  285. string encryptPath = outputPath + "\\EncryptByAllPasswordsTest.pdf";
  286. if (!document.WriteToFilePath(encryptPath))
  287. {
  288. return false;
  289. }
  290. CPDFDocument encryptedDoc = CPDFDocument.InitWithFilePath(encryptPath);
  291. if (encryptedDoc.IsEncrypted)
  292. {
  293. Console.WriteLine("File is encrypted.");
  294. Console.WriteLine("Browse the changed file in " + encryptPath);
  295. Console.WriteLine("User password is: {0}", userPassword);
  296. Console.WriteLine("Owner password is: {0}", ownerPassword);
  297. }
  298. else
  299. {
  300. Console.WriteLine("File encrypt failed");
  301. return false;
  302. }
  303. return true;
  304. }
  305. /// <summary>
  306. /// Print printing and copying permissions info.
  307. /// </summary>
  308. /// <param name="permissionsInfo">Permissions info about a document</param>
  309. static private void PrintPermissionsInfo(CPDFPermissionsInfo permissionsInfo)
  310. {
  311. Console.Write("AllowsPrinting: ");
  312. Console.Write(permissionsInfo.AllowsPrinting == true ? "Yes\n" : "No\n");
  313. Console.Write("AllowsCopying: ");
  314. Console.Write(permissionsInfo.AllowsCopying == true ? "Yes\n" : "No\n");
  315. }
  316. /// <summary>
  317. /// Unlock document
  318. /// </summary>
  319. /// <param name="document"></param>
  320. static private bool Unlock(CPDFDocument document)
  321. {
  322. userPassword = "User";
  323. ownerPassword = "Owner";
  324. //Check if document is locked
  325. if (document.IsLocked)
  326. {
  327. Console.WriteLine("Document is locked");
  328. }
  329. PrintPermissionsInfo(document.GetPermissionsInfo());
  330. Console.WriteLine("Unlock with owner password");
  331. document.CheckOwnerPassword("123");
  332. //Check permissions info.
  333. PrintPermissionsInfo(document.GetPermissionsInfo());
  334. return true;
  335. }
  336. /// <summary>
  337. /// Decrypted
  338. /// </summary>
  339. /// <param name="document"></param>
  340. static private bool Decrypt(CPDFDocument document)
  341. {
  342. userPassword = "User";
  343. ownerPassword = "Owner";
  344. string decryptPath = outputPath + "\\DecryptTest.pdf";
  345. document.UnlockWithPassword(userPassword);
  346. if (!document.Decrypt(decryptPath))
  347. {
  348. return false;
  349. }
  350. CPDFDocument decryptDocument = CPDFDocument.InitWithFilePath(decryptPath);
  351. if (decryptDocument.IsEncrypted)
  352. {
  353. return false;
  354. }
  355. else
  356. {
  357. Console.WriteLine("Document decrypt done.");
  358. }
  359. return true;
  360. }
  361. }
  362. }