BadPasswordException:用户密码错误 [英] BadPasswordException: Bad user password

查看:1117
本文介绍了BadPasswordException:用户密码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建加密的PDF:

I am creating an encrypted PDF using this code:

Document document = new Document(
    new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight()));
PdfWriter writer = PdfWriter.getInstance(document,
    new FileOutputStream(RESULT1));
writer.setBoxSize("art", new Rectangle(36, 54, 555, 791));
writer.setEncryption("Vibhu".getBytes(), "Vibhu@123456789".getBytes(),
    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
document.open();
// Creating Tables and Cells
document.close();

我省略了创建单元格和表格的代码以及创建表格的代码,其中的值如可以输入名字,姓氏。

I omitted to code that creates cells and tables and the code that creates a form in which values such as firstname, lastname can be entered.

然后我操纵这样的表格。

Then I manipulate the form like this.

private void manipulatePdf(String src, String dest, EclaimsVO eclaimsVO,
    String language) throws IOException, DocumentException {
    File file = new File(src);
    if (isObjectPresent(file)) {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper;
        stamper = new PdfStamper(reader, new FileOutputStream(dest),
                '\0', false);
        form = stamper.getAcroFields();
        addToForm("field_2", eclaimsVO.getName());
        addToForm("field_3", eclaimsVO.getSurName());
        stamper.close();
    }
}

我一直在尝试为PDF添加密码文件由 setEncryption()创建文件时,但在我读取文件时抛出此异常:

I have been trying to add a password to a PDF file by setEncryption() when I create the file, but it throws this exception when I read the file:

com.itextpdf.text.exceptions.BadPasswordException: Bad user password
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:681)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:181)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:219)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)

但是,如果我将unethicalreading更改为true并将OWNER-PASSWORD设置为空字符串,则异常不会不会出现。

But, if I change the unethicalreading to true and set the OWNER-PASSWORD as an empty string the exception doesn't come up.

writer.setEncryption("".getBytes(), "Vibhu@123456789".getBytes(),
            PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
PdfReader.unethicalreading = true;

现在当我尝试打开PDF时,它会在没有密码提示的情况下打开。
但是当我打开PDF时,该文档有一个Sample.pdf(SECURED)标题。

And now when I try opening the PDF it gets opened without a password prompt. But the document has a "Sample.pdf(SECURED)" title when I open the PDF.

推荐答案

首先让我专注于一些细节:

First allow me to focus on some details:

Document document = new Document(
    new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight()));

constance A4 Rectangle 具有特定宽度和高度的对象。您可以这样使用它:

The constance A4 is a Rectangle object with a specific width and height. You can use it like this:

Document document = new Document(PageSize.A4);

为什么要创建另一个矩形? new Rectangle(PageSize.A4.getWidth(),PageSize.A4.getHeight())相当于 PageSize.A4 ,那你为什么不只是使用 PageSize.A4

Why are you creating yet another rectangle? new Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight()) is the equivalent of PageSize.A4, so why aren't you just using PageSize.A4?

为什么要定义一个艺术盒?

Why are you defining an art box?

writer.setBoxSize(art,new Rectangle(36,54,555,791));

writer.setBoxSize("art", new Rectangle(36, 54, 555, 791));

通常很少有理由创建一个艺术盒。为什么不删除这一行?

There is usually very little reason to create an Art box. Why not remove this line?

现在问题的本质是:

第一个反问题:

您似乎正在创建一个以后要填写的表单。如果此表单供私人使用,为什么要加密表单?为什么不在填写过程中对其进行加密?这不会简化事情吗?

It seems that you are creating a form that you later want to fill out. If this form is meant for private use, why are you encrypting the form? Why not encrypt it during the process of filling it out? Wouldn't that simplify things?

您正在做的事情的简短描述:

假设您确实要加密模板。在这种情况下,您知道所有者密码:

Suppose that you really want to encrypt the template. In that case, you know the owner password:

writer.setEncryption("Vibhu".getBytes(), "Vibhu@123456789".getBytes(),
    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

您还试过:

writer.setEncryption("".getBytes(), "Vibhu@123456789".getBytes(),
    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

您声称​​将OWNER-PASSWORD设置为空字符串。那是不正确!您使用用户密码的空字符串用户密码是最终用户(想要阅读文档的人)在打开文档时需要输入的密码。 所有者密码是由创建文档的人员引入的用户未知的密码。当他想要更改文档中的某些内容时,此人将使用该密码,例如:当他想填写表单时。​​

You claim that you set the OWNER-PASSWORD as an empty string. That is not true! You are using an empty String for the user password. The user password is the password that an end user (a human being who want to read the document) needs to enter when he wants to open the document. The owner password is the password unknown to the user that is introduced by the person creating the document. This person will use that password when he wants to change something in the document, for instance: when he wants to fill out a form.

第二个反问题:

为什么使用这一行:

PdfReader.unethicalreading = true;

此行是人们在不知道所有者密码时使用的行。但是,你知道所有者密码:它是Vibhu @ 123456789

This line is what people use when they don't know the owner password. However, you do know the owner password: it's "Vibhu@123456789".

避免BadPasswordException的简单解决方案:

由于您知道所有者密码,因此您应该在阅读文件。我在回答如何解答问题时解释了这一点。我可以使用所有者密码解密PDF文档吗?

Since you know the owner password, you should use it when reading the document. This is explained in my answer to the question How can I decrypt a PDF document with the owner password?

您应该更新创建阅读器的行这样的对象:

You should update the line where you create the reader object like this:

PdfReader reader = new PdfReader(src, "Vibhu@123456789".getBytes());

现在你将不再获得 BadPasswordException 你可以安全地填写你的表格。

Now you will no longer get a BadPasswordException and you can safely fill out your form.

这篇关于BadPasswordException:用户密码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆