没有密码的Itextsharp加密无法正常工作

问题描述 投票:0回答:1

我试图使用itextsharp writer.setEncryption方法,没有密码加密文件,但它设置一些随机pdf的密码,即使我没有通过任何加密它。

pdf.SetEncryption(Nothing, Nothing, PdfWriter.AllowScreenReaders, PdfWriter.STRENGTH40BITS)
vb.net pdf encryption itext
1个回答
1
投票

它运作正常。

请查看您使用的方法的文档:

/** Sets the encryption options for this document. The userPassword and the
*  ownerPassword can be null or have zero length. In this case the ownerPassword
*  is replaced by a random string. The open permissions for the document can be
*  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
*  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
*  The permissions can be combined by ORing them.
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
* @throws DocumentException if the document is already open
*/
virtual public void SetEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, bool strength128Bits)

因此,如果您没有为所有者密码提供值,则记录的行为是使用随机字符串,与您观察到的完全相同。


显然,您尝试将PDF的权限设置为仅PdfWriter.AllowScreenReaders,但设置此权限选择仅适用于加密文件,而对于加密,需要非空的所有者密码,因此iText会为您选择一个。

另一方面,空用户密码是可能的,因为在加密和解密期间使用它的过程中,“默认密码字符串”无论如何都附加到用户密码值。

Addendum

回答评论中的问题......

What does "is replaced by a random string" mean exactly? And in what format would that password be?

if (ownerPassword == null || ownerPassword.Length == 0)
    ownerPassword = DigestAlgorithms.Digest("MD5", CreateDocumentId());

PdfEncryption方法SetupAllKeys

其中CreateDocumentId定义如下:

public static byte[] CreateDocumentId() {
    long time = DateTime.Now.Ticks + Environment.TickCount;
    long mem = GC.GetTotalMemory(false);
    String s = time + "+" + mem + "+" + (seq++);
    byte[] b = Encoding.ASCII.GetBytes(s);
    return DigestAlgorithms.Digest("MD5", b);
}

Can you still retrieve the password somehow?

密码未显式存储在任何地方以便以后检索。在这种情况下,即使用空的所有者密码进行加密的呼叫,假设呼叫者对知道密码不感兴趣。

如果这听起来很奇怪,请注意,对于文档数据的实际加密,使用的是用户密码,而不是所有者密码。 (更准确地说是从用户密码派生的值。)文档加密期间的所有者密码仅用于加密用户密码(更准确地说,是从中派生的值)并将该值存储在PDF中。

当再次打开PDF并且用户提供密码时,测试它是否可以立即用于文档数据解密(即它是用户密码),或者是否可以用于将上述值解密为用户密码然后可以解密文档(即它是所有者密码)。

在任何一种情况下,用户都可以访问PDF,但如果它只是用户密码而不是所有者密码,则PDF处理器应根据给定的权限值限制允许的操作。

因此,在此处不给予所有者密码的呼叫者被解释为对在符合PDF处理器的规范中具有完全访问PDF的任何人不感兴趣,用于限制访问用户密码就足够了。这与由于丢失密码使文档无法加密无关...

Would it use one of the indicated key strengths?

好吧,MD5返回128位散列,但由于散列数据的结构众所周知,密码远没有真正随机的128 bis值那么强。另一方面,一个八字符串通常也没有那么强......

© www.soinside.com 2019 - 2024. All rights reserved.