Differentiate encrypted pptx from encrypted docx and encrypted xlsx

Hello Aspose.
I want to ask if Aspose slides can differentiate encrypted pptx from encrypted docx and encrypted xlsx?
My Aspose version: 18.4.0.0. C#
My use case: user submits encrypted file which can be pptx, xlsx and docx. This file has “tmp” extension. I want to detect the correct filetype, which is docx or xlsx or pptx. User dont submit the file’s password.
My code is
IPresentationInfo info = PresentationFactory.Instance.GetPresentationInfo(inputFile);
// docx and xlsx is detected as info.LoadFormat = pptx.

Thanks you

@truongminhlong,

I have observed your comments. Can you please share source file so that we may further investigate to help you out.

sample.zip (54.9 KB)
@Adnan.Ahmad
Thanks you for your response
I attached sample.zip which contains test.docx and test.xlsx. These files have password “test” and are detected as pptx.
Thanks

@truongminhlong,

I have observed your requirements and like to share that if the document is encrypted (no matter XLSX, DOCX or PPTX) and you load that using Aspose.Slides without providing any password, Aspose.Slides will throw InvalidPasswordException in that case. You can try load the document with password protection without providing valid password and InvalidPasswordException will be thrown that you need to catch and handle. Inside catch block you need to again load the document with valid password and Aspose.Slides will throw PptxUnsupportedFormatException on loading a password protected XLSX or DOCX with valid password provided. This way you can handle loading the valid file format supported files. Please try using following sample code to serve the purpose.

    public static void DetectFileFormat()
    {

        try
        {
            String path = @"C:\Aspose Data\sampleFiles\";
    
            Presentation pres = new Presentation(path + "test.docx");
        }
        catch (InvalidPasswordException ex)
        {
            String s = ex.StackTrace;
            try
            {
                String path = @"C:\Aspose Data\sampleFiles\";

                Aspose.Slides.LoadOptions opts = new Aspose.Slides.LoadOptions();
                opts.Password = "test";

                Presentation pres = new Presentation(path + "test.docx", opts);
            }

            catch (PptxUnsupportedFormatException e)
            {
                String ssd = e.StackTrace;
            }
            catch (Exception exx)
            {
                String ss = exx.StackTrace;
            }
        }
        catch (PptxUnsupportedFormatException e)
        {
            String ssd = e.StackTrace;
        }
    }

@mudassir.fayyaz
Thanks you for your response
That’s the way i’m using now but it’s slower than using GetPresentationInfo(inputFile) in multithreading.
Is there any ways to detect encrypted file type without opening the file?

@truongminhlong,

I have observed your comments. I like to inform that you can open pptx file as zip archive and check it for a files, which defines a presentation as encrypted. Please check below sample code.

ZipArchive pres = ZipFile.Open(inputFileName, ZipArchiveMode.Read);
foreach (ZipArchiveEntry zipArchiveEntry in pres.Entries)
{
if (zipArchiveEntry.Name == “EncriptedPackage”)
{
Presentation_IS_ENCRYPTED = true;
break;
}
}

Thanks you. My dot net framework is 4.0 and I cannot update it due to our policy
ZipArchive is not supported in dot net framework 4.0
More over, both encrypted docx and encrypted xlsx have EntryName of “EncryptedPackage”. There for, we cannot use EncryptedPackage to differ them from encrypted pptx.

@truongminhlong,

I have observed your comments and like to share tha the solution that I have proposed is simply using Aspose.Slides API to load the files and based on handles exception it is making decision about a valid file being loaded. Have you tried executing the sample code in your application. If Aspose.Slides API is working in your environment then proposed solution may also work.

@mudassir.fayyaz
Yes, I tried your sample code and it works as you proposed. It did solve my problem but I’m concerned about the speed in multi-threading. Each thread must open file 2 times.
That’s way I prefer another faster way to detect file such as GetPresentationInfo(). GetPresentationInfo() works with non-encrypted file and faster than opening file while it cannot be used in encrypted file.

@truongminhlong,

I regret to share that reading and encrypted file is different so it information may not be accessible via GetPresentationInfo(). In case of encrypted file, one need to provide a valid password to access the document information and subsequently verifying its type information.

Thanks you for your support.
I’m good now.

@truongminhlong,

Thank you for your feedback.