EncryptTest.vb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. Imports ComPDFKit.PDFDocument
  2. Imports System
  3. Imports System.IO
  4. Module EncryptTest
  5. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) & "\Output\VB"
  6. Private userPassword As String = String.Empty
  7. Private ownerPassword As String = String.Empty
  8. Sub Main(args As String())
  9. Console.WriteLine("Running Encrypt test sample..." & vbCrLf)
  10. SDKLicenseHelper.LicenseVerify()
  11. If Not Directory.Exists(outputPath) Then
  12. Directory.CreateDirectory(outputPath)
  13. End If
  14. ' Encrypt by user password
  15. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  16. If EncryptByUserPassword(document) Then
  17. Console.WriteLine("Encrypt by user password done.")
  18. Else
  19. Console.WriteLine("Encrypt by user password failed.")
  20. End If
  21. document.Release()
  22. Console.WriteLine("--------------------")
  23. ' Encrypt by owner password
  24. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  25. If EncryptByOwnerPassword(document) Then
  26. Console.WriteLine("Encrypt by owner password done.")
  27. Else
  28. Console.WriteLine("Encrypt by owner password failed.")
  29. End If
  30. document.Release()
  31. Console.WriteLine("--------------------")
  32. ' Encrypt by both user and owner passwords
  33. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  34. If EncryptByAllPasswords(document) Then
  35. Console.WriteLine("Encrypt by Both user and owner passwords done.")
  36. Else
  37. Console.WriteLine("Encrypt by Both user and owner passwords failed.")
  38. End If
  39. document.Release()
  40. Console.WriteLine("--------------------")
  41. ' Unlock
  42. document = CPDFDocument.InitWithFilePath("AllPasswords.pdf")
  43. If Unlock(document) Then
  44. Console.WriteLine("Unlock done.")
  45. Else
  46. Console.WriteLine("Unlock failed.")
  47. End If
  48. document.Release()
  49. Console.WriteLine("--------------------")
  50. ' Decrypt
  51. document = CPDFDocument.InitWithFilePath("AllPasswords.pdf")
  52. If Decrypt(document) Then
  53. Console.WriteLine("Decrypt done.")
  54. Else
  55. Console.WriteLine("Decrypt failed.")
  56. End If
  57. document.Release()
  58. Console.WriteLine("--------------------")
  59. Console.WriteLine("Done!")
  60. Console.WriteLine("--------------------")
  61. Console.ReadLine()
  62. End Sub
  63. Private Function EncryptUseRC4Algo(document As CPDFDocument, permissionsInfo As CPDFPermissionsInfo) As Boolean
  64. Dim encryptionLevel As CPDFDocumentEncryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4
  65. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel)
  66. Dim encryptPath As String = outputPath & "\EncryptUseRC4Test.pdf"
  67. If Not document.WriteToFilePath(encryptPath) Then
  68. Return False
  69. End If
  70. Dim encryptedDoc As CPDFDocument = CPDFDocument.InitWithFilePath(encryptPath)
  71. If encryptedDoc.IsEncrypted Then
  72. Console.WriteLine("File is encrypted")
  73. Console.WriteLine("Browse the changed file in: " & encryptPath)
  74. Console.WriteLine("User password is: {0}", userPassword)
  75. Else
  76. Console.WriteLine("File encrypt failed")
  77. Return False
  78. End If
  79. Return True
  80. End Function
  81. Private Function EncryptUseAES128Algo(document As CPDFDocument, permissionsInfo As CPDFPermissionsInfo) As Boolean
  82. Dim encryptionLevel As CPDFDocumentEncryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelAES128
  83. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel)
  84. Dim encryptPath As String = outputPath & "\EncryptUseAES128Test.pdf"
  85. If Not document.WriteToFilePath(encryptPath) Then
  86. Return False
  87. End If
  88. Dim encryptedDoc As CPDFDocument = CPDFDocument.InitWithFilePath(encryptPath)
  89. If encryptedDoc.IsEncrypted Then
  90. Console.WriteLine("File is encrypted")
  91. Console.WriteLine("Browse the changed file in: " & encryptPath)
  92. Console.WriteLine("User password is: {0}", userPassword)
  93. Else
  94. Console.WriteLine("File encrypt failed")
  95. Return False
  96. End If
  97. Return True
  98. End Function
  99. Private Function EncryptUseAES256Algo(document As CPDFDocument, permissionsInfo As CPDFPermissionsInfo) As Boolean
  100. Dim encryptionLevel As CPDFDocumentEncryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelAES256
  101. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel)
  102. Dim encryptPath As String = outputPath & "\EncryptUseAES256Test.pdf"
  103. If Not document.WriteToFilePath(encryptPath) Then
  104. Return False
  105. End If
  106. Dim encryptedDoc As CPDFDocument = CPDFDocument.InitWithFilePath(encryptPath)
  107. If encryptedDoc.IsEncrypted Then
  108. Console.WriteLine("File is encrypted")
  109. Console.WriteLine("Browse the changed file in " & encryptPath)
  110. Console.WriteLine("User password is: {0}", userPassword)
  111. Else
  112. Console.WriteLine("File encrypt failed")
  113. Return False
  114. End If
  115. Return True
  116. End Function
  117. Private Function EncryptUseNoEncryptAlgo(document As CPDFDocument, permissionsInfo As CPDFPermissionsInfo) As Boolean
  118. Dim encryptionLevel As CPDFDocumentEncryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelNoEncryptAlgo
  119. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel)
  120. Dim encryptPath As String = outputPath & "\EncryptUseNoEncryptAlgoTest.pdf"
  121. If Not document.WriteToFilePath(encryptPath) Then
  122. Return False
  123. End If
  124. Dim encryptedDoc As CPDFDocument = CPDFDocument.InitWithFilePath(encryptPath)
  125. If encryptedDoc.IsEncrypted Then
  126. Console.WriteLine("File is encrypted.")
  127. Console.WriteLine("Browse the changed file in " & encryptPath)
  128. Console.WriteLine("User password is: {0}", userPassword)
  129. Else
  130. Console.WriteLine("File encrypt failed")
  131. Return False
  132. End If
  133. Return True
  134. End Function
  135. Private Function EncryptByUserPassword(document As CPDFDocument) As Boolean
  136. Dim result As Boolean = True
  137. userPassword = "User"
  138. ownerPassword = String.Empty
  139. Dim permissionsInfo As New CPDFPermissionsInfo()
  140. If EncryptUseRC4Algo(document, permissionsInfo) Then
  141. Console.WriteLine("RC4 encrypt done." & vbCrLf)
  142. Else
  143. Console.WriteLine("RC4 encrypt failed." & vbCrLf)
  144. result = False
  145. End If
  146. If EncryptUseAES128Algo(document, permissionsInfo) Then
  147. Console.WriteLine("AES128 encrypt done." & vbCrLf)
  148. Else
  149. Console.WriteLine("AES128 encrypt failed." & vbCrLf)
  150. result = False
  151. End If
  152. If EncryptUseAES256Algo(document, permissionsInfo) Then
  153. Console.WriteLine("AES256 encrypt done." & vbCrLf)
  154. Else
  155. Console.WriteLine("AES256 encrypt failed." & vbCrLf)
  156. result = False
  157. End If
  158. If EncryptUseNoEncryptAlgo(document, permissionsInfo) Then
  159. Console.WriteLine("NoEncryptAlgo encrypt done." & vbCrLf)
  160. Else
  161. Console.WriteLine("NoEncryptAlgo encrypt failed." & vbCrLf)
  162. result = False
  163. End If
  164. Return result
  165. End Function
  166. Private Function EncryptByOwnerPassword(document As CPDFDocument) As Boolean
  167. userPassword = Nothing
  168. ownerPassword = "Owner"
  169. Dim permissionsInfo As New CPDFPermissionsInfo()
  170. permissionsInfo.AllowsPrinting = False
  171. permissionsInfo.AllowsCopying = False
  172. Dim encryptionLevel As CPDFDocumentEncryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4
  173. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel)
  174. Dim encryptPath As String = outputPath & "\EncryptByOwnerPasswordTest.pdf"
  175. If Not document.WriteToFilePath(encryptPath) Then
  176. Return False
  177. End If
  178. Dim encryptedDoc As CPDFDocument = CPDFDocument.InitWithFilePath(encryptPath)
  179. If encryptedDoc.IsEncrypted Then
  180. Console.WriteLine("File is encrypted.")
  181. Console.WriteLine("Browse the changed file in " & encryptPath)
  182. Console.WriteLine("Owner password is: {0}", ownerPassword)
  183. Else
  184. Console.WriteLine("File encrypt failed")
  185. Return False
  186. End If
  187. Return True
  188. End Function
  189. Private Function EncryptByAllPasswords(document As CPDFDocument) As Boolean
  190. userPassword = "User"
  191. ownerPassword = "Owner"
  192. Dim permissionsInfo As New CPDFPermissionsInfo()
  193. permissionsInfo.AllowsPrinting = False
  194. permissionsInfo.AllowsCopying = False
  195. Dim encryptionLevel As CPDFDocumentEncryptionLevel = CPDFDocumentEncryptionLevel.CPDFDocumentEncryptionLevelRC4
  196. document.Encrypt(userPassword, ownerPassword, permissionsInfo, encryptionLevel)
  197. Dim encryptPath As String = outputPath & "\EncryptByAllPasswordsTest.pdf"
  198. If Not document.WriteToFilePath(encryptPath) Then
  199. Return False
  200. End If
  201. Dim encryptedDoc As CPDFDocument = CPDFDocument.InitWithFilePath(encryptPath)
  202. If encryptedDoc.IsEncrypted Then
  203. Console.WriteLine("File is encrypted.")
  204. Console.WriteLine("Browse the changed file in " & encryptPath)
  205. Console.WriteLine("User password is: {0}", userPassword)
  206. Console.WriteLine("Owner password is: {0}", ownerPassword)
  207. Else
  208. Console.WriteLine("File encrypt failed")
  209. Return False
  210. End If
  211. Return True
  212. End Function
  213. Private Sub PrintPermissionsInfo(permissionsInfo As CPDFPermissionsInfo)
  214. Console.Write("AllowsPrinting: ")
  215. Console.Write(If(permissionsInfo.AllowsPrinting = True, "Yes" & vbCrLf, "No" & vbCrLf))
  216. Console.Write("AllowsCopying: ")
  217. Console.Write(If(permissionsInfo.AllowsCopying = True, "Yes" & vbCrLf, "No" & vbCrLf))
  218. End Sub
  219. Private Function Unlock(document As CPDFDocument) As Boolean
  220. userPassword = "User"
  221. ownerPassword = "Owner"
  222. If document.IsLocked Then
  223. Console.WriteLine("Document is locked")
  224. End If
  225. PrintPermissionsInfo(document.GetPermissionsInfo())
  226. Console.WriteLine("Unlock with owner password")
  227. document.CheckOwnerPassword("123")
  228. PrintPermissionsInfo(document.GetPermissionsInfo())
  229. Return True
  230. End Function
  231. Private Function Decrypt(document As CPDFDocument) As Boolean
  232. userPassword = "User"
  233. ownerPassword = "Owner"
  234. Dim decryptPath As String = outputPath & "\DecryptTest.pdf"
  235. document.UnlockWithPassword(userPassword)
  236. If Not document.Decrypt(decryptPath) Then
  237. Return False
  238. End If
  239. Dim decryptDocument As CPDFDocument = CPDFDocument.InitWithFilePath(decryptPath)
  240. If decryptDocument.IsEncrypted Then
  241. Return False
  242. Else
  243. Console.WriteLine("Document decrypt done.")
  244. End If
  245. Return True
  246. End Function
  247. End Module