简体   繁体   English

检查 Excel 文件是否在 .NET Core 中受密码保护

[英]Check whether Excel file is password protected in .NET Core

Tech:技术:

C# C#

ASP.NET Core 2.0 ASP.NET 核心 2.0

Excel (Mostly OpenXml, but occasional legacy document) Excel(主要是 OpenXml,但偶尔会有遗留文档)

Currently using ExcelDataReader to read the files.当前使用 ExcelDataReader 读取文件。

Scenario:设想:

I need to store some files on a server and access them later to do work.我需要在服务器上存储一些文件并稍后访问它们以完成工作。 Users upload the files through a web API and make calls requesting work to be done in the same way.用户通过 Web API 上传文件并调用请求以相同方式完成的工作。

Some of the users password-protect their Excel files, and it is a requirement that these files remain password-protected while on the server.一些用户使用密码保护他们的 Excel 文件,并且要求这些文件在服务器上时保持密码保护。

Problem:问题:

I need to detect whether the document is password-protected so I can use the appropriate configuration to access it.我需要检测文档是否受密码保护,以便我可以使用适当的配置来访问它。

I've googled this a good bit, but it mostly comes up with Workbook.HasPassword (I think this is part of Excel.Interop, which I'd like to avoid adding).我在谷歌上搜索了很多,但主要是 Workbook.HasPassword (我认为这是 Excel.Interop 的一部分,我想避免添加)。 I also found this link to a similar question on Office documents, in which the solution was a long checklist against various header data via file stream.我还发现此链接指向有关 Office 文档的类似问题,其中解决方案是通过文件流针对各种标头数据的长清单。

Ideally, I'd like a Core-friendly solution that does not require me to open the file.理想情况下,我想要一个不需要我打开文件的 Core 友好的解决方案。 I don't work with documents that often so I'm at a loss as to where to look next.我不经常处理文档,所以我不知道下一步该往哪里看。

Any suggestions?有什么建议么?

Since you are already using ExcelDataReader, open the file in a try-catch clause and handle InvalidPasswordException :由于您已经在使用 ExcelDataReader,请在 try-catch 子句中打开文件并处理InvalidPasswordException

bool ExcelFileRequiresPassword(Stream stream) {
    try {
        using (ExcelReaderFactory.CreateReader(stream)) { }
    } catch (InvalidPasswordException) {
        return true;
    }
    return false;
}

This is not too bad performance wise.这在性能方面还算不错。 Behind the scenes it checks only the relevant headers in the file, and handles all the gory BIFF, ZIP and versioning details.在幕后,它只检查文件中的相关标题,并处理所有血腥的 BIFF、ZIP 和版本控制细节。 No data will be read if there is no password.如果没有密码,则不会读取任何数据。

You aren't going to find out whether the file is password protected without opening the file.在不打开文件的情况下,您不会发现文件是否受密码保护。 That information is in the file (as far as I know);该信息在文件中(据我所知); it's not exposed in the file system.它没有在文件系统中公开。

You should be able to get at it quickly using the OpenXml SDK.您应该能够使用 OpenXml SDK 快速了解它。 Create a non-protected file.创建一个不受保护的文件。 Copy it.复制它。 Open the copy and set a password and save it.打开副本并设置密码并保存。 Then use the openXml "Productivity Tool" to diff the two files ("Compare Files", up on the tool bar).然后使用 openXml“生产力工具”来比较这两个文件(“比较文件”,在工具栏上)。 It should zero in quickly on where to look.它应该快速归零看哪里。

This is an old post as I add to it, but recently I wanted to establish if spreadsheets were password protected in C# without using an external library I ended up creating the following code.这是我添加的旧帖子,但最近我想确定电子表格是否在 C# 中受密码保护,而不使用外部库,我最终创建了以下代码。

There may be cases and file formats that I have not found, but this will I hope put you on the right track.可能有些案例和文件格式我还没有找到,但我希望这能让你走上正轨。

NOTE: The number of spaces in the comparison text, is important so be careful when cutting and pasting.注意:比较文本中的空格数很重要,因此在剪切和粘贴时要小心。

static bool IsExcelPasswordProtected(string strSource)
{
    bool blResult = false;

    if (File.Exists(strSource))
    {
        char[] chBuffer = new char[4096];   // The character strings usually occur within the first 2K in my testing, but just in case
        TextReader trReader = new StreamReader(strSource, Encoding.UTF8, true);

        // Read the buffer
        trReader.ReadBlock(chBuffer, 0, chBuffer.Length);
        trReader.Close();

        // Remove non-printable and unicode characters, we're only interested in ASCII character set
        for (int i = 0; i < chBuffer.Length; i++)
        {
            if ((chBuffer[i] < ' ') || (chBuffer[i] > '~')) chBuffer[i] = ' ';
        }

        string strBuffer = new string(chBuffer);

        // .xls format files, version 97 to 2003 contains this text when password protected
        if (strBuffer.Contains("M i c r o s o f t   E n h a n c e d   C r y p t o g r a p h i c   P r o v i d e r"))
        {
            blResult = true;
        }
        // .xlsx format files contain this text when password protected
        else if (strBuffer.Contains("E n c r y p t e d P a c k a g e"))
        {
            blResult = true;
        }
        // .xlsx format files contain this text when not password protected
        else if (strBuffer.Contains("[Content_Types]"))
        {
            blResult = false;
        }
        // .xlsx format files contain this text when not password protected
        else
        {
            blResult = false;
        }
    }
    else
    {
        // File not found...deal with as you wish
    }

    return (blResult);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中检查Excel文件是否受写保护? - How to check whether a excel file is write protected or not in C#? 如何检查 Excel 工作簿或工作表是否受密码保护? - how to check Excel Workbook or sheet is password protected or not? 未指定密码时检查工作表是否受保护 - Check whether worksheet is protected when no password has been specified 检查NSF文件是否受密码保护,是否使用C#。建议某些dll在未安装Domino服务器的情况下访问NSF文件 - Check whether NSF File is Password Protected or Not using C#.Suggest some dll to access NSF file without Domino Server installed 如何创建受密码保护的 Excel 文件? - How to create a password protected Excel file? 使用EPPlus ExcelPackage检查受密码保护的文件 - Check for password protected file with EPPlus ExcelPackage 使用C#确定SQL备份文件是否受密码保护 - Determining with C# Whether a SQL Backup File is Password Protected 检查共享文件夹是否在vb.net中受密码保护 - Check if a shared folder is password protected in vb.net 受密码保护的安装程序.net - password protected installer .net 如何确定使用OLEDB在C#中Excel工作簿是否受密码保护? - How to determine whether excel workbook is password protected or not in C# using OLEDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM