Friday, 21 February 2014

How to detect encrypted document using C# ,DotNet, Aspose

Detect encrypted document using C#, DotNet, Aspose  

#region "Description"
        /*
          Microsoft office documents (.doc, .docx, .xls, .xlsx, .pdf, .ppt, .pptx) can be verified for encryption using Aspose dlls such as Aspose word, Aspose.cells, Aspose.slides, Aspose.pdf etc. as given in blog     
Using method such as isPasswordProtectedDoc() and isCorruptDoc(), we can detect encryption of Microsoft documents
  
         */
#endregion




 #region Methods

//Include Aspose before using below given methods
using Aspose.Words; //For Word
using Aspose.Cells; //For Excel
using Aspose.Pdf; //For PDf
using Aspose.Slides; //For PPT


         /// <summary>
        /// Given Function takes input as Document stream and returns true or false value based on whether document is password protected or not
        /// </summary>
        /// <param name="fileDataStream"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public bool isPasswordProtectedDoc(Stream fileDataStream, string extension)
        {
            bool isPasswordProtected = false;           
            try
            {
                if (extension == ".doc" || extension == ".docx")
                {
                    try
                    {
                        Aspose.Words.FileFormatInfo info = Aspose.Words.FileFormatUtil.DetectFileFormat(fileDataStream);
                        if (info.IsEncrypted)
                            isPasswordProtected = true;
                        Aspose.Words.Document doccurrept = new Aspose.Words.Document(fileDataStream);
                    }
                    catch (Exception exc)
                    {
                        if (exc.Message == "The document password is incorrect.")
                        {
                            isPasswordProtected = true;
                        }                    
                    }
                }
                else if (extension == ".xls" || extension == ".xlsx")
                {
                    try
                    {
                        Aspose.Cells.Workbook objworkpass = new Aspose.Cells.Workbook(fileDataStream);
                    }
                    catch (Aspose.Cells.CellsException excell)
                    {
                        if (excell.Code.ToString() == "IncorrectPassword")
                        {
                            isPasswordProtected = true;
                        }
                    }
                    catch (Exception exc)
                    {
                        //objfileprops.iscorrupted = true;
                    }
                }
                else if (extension == ".ppt")
                {
                    try
                    {
                        Aspose.Slides.Presentation doccurrept = new Aspose.Slides.Presentation(fileDataStream);                                              
                    }
                    catch (Aspose.Slides.InvalidPasswordException exc)
                    {
                        isPasswordProtected = true;
                    }
                    catch (Exception exc)
                    { 
                       // objfileprops.iscorrupted = true;
                    }
                }
                else if (extension == ".pptx")
                {
                    try
                    {
                        Aspose.Slides.Pptx.PresentationEx doccurrept = new Aspose.Slides.Pptx.PresentationEx(fileDataStream);
                    }
                    catch (Aspose.Slides.InvalidPasswordException exc)
                    {
                        isPasswordProtected = true;
                    }
                    catch (Exception exc)
                    {  
                       // objfileprops.iscorrupted = true;
                    }
                }
                else if (extension == ".pdf")
                {
                    try
                    {
                        Aspose.Pdf.Facades.PdfFileInfo fileInfo = new Aspose.Pdf.Facades.PdfFileInfo(fileDataStream);
                        if (fileInfo.IsEncrypted)
                            isPasswordProtected = true;
                        Aspose.Pdf.Document doccurrept = new Aspose.Pdf.Document(fileDataStream);
                    }
                    catch (Aspose.Pdf.Exceptions.InvalidPasswordException)
                    {
                        isPasswordProtected = true;
                    }
                    catch (Exception exc)
                    {
                        //objfileprops.iscorrupted = true;
                    }
                } 
            }
            catch(Exception ex)
            {
            }
            return isPasswordProtected;
        }

        /// <summary>
        /// Given Function takes input as Document stream and returns true or false value based on whether document is corrupt or not
        /// </summary>
        /// <param name="fileDataStream"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public bool isCorruptDoc(Stream fileDataStream, string extension)
        {
            bool isCorrupted = false;
            try
            {
                if (extension == ".doc" || extension == ".docx")
                {
                    try
                    {
                         Aspose.Words.FileFormatInfo info = Aspose.Words.FileFormatUtil.DetectFileFormat(fileDataStream);                      
                        Aspose.Words.Document doccurrept = new Aspose.Words.Document(fileDataStream);
                    }
                    catch (Exception exc)
                    {
                        if (exc.Message != "The document password is incorrect.")
                        {                        
                            isCorrupted = true;
                        }
                    }
                }
                else if (extension == ".xls" || extension == ".xlsx")
                {
                    try
                    {
                        Aspose.Cells.Workbook objworkpass = new Aspose.Cells.Workbook(fileDataStream);
                    }
                    catch (Aspose.Cells.CellsException excell)
                    {
                        if (excell.Code.ToString() != "IncorrectPassword")
                        {
                            isCorrupted = true;
                        }
                    }
                    catch (Exception exc)
                    {
                        isCorrupted = true;
                    }
                }
                else if (extension == ".ppt")
                {
                    try
                    {
                        Aspose.Slides.Presentation doccurrept = new Aspose.Slides.Presentation(fileDataStream);
                    }
                    catch (Aspose.Slides.InvalidPasswordException exc)
                    {
                        //Do nothing
                    }
                    catch (Exception exc)
                    {
                        isCorrupted = true;
                    }
                }
                else if (extension == ".pptx")
                {
                    try
                    {
                        Aspose.Slides.Pptx.PresentationEx doccurrept = new Aspose.Slides.Pptx.PresentationEx(fileDataStream);
                    }
                    catch (Aspose.Slides.InvalidPasswordException exc)
                    {
                        //Do nothing
                    }
                    catch (Exception exc)
                    {
                        isCorrupted = true;
                    }
                }
                else if (extension == ".pdf")
                {
                    try
                    {
                        Aspose.Pdf.Facades.PdfFileInfo fileInfo = new Aspose.Pdf.Facades.PdfFileInfo(fileDataStream);                       
                        Aspose.Pdf.Document doccurrept = new Aspose.Pdf.Document(fileDataStream);
                    }
                    catch (Aspose.Pdf.Exceptions.InvalidPasswordException)
                    {
                        //Do nothing
                    }
                    catch (Exception exc)
                    {
                        isCorrupted = true;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return isCorrupted;
        }
#endregion
      


No comments:

Post a Comment