-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFCrypt.java
More file actions
107 lines (101 loc) · 3.26 KB
/
Copy pathPDFCrypt.java
File metadata and controls
107 lines (101 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* This class allows users to encrypt or decrypt multiple PDF files through an
* intuitive graphical interface (without using the PDFBox's command line).
*
* @author Eric Normandin
*/
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.NoSuchFileException;
/*******************
* PDFbox *
*******************/
import org.apache.pdfbox.Loader;
//import org.apache.pdfbox.util.Version;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
public class PDFCrypt {
public int encryptPDF(
String inputFilePath,
String outputFilePath,
char[] userPassword,
char[] ownerPassword) throws IOException {
try (PDDocument document = Loader.loadPDF(new File(inputFilePath))) {
// Load the PDF file
//PDDocument document = Loader.loadPDF(new File(inputFilePath));
// Create a protection policy with AES 128-bit
AccessPermission accessPermission = new AccessPermission();
//StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(
// String.copyValueOf(ownerPassword),
// String.copyValueOf(userPassword),
// accessPermission
//);
StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(
String.valueOf(ownerPassword),
String.valueOf(userPassword),
accessPermission
);
protectionPolicy.setEncryptionKeyLength(128); // AES 128-bit
protectionPolicy.setPermissions(accessPermission);
// Apply encryption to the document
document.protect(protectionPolicy);
// Delete the destination file if it already exists
Files.deleteIfExists(Paths.get(outputFilePath));
// Save the encrypted document
document.save(outputFilePath);
document.close(); // not necessary, the "try-with-resources" do it also.
return 0;
}
catch (NoSuchFileException e) {
//e.printStackTrace();
return 1;
}
catch (IOException e) {
//e.printStackTrace();
return 1;
}
catch (Exception e) {
//e.printStackTrace();
return 1;
}
}
public int decryptPDF(
String inputFilePath,
String outputFilePath,
char[] password) throws IOException {
//try (PDDocument document = Loader.loadPDF(new File(inputFilePath), String.copyValueOf(password))) {
try (PDDocument document = Loader.loadPDF(new File(inputFilePath), String.valueOf(password))) {
// Load the encrypted PDF document using Loader.loadPDF
//PDDocument document = Loader.loadPDF(new File(inputFilePath), password);
// Remove document protection
document.setAllSecurityToBeRemoved(true);
// Delete the destination file if it already exists
Files.deleteIfExists(Paths.get(outputFilePath));
// Save the decrypted document
document.save(outputFilePath);
document.close(); // not necessary, the "try-with-resources" do it also.
//Success
return 0;
}
catch (InvalidPasswordException e) {
//e.printStackTrace();
return 1;
}
catch (NoSuchFileException e) {
//e.printStackTrace();
return 1;
}
catch (IOException e) {
//e.printStackTrace();
return 1;
}
catch (Exception e) {
//e.printStackTrace();
return 1;
}
}
}