private void UnprotectFile(string file) { MasterKey masterKey = new MasterKey(); try { StatusText.Text += "Removing protection from: " + file; if (masterKey.FileIsXlsx(file)) { if (masterKey.XlsxIsProtected(file)) { string protectedParts = ""; if (masterKey.XlsxIsWorkbookProtected(file) && masterKey.XlsxIsWorksheetProtected(file)) { protectedParts = "[Workbook,Worksheet]"; } else if (masterKey.XlsxIsWorkbookProtected(file)) { protectedParts = "[Workbook]"; } else if (masterKey.XlsxIsWorksheetProtected(file)) { protectedParts = "[Worksheet]"; } masterKey.UnprotectXlsx(file); StatusText.Text += " OK. " + protectedParts + Environment.NewLine; } else { StatusText.Text += " Not protected." + Environment.NewLine; } } else if (masterKey.FileIsDocx(file)) { if (masterKey.DocxIsProtected(file)) { masterKey.UnprotectDocx(file); StatusText.Text += " OK. " + Environment.NewLine; } else { StatusText.Text += " Not protected." + Environment.NewLine; } } else { StatusText.Text += " Not recognized as valid DOCX or XLSX file type." + Environment.NewLine; } } catch (Exception x) { StatusText.Text += x.Message + Environment.NewLine; } }
static void Main(string[] args) { Console.WriteLine(""); Console.WriteLine("Office Master Key"); Console.WriteLine("Remove document protection mechanisms for DOCX and XLSX files."); Console.WriteLine(""); if (args.Length == 0) { Console.WriteLine("Usage: omkcli file1 [file2 ... [ fileN ] ] "); Console.WriteLine(""); return; } MasterKey masterKey = new MasterKey(); foreach (string arg in args) { Console.Write("Removing protection from: " + arg); try { if (masterKey.FileIsXlsx(arg)) { if (masterKey.XlsxIsProtected(arg)) { string protectedParts = ""; if (masterKey.XlsxIsWorkbookProtected(arg) && masterKey.XlsxIsWorksheetProtected(arg)) { protectedParts = "[Workbook,Worksheet]"; } else if (masterKey.XlsxIsWorkbookProtected(arg)) { protectedParts = "[Workbook]"; } else if (masterKey.XlsxIsWorksheetProtected(arg)) { protectedParts = "[Worksheet]"; } masterKey.UnprotectXlsx(arg); Console.WriteLine(" OK. " + protectedParts); } else { Console.WriteLine(" Not protected."); } } else if (masterKey.FileIsDocx(arg)) { if (masterKey.DocxIsProtected(arg)) { masterKey.UnprotectDocx(arg); Console.WriteLine(" OK."); } else { Console.WriteLine(" Not protected."); } } else { throw new ApplicationException("Not recognized as valid DOCX or XLSX file type."); } } catch (Exception ex) { Console.WriteLine(" Failed. " + ex.Message); } } }