Java Tutorial/PDF/PDF Encrypt Decrypt
Содержание
Decrypt Pdf file
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfEncryptor
.encrypt(new PdfReader("1.pdf"), new FileOutputStream("Encrypted2.pdf"),
"Hello".getBytes(), "World".getBytes(), PdfWriter.AllowDegradedPrinting,
PdfWriter.STRENGTH128BITS);
// decrypt the file
PdfReader reader = new PdfReader("Encrypted2.pdf", "World".getBytes());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("Decrypted1.pdf"));
stamper.close();
getEncryptionInformation("1.pdf", null);
getEncryptionInformation("Encrypted2.pdf", "World");
getEncryptionInformation("Decrypted1.pdf", "World");
}
public static void getEncryptionInformation(String filename, String ownerpassword)
throws Exception {
PdfReader reader;
if (ownerpassword == null)
reader = new PdfReader(filename);
else
reader = new PdfReader(filename, ownerpassword.getBytes());
System.out.println("Encrypted? " + reader.isEncrypted());
if (reader.isEncrypted()) {
System.out.println("Permissions: "
+ PdfEncryptor.getPermissionsVerbose(reader.getPermissions()));
System.out.println("128 bit? " + reader.is128Key());
}
}
}
Degraded printing
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfEncryptor
.encrypt(new PdfReader("1.pdf"), new FileOutputStream("Encrypted2.pdf"),
"Hello".getBytes(), "World".getBytes(), PdfWriter.AllowDegradedPrinting,
PdfWriter.STRENGTH128BITS);
getEncryptionInformation("1.pdf", null);
getEncryptionInformation("Encrypted2.pdf", "World");
}
public static void getEncryptionInformation(String filename, String ownerpassword)
throws Exception {
PdfReader reader;
if (ownerpassword == null)
reader = new PdfReader(filename);
else
reader = new PdfReader(filename, ownerpassword.getBytes());
System.out.println("Encrypted? " + reader.isEncrypted());
if (reader.isEncrypted()) {
System.out.println("Permissions: "
+ PdfEncryptor.getPermissionsVerbose(reader.getPermissions()));
System.out.println("128 bit? " + reader.is128Key());
}
}
}
Encrypt the file with PdfStamper
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("1.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("Encrypted1.pdf"));
stamper.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.AllowPrinting
| PdfWriter.AllowCopy, PdfWriter.STRENGTH40BITS);
stamper.close();
getEncryptionInformation("1.pdf", null);
getEncryptionInformation("Encrypted1.pdf", "World");
}
public static void getEncryptionInformation(String filename, String ownerpassword)
throws Exception {
PdfReader reader;
if (ownerpassword == null)
reader = new PdfReader(filename);
else
reader = new PdfReader(filename, ownerpassword.getBytes());
System.out.println("Encrypted? " + reader.isEncrypted());
if (reader.isEncrypted()) {
System.out.println("Permissions: "
+ PdfEncryptor.getPermissionsVerbose(reader.getPermissions()));
System.out.println("128 bit? " + reader.is128Key());
}
}
}
Get Encryption Information
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("1.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("Encrypted1.pdf"));
stamper.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.AllowPrinting
| PdfWriter.AllowCopy, PdfWriter.STRENGTH40BITS);
stamper.close();
getEncryptionInformation("1.pdf", null);
getEncryptionInformation("Encrypted1.pdf", "World");
}
public static void getEncryptionInformation(String filename, String ownerpassword)
throws Exception {
PdfReader reader;
if (ownerpassword == null)
reader = new PdfReader(filename);
else
reader = new PdfReader(filename, ownerpassword.getBytes());
System.out.println("Encrypted? " + reader.isEncrypted());
if (reader.isEncrypted()) {
System.out.println("Permissions: "
+ PdfEncryptor.getPermissionsVerbose(reader.getPermissions()));
System.out.println("128 bit? " + reader.is128Key());
}
}
}