In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "the implementation of PDF encryption", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the implementation of PDF encryption"!
Pdf encryption implementation
In the work, the requirement is to encrypt the pdf memory permission, distinguish the permission to open, edit and print, and set the password. after investigation, there are two ways to realize it, but these two methods are not well compatible with most pdf software on the market, and after restricting the printing function, few software can explicitly require to enter the print password, which shows that the print button is grayed out and cannot be printed. It is silly to print only after entering ownerPassword (file owner permission). When there is a need to enter a password interface for printing, please bring your knife to re-discuss the requirements with the product.
Adobe Acrobat DC
The software is a relatively standard pdf tool in the market. The detailed pdf access control interface of the software is shown in the figure:
As can be seen from the picture:
This tool has two passwords, namely, the user permission password (required to open pdf) and the owner permission password (editing and other permissions). After testing, the owner permission password can be viewed. For example, other tools such as chrome have an interface to enter the password when opened.
N multiple permissions
1: allow printing
2: allow changes
With all these permissions in total, let's start our implementation.
Implemented through pdfbox
The implementation of pdfbox encryption is very simple. Of course, the functions of this class are not only encryption, but also many implementations. For more information, please see official demo and api https://pdfbox.apache.org/docs/2.0.13/javadocs/.
Pom dependence
Org.apache.pdfbox pdfbox 2.0.16 org.bouncycastle bcprov-jdk15on 1.57
PDFEncryptUtils.java
/ *
Control the permissions of pdf
* @ author Calvin * @ date 2019-07-17 * @ since v1.0 * / public class PDFEncryptUtils {/ * encryption * @ param fileName File name * @ param fileAuth File permission * @ throws Exception * / public static void encrypt (String fileName, FileAuth fileAuth) throws Exception {File file = new File (fileName) PDDocument document = PDDocument.load (file); AccessPermission permissions = new AccessPermission (); / / it is easy to implement here, and there are many permissions. Only the most commonly used permissions are implemented here. Permissions.setCanExtractContent (fileAuth.getOpen ()! = 1) can be operated by default in opening, editing, and printing / / permissions. Permissions.setCanModify (fileAuth.getEdit ()! = 1); permissions.setCanPrint (fileAuth.getPrint ()! = 1); StandardProtectionPolicy policy = new StandardProtectionPolicy (fileAuth.getOwnerPassword (), fileAuth.getUserPassword (), permissions); SecurityHandler handler = new StandardSecurityHandler (policy); handler.prepareDocumentForEncryption (document) PDEncryption encryption = new PDEncryption (); encryption.setSecurityHandler (handler); document.setEncryptionDictionary (encryption); / / Save the original path document.save (file.getPath ());}}
FileAuth.java
/ *
User permissions
* * @ author Calvin * @ date 2019-07-15 * @ since v1.0 * / public class FileAuth {/ * can I open * / private int open; / * can I edit * / private int edit / * can I print * / private int print; / * * owner permission password * / private String ownerPassword; / * user permission password * / private String userPassword; public int getOpen () {return open } public void setOpen (int open) {this.open = open;} public int getEdit () {return edit;} public void setEdit (int edit) {this.edit = edit;} public int getPrint () {return print } public void setPrint (int print) {this.print = print;} public String getOwnerPassword () {return ownerPassword;} public void setOwnerPassword (String ownerPassword) {this.ownerPassword = ownerPassword;} public String getUserPassword () {return userPassword } public void setUserPassword (String userPassword) {this.userPassword = userPassword;}} encrypt pdf with itext-pdf com.itextpdf itextpdf 5.5.11
PDFEncryptUtils.java
/ * * set permissions for pdf * @ param pdfStamper pdf file * @ param fileAuth file permission password * @ throws IOException file exception * @ throws DocumentException * / public static void encrypt (PdfStamper pdfStamper, FileAuth fileAuth) throws DocumentException, IOException {pdfStamper.setEncryption (null, null, PdfWriter.ALLOW_COPY, true); pdfStamper.setEncryption (null, null, PdfWriter.ALLOW_DEGRADED_PRINTING, true) PdfStamper.setEncryption (null, null, PdfWriter.ALLOW_FILL_IN, true); pdfStamper.setEncryption (null, null, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, true); pdfStamper.setEncryption (null, null, PdfWriter.ALLOW_MODIFY_CONTENTS, true); pdfStamper.setEncryption (null, null, PdfWriter.ALLOW_PRINTING, true); byte [] userpassword = fileAuth.getUserPassword (). GetBytes (); byte [] owenerpassword = fileAuth.getOwnerPassword () .getBytes () If (fileAuth.getOpen () = = 1) {pdfStamper.setEncryption (userpassword, userpassword, PdfWriter.ALLOW_SCREENREADERS, false);} if (fileAuth.getEdit () = = 1) {pdfStamper.setEncryption (userpassword, owenerpassword, PdfWriter.ALLOW_PRINTING, false); pdfStamper.setEncryption (userpassword, owenerpassword, PdfWriter.ALLOW_DEGRADED_PRINTING, false) } if (fileAuth.getPrint () = = 1) {pdfStamper.setEncryption (userpassword, owenerpassword, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, false); pdfStamper.setEncryption (userpassword, owenerpassword, PdfWriter.ALLOW_MODIFY_CONTENTS, false); pdfStamper.setEncryption (userpassword, owenerpassword, PdfWriter.ALLOW_FILL_IN, false);} pdfStamper.close () }
Client call
Public static void main (String [] args) throws IOException, DocumentException, NoSuchFieldException, IllegalAccessException {PdfReader reader = new PdfReader ("D:\\ 4028832b6c4af5e2016c4af694310044.pdf"); java.lang.reflect.Field f = reader.getClass (). GetDeclaredField ("encrypted"); f.setAccessible (true); f.set (reader, false); PdfStamper stamper = new PdfStamper (reader, new FileOutputStream ("D:\\ test1.pdf")); FileAuth fileAuth = new FileAuth () FileAuth.setEdit (1); fileAuth.setOpen (1); fileAuth.setPrint (1); fileAuth.setUserPassword ("123456"); fileAuth.setOwnerPassword ("654321"); encrypt (stamper, fileAuth);} shortcomings
1: these two methods are not compatible with all the software on the market, and some pdf password cracking tools can decrypt the operation, secure password, or it is recommended to use certificate 2: if the permission to print is limited, then unless you take the initiative to obtain the owner permission of pdf, the print button is disabled, cannot print, and there is no place to enter the print password.
At this point, I believe you have a deeper understanding of the "implementation of PDF encryption", you might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.