java 解密PDF格式文件

 

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[] argsthrows 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());
    }
  }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 提供了许多加密和解密 PDF 文档的库。其中一些库是: 1. Apache PDFBox:它是一个开源库,可以用来创建、修改和提取 PDF 文档。它还提供了加密和解密 PDF 文档的功能。 2. iText:它是一个广泛使用的 PDF 库,可以用来创建、修改和提取 PDF 文档。它还提供了加密和解密 PDF 文档的功能。 以下是使用 Apache PDFBox 加密和解密 PDF 文档的示例代码: 1. 加密 PDF 文档: ``` import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.encryption.AccessPermission; import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; import java.io.File; import java.io.IOException; public class PDFEncryptor { public static void main(String[] args) throws IOException { // 加载 PDF 文档 PDDocument document = PDDocument.load(new File("example.pdf")); // 设置访问权限 AccessPermission ap = new AccessPermission(); ap.setCanPrint(false); // 设置加密策略 StandardProtectionPolicy spp = new StandardProtectionPolicy("password", "ownerpassword", ap); spp.setEncryptionKeyLength(128); // 应用加密策略 document.protect(spp); // 保存加密后的 PDF 文档 document.save("example-encrypted.pdf"); document.close(); } } ``` 2. 解密 PDF 文档: ``` import org.apache.pdfbox.pdmodel.PDDocument; import java.io.File; import java.io.IOException; public class PDFDecryptor { public static void main(String[] args) throws IOException { // 加载加密的 PDF 文档 PDDocument document = PDDocument.load(new File("example-encrypted.pdf"), "password"); // 解密 PDF 文档 document.setAllSecurityToBeRemoved(true); // 保存解密后的 PDF 文档 document.save("example-decrypted.pdf"); document.close(); } } ``` 这些示例代码使用 Apache PDFBox 库加密和解密 PDF 文档。您可以根据需要使用其他库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值