Bug之旅:原来 Excel 文件加密后文件格式还不一样

是近在学习 Go 操作 Excel 的库 excelize 时,在官方文档工作簿一节列举了在读写电子表格时的选项如下:

go 复制代码
type Options struct {
    MaxCalcIterations uint
    Password          string
    RawCellValue      bool
    UnzipSizeLimit    int64
    UnzipXMLSizeLimit int64
    ShortDatePattern  string
    LongDatePattern   string
    LongTimePattern   string
    CultureInfo       CultureName
}

很多选项不知道有什么作用,唯一能立马明白的就是 Password 这个选项,于是创建了一个新的电子表格并设置了密码保存,然后在 Excelize 中使用如下代码打开:

go 复制代码
package main

import (
	"log"

	"github.com/xuri/excelize/v2"
)

func main() {
	f, err := excelize.OpenFile("E:\\ExcelDemo\\Book1.xlsx")

	if err != nil {
		log.Fatal(err)
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			log.Fatal(err)
		}
	}()
}

运行后得到了一个让人诧异的错误:

python 复制代码
2024/04/17 14:45:30 zip: not a valid zip file

很奇怪,不应该提示这个文件已经加密,请提供密码吗?然后我把设置的密码加上后,一切又正常了.

go 复制代码
f, err := excelize.OpenFile("E:\\ExcelDemo\\Book1.xlsx", excelize.Options{
	Password: "123456",
})

带着疑问,我去官方 Issue 中使用 "zip: not a valid zip file" 进行搜索,发现这个相关的问题还真不少,但是粗略查看了几个后,也没有和我这个类似的,也没有人提供什么好的解决方案,只看到的有的说复制一份,然后再打开可以,这似乎不是我想要的,于是我就用蹩脚英文提了一个 Issue,希望得到作者的帮助。

第二天上班发现 Issue 已经被关了,xuri 大佬给出了产生这个问题的原因,但是建议自己去解决:

Thanks for your issue. The unencrypted workbook is a compressed file with the ZIP format, but the encrypted workbook is a CFB (OLE) file, which is different from the ZIP format. You will get that error message not only after opening an encrypted workbook without specifying the correct password but also after opening any file format that isn't supported by this library. So I think this error message is expected. Note that, you can roughly determine if a file is in a CFB format by this identifier. I'll close this issue. If you have any questions, please let me know, and you can reopen this anytime.

加密之后怎么就变味了,于是在网上看了一下 CFB 相关的信息,比如:

扫了一下有点复杂,没必要去了解,于是又顺着 xuri 在回复中提及源码中的 identifier:

go 复制代码
const oleIdentifier = []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}

然后立马下载了一个查看文件 16 进制的软件 WinHex,将一个加密和一个未加密的文件同时打开,其头部对比如下:

图中红色圈中的不就刚好是 oleIdentifier 吗,于是又根据文章:

写了一个判断的方法 isOleExcel(f io.ReadSeeker) bool

go 复制代码
func isOleExcel(f io.ReadSeeker) bool {
	oleIdentifier := []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}
	buf := make([]byte, len(oleIdentifier))

	_, err := f.Read(buf)

	if err != nil {
		return false
	}

	f.Seek(0, io.SeekStart)

	return bytes.Compare(buf, oleIdentifier) == 0
}

上面的代码中,我们根据魔术字符串的长度读取了文件流的前 8 个字节到缓冲区,然后使用 f.Seek 方法将读写偏移重置为原始位置,不这样做的话,原始文件前 8 个字节会因为被读取了而变成空数据。然后使用 bytes.Compare 方法对比字节是否一样。

我们现在有了判断文件是否加密(如何有其它方式导致其变成 CFB 文件,提示信息就不一定正确了,所以官方不作统一处理,是权重考虑过了),如何用户提供了密码,Excelize 在验证时如果发现不正确会抛出 ErrWorkbookPassword 错误,下面是完整的实现参考:

go 复制代码
package main

import (
	"bytes"
	"io"
	"log"
	"os"

	"github.com/xuri/excelize/v2"
)

func main() {
	filePath := "E:/ExcelDemo/Book1.xlsx"

	bs, _ := os.Open(filePath)
	defer bs.Close()

	// f, err := excelize.OpenFile(filePath)
	f, err := excelize.OpenFile(filePath, excelize.Options{
		Password: "123456",
	})

	if err != nil {
		if err == excelize.ErrWorkbookPassword {
			// 也可以直接,使用 `log.Fatal(err)` 显示英文描述
			log.Fatal("Excel文件密码错误")
		} else {
			if isOleExcel(bs) {
				log.Fatal("文件已加密,请先解密后再操作")
			} else {
				log.Fatal(err)
			}
		}
		return
	}

	defer func() {
		if err := f.Close(); err != nil {
			log.Fatal(err)
		}
	}()
}

func isOleExcel(f io.ReadSeeker) bool {
	oleIdentifier := []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}
	buf := make([]byte, len(oleIdentifier))

	_, err := f.Read(buf)

	if err != nil {
		return false
	}

	f.Seek(0, io.SeekStart)

	return bytes.Compare(buf, oleIdentifier) == 0
}
相关推荐
王中阳Go1 小时前
12 Go Eino AI应用开发实战 | 消息队列架构
人工智能·后端·go
香吧香1 小时前
go项目使用go build 与 MakeFile 构建项目
go
代码扳手4 小时前
Go 微服务数据库实现全解析:读写分离、缓存防护与生产级优化实战
数据库·后端·go
王中阳Go5 小时前
我辅导400+学员拿Go Offer后发现:突破年薪50W,常离不开这10个实战技巧
后端·面试·go
梦幻通灵10 小时前
Excel序列生成的4种方案实战
excel
黄昏单车11 小时前
golang语言基础到进阶学习笔记
笔记·golang·go
王中阳Go1 天前
15 Go Eino AI应用开发实战 | 性能优化
后端·面试·go
王中阳Go1 天前
09 Go Eino AI应用开发实战 | Hertz Web 框架搭建
人工智能·后端·go
Way2top1 天前
Go语言动手写Web框架 - Gee第二天 上下文Context
go
2501_930707781 天前
使用C#代码将 Excel 转换为 ODS,或将 ODS 转换为 Excel
excel