如何解决确定 XLS 文件是否受“保护工作簿结构”选项的密码保护
此 StackOverflow 问题 (Detecting a password-protected document) 中得票数最多的答案描述了一种确定 Microsoft Office 文件是否受密码保护的良好而有效的方法。 但是,它会将完全加密的 Excel 文件和具有“保护工作簿结构”的 Excel 文件识别为受密码保护。
如果我从“ScanForPassword”方法中理解正确,它会根据文件中 FILEPASS 部分的存在来确定密码保护(filePassMagic 字节,值为 0x002F/47)。
// check for Excel header
const string workbookName = "W\0o\0r\0k\0b\0o\0o\0k" + afterNamePadding;
headerOffset = bufferString.IndexOf(workbookName,StringComparison.InvariantCulture);
if (headerOffset >= 0)
{
sectionId = BitConverter.ToInt32(buffer,headerOffset + sectionIdOffset);
int sectionOffset = coBaseOffset + sectionId * sectionSize;
const int streamScanSize = 0x100;
if (sectionOffset < 0 || sectionOffset + streamScanSize > stream.Length)
return false; // invalid document
var workbookStream = new byte[streamScanSize];
stream.Seek(sectionOffset,SeekOrigin.Begin);
ReadFromStream(stream,workbookStream);
short record = BitConverter.ToInt16(workbookStream,0);
short recordSize = BitConverter.ToInt16(workbookStream,sizeof(short));
const short bofMagic = 0x0809;
const short eofMagic = 0x000A;
const short filePassMagic = 0x002F;
if (record != bofMagic)
return false; // invalid BOF
// scan for FILEPASS record until the end of the buffer
int offset = sizeof(short) * 2 + recordSize;
int recordsLeft = 16; // simple infinite loop check just in case
do
{
record = BitConverter.ToInt16(workbookStream,offset);
if (record == filePassMagic)
return true;
recordSize = BitConverter.ToInt16(workbookStream,sizeof(short) + offset);
offset += sizeof(short) * 2 + recordSize;
recordsLeft--;
} while (record != eofMagic && recordsLeft > 0);
}
是否可以将使用“保护工作簿结构”方法加密的文件分开而不将其报告为受密码保护?我试图检查 FILEPASS 部分中的下一个值是否有意义。它通常是 54 或 200,但我已经看到完全密码保护和结构保护文件的 54 值。 我也找不到有用的文档(https://www.yumpu.com/en/document/read/46184961/openofficeorgs-documentation-of-the-microsoft-excel-file-format- 中的第 168 页)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。