我不明白为什么 PdfReader.ComputeUserPassword() 返回 null
I don't understand why PdfReader.ComputeUserPassword() is returning null
我正在尝试从 PDF 文件中接收用户密码。出于测试目的,我同时获得了主人密码和用户密码。现在我在一个参数中传递主人的密码,并用它来创建一个新的 iTextSharp.text.pdf.PdfReader
实例,它工作正常。然后我输入一个 if 子句,它应该 return PDF 是否以完全权限打开。在这个 if 子句中,我通过调用 iTextSharp.text.pdf.PdfReader.ComputeUserPassword()
which returns null
.
请求用户密码
我的整个代码如下所示(GetByteAr(string s)
returns 密码转换为字节数组):
public static bool IsPasswordProtectedOwner(string pdf, string ownerPw)
{
try
{
var reader = new PdfReader(pdf, GetByteAr(ownerPw));
if (reader.IsOpenedWithFullPermissions)
{
Console.WriteLine("opened with full permissions");
string pw = String.Empty;
var computedPassword = reader.ComputeUserPassword();
foreach (byte b in computedPassword)
pw += Char.ConvertFromUtf32(b);
}
else
{
Console.WriteLine("not opened with full permissions");
}
}
catch (Exception e) when (e is NullReferenceException || e is BadPasswordException)
{
Console.WriteLine(e);
}
return true;
}
我的输出如下所示:
opened with full permissions
System.NullReferenceException: Object reference not set to an instance of an object.
at PDFsV2.PDFInteractor.IsPasswordProtectedOwner(String pdf, String ownerPw)
in C:\Users\user\source\repos\PDFsV2\PDFsV2\PDFInteractor.cs:line 57
你能帮我理解为什么 computedPassword
是 null
吗? 为什么 ComputeUserPassword
returning null
?
编辑,这就是它return为空的原因:
https://api.itextpdf.com/iText5/5.5.13/
public byte[] computeUserPassword()
Computes user password if standard encryption handler is used with
Standard40, Standard128 or AES128 encryption algorithm.
Returns:
user password, or null if not a standard encryption handler was used, if standard encryption handler was used with AES256 encryption
algorithm, or if ownerPasswordUsed wasn't use to open the document.
https://github.com/kusl/itextsharp/blob/master/tags/iTextSharp_5_4_5/src/core/iTextSharp/text/pdf/PdfReader.cs#L3849 将 ComputeUserPassword
的实现显示为:
public byte[] ComputeUserPassword() {
if (!encrypted || !ownerPasswordUsed) return null;
return decrypt.ComputeUserPassword(password);
}
根据该代码(第二行),ComputeUserPassword
可能是 null
。因此,您需要在代码中满足这一点(即在 foreach
之前检查它是否 null
)。
您的情况可能是因为:
ownerPasswordUsed = decrypt.ReadKey(enc, password);
正在返回 false
。这可能表明您输入了错误的密码值。
同样,the docs状态:
user password, or null if not a standard encryption handler was used
or if ownerPasswordUsed wasn't use to open the document.
我正在尝试从 PDF 文件中接收用户密码。出于测试目的,我同时获得了主人密码和用户密码。现在我在一个参数中传递主人的密码,并用它来创建一个新的 iTextSharp.text.pdf.PdfReader
实例,它工作正常。然后我输入一个 if 子句,它应该 return PDF 是否以完全权限打开。在这个 if 子句中,我通过调用 iTextSharp.text.pdf.PdfReader.ComputeUserPassword()
which returns null
.
我的整个代码如下所示(GetByteAr(string s)
returns 密码转换为字节数组):
public static bool IsPasswordProtectedOwner(string pdf, string ownerPw)
{
try
{
var reader = new PdfReader(pdf, GetByteAr(ownerPw));
if (reader.IsOpenedWithFullPermissions)
{
Console.WriteLine("opened with full permissions");
string pw = String.Empty;
var computedPassword = reader.ComputeUserPassword();
foreach (byte b in computedPassword)
pw += Char.ConvertFromUtf32(b);
}
else
{
Console.WriteLine("not opened with full permissions");
}
}
catch (Exception e) when (e is NullReferenceException || e is BadPasswordException)
{
Console.WriteLine(e);
}
return true;
}
我的输出如下所示:
opened with full permissions
System.NullReferenceException: Object reference not set to an instance of an object.
at PDFsV2.PDFInteractor.IsPasswordProtectedOwner(String pdf, String ownerPw)
in C:\Users\user\source\repos\PDFsV2\PDFsV2\PDFInteractor.cs:line 57
你能帮我理解为什么 computedPassword
是 null
吗? 为什么 ComputeUserPassword
returning null
?
编辑,这就是它return为空的原因:
https://api.itextpdf.com/iText5/5.5.13/
public byte[] computeUserPassword()
Computes user password if standard encryption handler is used with Standard40, Standard128 or AES128 encryption algorithm.
Returns: user password, or null if not a standard encryption handler was used, if standard encryption handler was used with AES256 encryption algorithm, or if ownerPasswordUsed wasn't use to open the document.
https://github.com/kusl/itextsharp/blob/master/tags/iTextSharp_5_4_5/src/core/iTextSharp/text/pdf/PdfReader.cs#L3849 将 ComputeUserPassword
的实现显示为:
public byte[] ComputeUserPassword() {
if (!encrypted || !ownerPasswordUsed) return null;
return decrypt.ComputeUserPassword(password);
}
根据该代码(第二行),ComputeUserPassword
可能是 null
。因此,您需要在代码中满足这一点(即在 foreach
之前检查它是否 null
)。
您的情况可能是因为:
ownerPasswordUsed = decrypt.ReadKey(enc, password);
正在返回 false
。这可能表明您输入了错误的密码值。
同样,the docs状态:
user password, or null if not a standard encryption handler was used or if ownerPasswordUsed wasn't use to open the document.