在先前測試中,Open XML SDK是唯一挑戰ReportViewer匯出Excel檔修改成功的程式庫,手邊的下一步需求是要將工作表(Worksheet)設為不可修改。

在Open XML SDK中,有個SheetProtection類別,將其加入xlsx的XML結構,就可向應用程式宣告該工作表允許或禁止的操作,例如: 刪除欄(deleteColumns)、重設儲存格格式(formatCells)、插入列(insertRow)... 等等。在SheetProctection設定可以指定解除鎖定的密碼雜湊值(Hash),甚至可採SHA-512再配合Salt反覆計算多次的高強度雜湊演算法,但必須強調,由於設定保護後的xlsx仍是一個可被解讀的ZIP檔,其中XML還是可能被編輯修改,即便不知密碼,有心人只需移除XML上的宣告即可移除保護,故工作表保護機制只防君子不防小人,不宜視作資安管控機制。

延伸閱讀: Overview of Protected Office Open XML Documents
Note, after implementing the document protection mechanism programmatically the document is not considered secure since the password is stored in plain text in the OOXML document structure and can fairly easily be obtained and/or removed by editing the “workbook.xml” file, under the “xl” folder (or the “document.xml” file for Word, under the “word” folder) in the ZIP package.  By comparison, a Compound File Binary file protected document is considered more secure since the password is stored in an encrypted stream in the CFB file format.

雖說不到資安防護的等級,工作表保護在一般情境下已可初步防止End-User任意更動報表數字混淆視聽,仍有一定實用性,以下是簡單程式範例:

    //OpenXML SDK
    string dst = src.Replace(Path.GetFileName(src), "sdk.xlsx");
    File.Copy(src, dst, true);
    using (var shtDoc = SpreadsheetDocument.Open(dst, true))
    {
        var sht = shtDoc.WorkbookPart.Workbook.Descendants<Sheet>().First();
        var shtPart = shtDoc.WorkbookPart.GetPartById(sht.Id) as WorksheetPart;
        //建立一個SheetProtection物件
        var proc = new SheetProtection()
        {
            Password = new HexBinaryValue("ABCD"),
            Sheet = true,
            Objects = true,
            Scenarios = true
        };
        //需安插於sheetData後方
        shtPart.Worksheet.InsertAfter<SheetProtection>(proc, 
            shtPart.Worksheet.Descendants<SheetData>().First());
        shtDoc.WorkbookPart.Workbook.Save();
        shtDoc.Close();
    }

補充說明: SheetProtection.Password的值會被當成密碼的雜湊值,故使用Excel開啟時,直接輸入ABCD是無法解密的,然而輸入什麼密碼才會產生ABCD這種雜湊值是個謎,等於沒人能在Excel中用密碼解鎖。誰都猜不出解鎖密碼符合我的應用情境,但如果想實現在Excel用密碼解鎖,MSDN論壇有篇高手寫的SheetProtection.Password雜湊演算法可由指定的密碼字串推算Password值,很值得參考。

【2012-12-28補充】新選擇: 令人驚豔的Excel程式庫 - ClosedXML


Comments

# by rogerwiga

Zet Excel's Library is what i am using.

Post a comment