下载个PDF居然还要密码?想要密码就付费?这我能忍你!Python分分钟解密它!
?
?
概敘
? ? ? ?今天因為需要用到一個參考文檔,然后在網上找了一下,就下載了一個pdf文檔!下載之后居然是加密的,如果要密碼就要給錢?我這暴脾氣,還真不能忍你!
? ? ? ???
想收我得錢,不存在的,接下來就是破解過程了!
說句題外話,為什么現在越來越多的文檔都是PDF格式呢?因為它是可移植文檔格式(PDF)是在線附件中使用最廣泛的文件格式之一。
大多數文檔,例如電子書、掃描文檔等,都使用這種pdf格式進行存儲和共享。
為了在各種平臺和媒體上共享,文檔面臨著許多威脅,例如信息泄漏和未經授權的個人訪問。因此,一個人必須對他/她的文件進行加密,以防止這種可以避免的事件和暴露。
Python是一種足智多謀的編程語言,它提供了一個非常棒的模塊,稱為PyPDF2對pdf文件進行加密和解密,從而增強所包含信息的安全性。
PyPDF 2庫能夠:
PyPDF2不是內置的庫,因此需要先安裝它,然后才能使用:
pip3 install PyPDF2下面的代碼實現中使用的文件可以找到這里
加密PDF文件
文件加密需要在文檔中添加密碼,從而允許只訪問授權人員。
為此,我們將遵循以下步驟:
步驟1:使用Reader對象打開pdf。
步驟2:通過迭代創建原始文件的副本
每一頁,并將其添加到新的pdf文件。
步驟3:加密新的pdf文件。
守則執行
# Import the required module and sub-modules from PyPDF2 import PdfFileWriter from PyPDF2 import PdfFileReader# Create a PdfFileWriter object result = PdfFileWriter()# Open the pdf file to encrypt file = PdfFileReader('Magazine.pdf')# Retrieve the number of pages to iterate in the original document length = file.numPages# Iterates through every page and adds it to the new file (a copy of the original) for i in range(length):pages = file.getPage(i)result.addPage(pages)# Creates a variable password. password = 'pam&Lab890'# Encrypt the file using the created password result.encrypt(password)# Open a new file 'Magazines.pdf' and write the encrypted pdf file with open('Magazines.pdf','wb') as f:result.write(f)這將創建原始文件的類似副本(可能具有指定的不同名稱),需要設置密碼才能啟用訪問。
解密PDF文件
解密用給定的密碼解鎖文檔,并將其轉換為不需要密碼密鑰訪問的pdf文件。
文件解密步驟與加密步驟幾乎類似。
步驟1:使用Reader對象打開pdf。
步驟2:加密新的pdf文件。
步驟3:通過迭代創建原始文件的副本
每一頁,并將其添加到新的pdf文件。
守則執行
# Import the required module and sub-modules from PyPDF2 import PdfFileWriter from PyPDF2 import PdfFileReader# Create a PdfFileWriter object result = PdfFileWriter()# Open the password - secured pdf file to decrypt file = PdfFileReader('Magazines.pdf')# Creates a variable password. password = 'pam&Lab890'# First, check if the file is encrypted then proceed if encrypted if file.isEncrypted:# Decrypt the file using the givenpassword file.decrypt(password)# Iterates through every page and adds it to the new file for i in range(31):pages = file.getPage(i)result.addPage(pages)# Open a new file 'Magazines1.pdf' and write the encrypted pdf filewith open('Magazines1.pdf','wb') as f:result.write(f)print('File decrypted successfully')else:print('File is not encrypted')如果你們發現這篇文章很有幫助,那么保存它,這樣你就可以隨時參考:shaush:。對于那些想了解更多關于PyPDF 2模塊和功能的人,只需在下面的注釋部分中推薦特定的區域(是否合并、拆分e.t.c)。
???
需要完整代碼:點這里即可
總結
以上是生活随笔為你收集整理的下载个PDF居然还要密码?想要密码就付费?这我能忍你!Python分分钟解密它!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PDF解密网站
- 下一篇: python 拦截windows弹窗广告