File shows isEncrypted after DecryptFile

I have created an app that uses Aspose.PDF to decrypt a series of files. Here is a snippet of code:

foreach(var _file in _files){
    try{
       PdfFileInfo _info = new PdfFileInfo(_file);
       var _isEncrypted = _info.IsEncrypted;
       _info.Close();
       if(_isEncrypted)
       {
           using(var secureDoc = new PdfFileSecurity())
           {
                secureDoc.BindPdf(_file);
                secureDoc.DecryptFile(_masterPassword);
                secureDoc.SetPrivilege(_privelege);  //_privelege defined earlier
                secureDoc.Save(_file);
                secureDoc.Close();
           }
        }
        else { Console.WriteLine("{0} not encrypted",_file); }
    }
    catch(Exception e)
    {
        Console.WriteLine("{0} - Fail: {1}",_file,e.Message);
    }
 }

The first time through, this code works fine. PDF’s in the directory that have no password protection already simply get a console line stating that they are not encrypted.

After running the code on all files in a given directory, I can open the files without being prompted for a password – so it would appear that the file has been successfully decrypted. However, if I run the code a second time on the same directory, I would expect to get the “not encrypted” message on all the files; however, what I get is the Fail message on all the files that were previously encrypted with a message stating “Invalid Password”.

This tells me that the PdfFileInfo.IsEncrypted is still returning true on all the files that were supposedly decrypted. This does not make sense to me.

Can someone explain what is going on? Thanks.

@rodd_harris

Thanks for contacting support.

Please note that it is not necessary that file will only be encrypted if it is password protected. A file will be encrypted if there are some DocumentPrivileges set. You may check DocumentPrivileges by following code snippet and see what privileges are set due to which the file is seemed to be encrypted.

Aspose.Pdf.Facades.PdfFileInfo info = new Aspose.Pdf.Facades.PdfFileInfo();
info.BindPdf("input.pdf");
var privilege = info.GetDocumentPrivilege();

In case your source PDF does not have any privilege set and still shows it is encrypted, please share it with us. We will test the scenario in our environment and address it accordingly.

Thanks for your reply – that definitely explains what’s happening because I am adding privileges. Is there a way to tell if a file is actually encrypted as opposed to just having priveleges set?

@rodd_harris

In case you want to check if PDF file is password protected, you may use HasOpenPassword property as follows, which will return true if a file is encrypted using password.

Aspose.Pdf.Facades.PdfFileInfo info = new Aspose.Pdf.Facades.PdfFileInfo();
info.BindPdf("input.pdf");
var hasopenend = info.HasOpenPassword;

Thanks. That is exactly what I needed.

@rodd_harris

Thanks for your kind feedback.

Please keep using our API and in case of any further query, please feel free to create a new topic in our support forums. We will be more than happy to assist you accordingly.