EncryptTest.cs 13 KB

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