【问题标题】:How to decrypt a pdf file by supplying password of the file as argument using c#?如何通过使用 c# 提供文件的密码作为参数来解密 pdf 文件?
【发布时间】:2012-06-13 11:44:37
【问题描述】:

我使用以下代码生成了一个带有密码保护的 pdf 文件:

using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        PdfReader reader = new PdfReader(input);
        PdfEncryptor.Encrypt(reader, output, true, strDob, "secret", PdfWriter.ALLOW_SCREENREADERS);
    }
}

我想根据我的某些条件通过代码删除使用上述代码生成的PDF文件的密码。

【问题讨论】:

  • 您使用什么库来编写 PDF?它的手册是否提到有关从现有文件中删除密码的任何内容?读取受密码保护的文件并将数据写入没有密码的新文件不是一个选项吗?
  • 嗨,感谢您的回复。我已经提到了上面的链接。但是 PdfReader.unethicalreading 没有进入我的智能。而且我没有使用任何 AES 或 Rijndael 算法来保护密码。我也尝试过 Rijndael Decrypt() 方法。但没有成功:(
  • 我用过itextsharp dll。和以下命名空间生成受密码保护的 pdf 文件。使用 iTextSharp.text;使用 iTextSharp.text.pdf;
  • 您需要最新版本。最近添加了不道德的阅读。

标签: c# itextsharp


【解决方案1】:
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "test_dec.pdf");//will be created automatically
//You must provide owner password but not the user password .
private void DecryptFile(string inputFile, string outputFile)
{

    string password = @"secret"; // Your Key Here           
    try
    {
        PdfReader reader = new PdfReader(inputFile, new System.Text.ASCIIEncoding().GetBytes(password));

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfStamper stamper = new PdfStamper(reader, memoryStream);
                stamper.Close();
                reader.Close();
                File.WriteAllBytes(outputFile, memoryStream.ToArray());
            }

    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message);
    }
}

【讨论】:

  • //我终于找到了解决方案 :) ... 使用以下代码删除对 pdf 文件的密码保护。 //Nasmspaces using iTextSharp.text;// 你必须使用 iTextSharp.text.pdf 添加 itextsharp.dll 作为参考;使用 System.IO;使用 System.Text;
猜你喜欢
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2017-11-17
  • 2012-12-08
相关资源
最近更新 更多